Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. VERSION 1.0 CLASS
  2. BEGIN
  3. MultiUse = -1 'True
  4. Persistable = 0 'NotPersistable
  5. DataBindingBehavior = 0 'vbNone
  6. DataSourceBehavior = 0 'vbNone
  7. MTSTransactionMode = 0 'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "Collection2"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Private Const INTERNAL_KEY_SIGNATURE As String = "_Collection2_Internal_Key_"
  17.  
  18. Private m_items As Collection
  19. Private m_indexes As Collection
  20. Private m_keys As Collection
  21.  
  22. Public Sub Dispose()
  23. Set m_items = Nothing
  24. Set m_indexes = Nothing
  25. Set m_keys = Nothing
  26. End Sub
  27.  
  28. Private Sub Class_Initialize()
  29. Set m_items = New Collection
  30. Set m_indexes = New Collection
  31. Set m_keys = New Collection
  32. End Sub
  33.  
  34. Private Sub Class_Terminate()
  35. Dispose
  36. End Sub
  37.  
  38. Public Property Get Item(Key As Variant) As Variant
  39. Attribute Item.VB_UserMemId = 0
  40. Dim k As String
  41.  
  42. k = m_indexes(Key)
  43.  
  44. If VarType(m_items(k)) = vbObject Then
  45. Set Item = m_items(k)
  46. Else
  47. Item = m_items(k)
  48. End If
  49. End Property
  50.  
  51. Public Function NewEnum() As IUnknown
  52. Attribute NewEnum.VB_UserMemId = -4
  53. Set NewEnum = m_items.[_NewEnum]
  54. End Function
  55.  
  56. Public Sub Add(Item As Variant, Optional Key As Variant, Optional Before As Variant, Optional After As Variant)
  57. Dim k As Variant
  58. Dim b As Variant
  59. Dim a As Variant
  60.  
  61. k = NextInternalKey()
  62. If IsMissing(Key) Then
  63. CollectionAddItem m_indexes, k, k, Before, After
  64. Else
  65. CollectionAddItem m_indexes, k, Key, Before, After
  66. m_keys.Add Key, k
  67. End If
  68.  
  69. If Not IsMissing(Before) Then
  70. b = m_indexes(Before)
  71. End If
  72.  
  73. If Not IsMissing(After) Then
  74. a = m_indexes(After)
  75. End If
  76.  
  77. CollectionAddItem m_items, Item, k, b, a
  78. End Sub
  79.  
  80. Private Function CollectionAddItem(a_collection As Collection, Item As Variant, Key As Variant, Before As Variant, After As Variant) As String
  81. If IsEmpty(Before) And IsEmpty(After) Then
  82. a_collection.Add Item, Key
  83.  
  84. ElseIf Not IsEmpty(Before) And IsEmpty(After) Then
  85. a_collection.Add Item, Key, Before
  86.  
  87. ElseIf IsEmpty(Before) And Not IsEmpty(After) Then
  88. a_collection.Add Item, Key, , After
  89.  
  90. Else
  91. ' this will raise an expected error
  92. a_collection.Add Item, Key, Before, After
  93. End If
  94. End Function
  95.  
  96. Private Function NextInternalKey() As String
  97. Dim n As String
  98.  
  99. n = INTERNAL_KEY_SIGNATURE & CStr(Int(4294967296# * Rnd - 2147483648#)) & CStr(Int(4294967296# * Rnd - 2147483648#))
  100.  
  101. If HasKey(n) Then
  102. NextInternalKey = NextInternalKey()
  103. Else
  104. NextInternalKey = n
  105. End If
  106. End Function
  107.  
  108. Public Property Get Count() As Long
  109. Count = m_items.Count
  110. End Property
  111.  
  112. Public Sub Remove(Key As Variant)
  113. Dim k As Variant
  114. k = m_indexes(Key)
  115.  
  116. m_items.Remove k
  117. m_indexes.Remove Key
  118. m_keys.Remove k
  119. End Sub
  120.  
  121. Public Property Get Items() As Collection
  122. Dim c As New Collection
  123. Dim k As Variant
  124.  
  125. For Each k In m_indexes
  126. If CollectionHasKey(m_keys, k) Then
  127. c.Add m_items(k), m_keys(k)
  128. Else
  129. c.Add m_items(k)
  130. End If
  131. Next
  132.  
  133. Set Items = c
  134. End Property
  135.  
  136. Public Property Get Keys() As Collection
  137. Dim c As New Collection
  138. Dim Key As Variant
  139.  
  140. For Each Key In m_keys
  141. c.Add Key
  142. Next
  143.  
  144. Set Keys = c
  145. End Property
  146.  
  147. Public Function Exists(Item As Variant) As Boolean
  148. Dim match As Boolean
  149. Dim v As Variant
  150.  
  151. For Each v In m_items
  152. Select Case VarType(v)
  153. Case vbEmpty
  154. match = VarType(Item) = vbEmpty
  155.  
  156. Case vbNull
  157. match = VarType(Item) = vbNull
  158.  
  159. Case vbByte, vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDecimal
  160. If IsNumeric(Item) Then
  161. match = v = Item
  162. End If
  163.  
  164. Case vbDate
  165. If VarType(Item) = vbDate Then
  166. match = v = Item
  167. End If
  168.  
  169. Case vbString
  170. If VarType(Item) = vbString Then
  171. match = v = Item
  172. End If
  173.  
  174. Case vbObject
  175. If VarType(Item) = vbObject Then
  176. match = v Is Item
  177. End If
  178.  
  179. Case vbBoolean
  180. If VarType(Item) = vbBoolean Then
  181. match = v = Item
  182. End If
  183.  
  184. ' vbError, vbVariant, vbDataObject, vbUserDefinedType, vbArray
  185. Case Else
  186. Err.Raise 3169
  187.  
  188. End Select
  189.  
  190. If match Then
  191. Exit For
  192. End If
  193. Next
  194.  
  195. Exists = match
  196. End Function
  197.  
  198. Public Function HasKey(Key As String) As Boolean
  199. HasKey = CollectionHasKey(m_indexes, Key)
  200. End Function
  201.  
  202. Public Function CollectionHasKey(a_collection As Collection, a_Key As Variant) As Boolean
  203. Dim l As Long
  204.  
  205. On Error Resume Next
  206. l = VarType(a_collection(a_Key))
  207. CollectionHasKey = Err.Number = 0
  208. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement