Posted By

Scooter on 05/30/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

wizard04
benrudolph


Dynamic Array Class


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2009/05/dynamic-arrays.html

I want to do a little more work on this, but as far as I know everything should be working fine.

  1. <%
  2. Class DynamicArray
  3. private data
  4.  
  5. ' Constructor
  6. private sub Class_Initialize()
  7. data = Array()
  8. end sub
  9.  
  10. ' Returns the number of items in the array.
  11. public property get Count
  12. if ubound(data) > 0 then
  13. Count = ubound(data) + 1
  14. else
  15. Count = 0
  16. end if
  17. end property
  18.  
  19. ' Return the item at the specified index of the array.
  20. public property get Item(position)
  21. 'Make sure the end developer is not requesting an "out of bounds" array element.
  22. if position < lbound(data) or position > ubound(data) then
  23. 'Invalid range.
  24. exit property
  25. end if
  26.  
  27. Item = data(position)
  28. end property
  29.  
  30. ' Return the array itself.
  31. public property get Items()
  32. Items = data
  33. end property
  34.  
  35. ' Set the item at the specified index of the array.
  36. public property let Item(position, value)
  37. 'Make sure position >= lbound(data)
  38. if position < lbound(data) then
  39. exit property
  40. end if
  41.  
  42. if position > ubound(data) then
  43. 'We need to resize the array
  44. redim preserve data(position)
  45. data(position) = value
  46. else
  47. 'We don't need to resize the array
  48. data(position) = value
  49. end if
  50. end property
  51.  
  52. ' Remove the item from the specified index of the array.
  53. public sub Remove(position)
  54. dim i
  55.  
  56. 'Make sure position is within acceptable ranges.
  57. if position < lbound(data) or position > ubound(data) then
  58. 'Invalid range.
  59. exit sub
  60. end if
  61.  
  62. for i = position to ubound(data) - 1
  63. data(i) = data(i + 1)
  64. next
  65.  
  66. redim preserve data(ubound(data) - 1)
  67. end sub
  68.  
  69. ' Erase the entire contents of the array.
  70. public sub RemoveAll()
  71. redim data(0)
  72. end sub
  73.  
  74. ' Add an item to the array.
  75. public sub Add(value)
  76. redim preserve data(ubound(data) + 1)
  77. if isObject(value) then
  78. set data(ubound(data)) = value
  79. else
  80. data(ubound(data)) = value
  81. end if
  82. end sub
  83.  
  84. ' Perform a binary search on the values in the array.
  85. private function BinarySearch(value, startPosition, endPosition)
  86. dim midPoint
  87.  
  88. if startPosition = endPosition then
  89. 'Array only contains one item.
  90. if value = data(startPosition) then
  91. BinarySearch = startPosition
  92. else
  93. 'Where it would have been...
  94. BinarySearch = -(startPosition)
  95. end if
  96. elseif startPosition > endPosition then
  97. 'Can't search backwards.
  98. BinarySearch = -(startPosition)
  99. else
  100. 'Array contains multiple items.
  101. midPoint = clng(startPosition + ((endPosition - startPosition) / 2))
  102.  
  103. 'if we're lucky, the value is at the midpoint.
  104. if value = aTheList(midPoint) then
  105. BinarySearch = midPoint
  106. else
  107. 'Determine on which side of the midpoint to look for the value.
  108. if value < data(midPoint) then
  109. BinarySearch = BinarySearch(value, startPosition, midPoint - 1)
  110. else
  111. BinarySearch = BinarySearch(value, midPoint + 1, endPosition)
  112. end if
  113. end if
  114. end if
  115. end function
  116.  
  117. ' Check if an item exists in the array.
  118. public function Exists(value)
  119. Exists = BinarySearch(value, lbound(data), ubound(data))
  120. end function
  121.  
  122. ' Select a random item from the array.
  123. public function Random()
  124. Random = data(Int((uBound(data) - lBound(data) + 1) * Rnd + lBound(data)))
  125. end function
  126.  
  127. ' Reverse the order of the items in the array.
  128. public function Reverse()
  129. for i = 0 to Round(ubound(data) / 2)
  130. call Swap(lbound(data) + i, ubound(data) - i)
  131. next
  132. end function
  133.  
  134. ' Sort the array alphanumerically.
  135. public sub Sort()
  136. call QuickSort(lbound(data), ubound(data))
  137. end sub
  138.  
  139. ' Quick sort is the fastest sort algorithm provided by this class.
  140. private sub QuickSort(loBound, hiBound)
  141. dim pivot
  142. dim loSwap
  143. dim hiSwap
  144.  
  145. if hiBound - loBound = 1 then
  146. if data(loBound) > data(hiBound) then
  147. call Swap(loBound, hiBound)
  148. end if
  149. end if
  150.  
  151. pivot = data(cint((loBound + hiBound) / 2))
  152. data(cint((loBound + hiBound) / 2)) = data(loBound)
  153. data(loBound) = pivot
  154. loSwap = loBound + 1
  155. hiSwap = hiBound
  156.  
  157. do
  158. do while loSwap < hiSwap and data(loSwap) <= pivot
  159. loSwap = loSwap + 1
  160. loop
  161.  
  162. do while data(hiSwap) > pivot
  163. hiSwap = hiSwap - 1
  164. loop
  165.  
  166. if loSwap < hiSwap then
  167. call Swap(loSwap, hiSwap)
  168. end if
  169. loop while loSwap < hiSwap
  170.  
  171. data(loBound) = data(hiSwap)
  172. data(hiSwap) = pivot
  173.  
  174. if loBound < (hiSwap - 1) then
  175. call QuickSort(loBound, hiSwap - 1)
  176. end if
  177.  
  178. if hiSwap + 1 < hibound then
  179. call QuickSort(hiSwap + 1, hiBound)
  180. end if
  181. end sub
  182.  
  183. ' This sorting method is not used by this class because it is slow, but is provided here for fun.
  184. private sub BubbleSort()
  185. dim i
  186. dim j
  187.  
  188. for i = ubound(data) - 1 to 0 step -1
  189. for j = 0 to i
  190. if data(j) > data(j + 1) then
  191. call Swap(j + 1, j)
  192. end if
  193. next
  194. next
  195. end sub
  196.  
  197. ' This sorting method is not used by this class because it is slow, but is provided here for fun.
  198. private sub CombSort()
  199. dim i
  200. dim gap
  201. dim isSwapped
  202. dim arraySize
  203.  
  204. const shrink = 1.3
  205.  
  206. arraySize = ubound(data)
  207. gap = arraySize - 1
  208.  
  209. do
  210. gap = Int(gap / shrink)
  211. isSwapped = true
  212.  
  213. for i = 0 to arraySize - gap
  214. if data(i) > data(i + gap) then
  215. call Swap(i, i + gap)
  216. isSwapped = false
  217. end if
  218. next
  219. loop until not isSwapped and gap = 1
  220. end sub
  221.  
  222. ' This sorting method is not used by this class because it is slow, but is provided here for fun.
  223. private sub ExchangeSort()
  224. dim front
  225. dim back
  226. dim arraySize
  227.  
  228. arraySize = ubound(data)
  229.  
  230. for front = 0 to arraySize
  231. for back = front to arraySize
  232.  
  233. if data(front) > data(back) then
  234. call Swap(front, back)
  235. end if
  236. next
  237. next
  238. end sub
  239.  
  240. ' This sorting method is not used by this class because it is slow, but is provided here for fun.
  241. private sub SelectionSort
  242. dim front
  243. dim back
  244. dim location
  245. dim arraySize
  246.  
  247. arraySize = ubound(data)
  248.  
  249. for front = 0 to arraySize - 1
  250. location = front
  251.  
  252. for back = front to arraySize
  253. if data(location) > data(back) then
  254. location = back
  255. end if
  256. next
  257.  
  258. call Swap(location, front)
  259. next
  260. end sub
  261.  
  262. ' Randomize the order of the items in the array.
  263. public sub Shuffle
  264. dim i
  265.  
  266. randomize timer
  267.  
  268. for each i in data
  269. call Swap(i, int(rnd * (ubound(data) + 1)))
  270. next
  271. end sub
  272.  
  273. ' Swap two specified items in the array; used by sorting algorithms.
  274. private sub Swap(x, y)
  275. dim temp
  276.  
  277. temp = data(x)
  278. data(x) = data(y)
  279. data(y) = temp
  280. end sub
  281.  
  282. ' Return the sum of the values of the array.
  283. public function Sum()
  284. dim i
  285. dim result
  286.  
  287. result = 0
  288.  
  289. ' Only numeric items can be added.
  290. for each i in data
  291. if isNumeric(i) then
  292. result = result + i
  293. end if
  294. next
  295.  
  296. Sum = result
  297. end function
  298.  
  299. ' Return the product of the values of the array.
  300. public function Product()
  301. dim i
  302. dim result
  303.  
  304. result = 0
  305.  
  306. ' Only numeric items can be multiplied.
  307. for each i in data
  308. if isNumeric(i) then
  309. result = result * i
  310. end if
  311. next
  312.  
  313. Product = result
  314. end function
  315.  
  316. ' Destructor
  317. private sub Class_Terminate
  318. data = nothing
  319. end sub
  320. end Class
  321. %>

Report this snippet  

You need to login to post a comment.