/ 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.
Expand |
Embed | Plain Text
<% Class DynamicArray private data ' Constructor private sub Class_Initialize() data = Array() end sub ' Returns the number of items in the array. public property get Count if ubound(data) > 0 then Count = ubound(data) + 1 else Count = 0 end if end property ' Return the item at the specified index of the array. public property get Item(position) 'Make sure the end developer is not requesting an "out of bounds" array element. if position < lbound(data) or position > ubound(data) then 'Invalid range. exit property end if Item = data(position) end property ' Return the array itself. public property get Items() Items = data end property ' Set the item at the specified index of the array. public property let Item(position, value) 'Make sure position >= lbound(data) if position < lbound(data) then exit property end if if position > ubound(data) then 'We need to resize the array redim preserve data(position) data(position) = value else 'We don't need to resize the array data(position) = value end if end property ' Remove the item from the specified index of the array. public sub Remove(position) dim i 'Make sure position is within acceptable ranges. if position < lbound(data) or position > ubound(data) then 'Invalid range. exit sub end if for i = position to ubound(data) - 1 data(i) = data(i + 1) next redim preserve data(ubound(data) - 1) end sub ' Erase the entire contents of the array. public sub RemoveAll() redim data(0) end sub ' Add an item to the array. public sub Add(value) redim preserve data(ubound(data) + 1) if isObject(value) then set data(ubound(data)) = value else data(ubound(data)) = value end if end sub ' Perform a binary search on the values in the array. private function BinarySearch(value, startPosition, endPosition) dim midPoint if startPosition = endPosition then 'Array only contains one item. if value = data(startPosition) then BinarySearch = startPosition else 'Where it would have been... BinarySearch = -(startPosition) end if elseif startPosition > endPosition then 'Can't search backwards. BinarySearch = -(startPosition) else 'Array contains multiple items. midPoint = clng(startPosition + ((endPosition - startPosition) / 2)) 'if we're lucky, the value is at the midpoint. if value = aTheList(midPoint) then BinarySearch = midPoint else 'Determine on which side of the midpoint to look for the value. if value < data(midPoint) then BinarySearch = BinarySearch(value, startPosition, midPoint - 1) else BinarySearch = BinarySearch(value, midPoint + 1, endPosition) end if end if end if end function ' Check if an item exists in the array. public function Exists(value) Exists = BinarySearch(value, lbound(data), ubound(data)) end function ' Select a random item from the array. public function Random() Random = data(Int((uBound(data) - lBound(data) + 1) * Rnd + lBound(data))) end function ' Reverse the order of the items in the array. public function Reverse() for i = 0 to Round(ubound(data) / 2) call Swap(lbound(data) + i, ubound(data) - i) next end function ' Sort the array alphanumerically. public sub Sort() call QuickSort(lbound(data), ubound(data)) end sub ' Quick sort is the fastest sort algorithm provided by this class. private sub QuickSort(loBound, hiBound) dim pivot dim loSwap dim hiSwap if hiBound - loBound = 1 then if data(loBound) > data(hiBound) then call Swap(loBound, hiBound) end if end if pivot = data(cint((loBound + hiBound) / 2)) data(cint((loBound + hiBound) / 2)) = data(loBound) data(loBound) = pivot loSwap = loBound + 1 hiSwap = hiBound do do while loSwap < hiSwap and data(loSwap) <= pivot loSwap = loSwap + 1 loop do while data(hiSwap) > pivot hiSwap = hiSwap - 1 loop if loSwap < hiSwap then call Swap(loSwap, hiSwap) end if loop while loSwap < hiSwap data(loBound) = data(hiSwap) data(hiSwap) = pivot if loBound < (hiSwap - 1) then call QuickSort(loBound, hiSwap - 1) end if if hiSwap + 1 < hibound then call QuickSort(hiSwap + 1, hiBound) end if end sub ' This sorting method is not used by this class because it is slow, but is provided here for fun. private sub BubbleSort() dim i dim j for i = ubound(data) - 1 to 0 step -1 for j = 0 to i if data(j) > data(j + 1) then call Swap(j + 1, j) end if next next end sub ' This sorting method is not used by this class because it is slow, but is provided here for fun. private sub CombSort() dim i dim gap dim isSwapped dim arraySize const shrink = 1.3 arraySize = ubound(data) gap = arraySize - 1 do gap = Int(gap / shrink) isSwapped = true for i = 0 to arraySize - gap if data(i) > data(i + gap) then call Swap(i, i + gap) isSwapped = false end if next loop until not isSwapped and gap = 1 end sub ' This sorting method is not used by this class because it is slow, but is provided here for fun. private sub ExchangeSort() dim front dim back dim arraySize arraySize = ubound(data) for front = 0 to arraySize for back = front to arraySize if data(front) > data(back) then call Swap(front, back) end if next next end sub ' This sorting method is not used by this class because it is slow, but is provided here for fun. private sub SelectionSort dim front dim back dim location dim arraySize arraySize = ubound(data) for front = 0 to arraySize - 1 location = front for back = front to arraySize if data(location) > data(back) then location = back end if next call Swap(location, front) next end sub ' Randomize the order of the items in the array. public sub Shuffle dim i randomize timer for each i in data call Swap(i, int(rnd * (ubound(data) + 1))) next end sub ' Swap two specified items in the array; used by sorting algorithms. private sub Swap(x, y) dim temp temp = data(x) data(x) = data(y) data(y) = temp end sub ' Return the sum of the values of the array. public function Sum() dim i dim result result = 0 ' Only numeric items can be added. for each i in data if isNumeric(i) then result = result + i end if next Sum = result end function ' Return the product of the values of the array. public function Product() dim i dim result result = 0 ' Only numeric items can be multiplied. for each i in data if isNumeric(i) then result = result * i end if next Product = result end function ' Destructor private sub Class_Terminate data = nothing end sub end Class %>
You need to login to post a comment.
