| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
#1
|
||||
|
||||
|
ever since I met ArrayList in .NET, I can't get it out of my thoughts. it was love from first sight. as classic ASP does not have this wonderful component, I decided I would add it.
![]() Code:
<% Option Explicit %>
<%
Class ArrayList
Private CAPACITY
Private m_arrData()
Private m_Count
Private m_DefaultComparer
Private Sub Class_Initialize
CAPACITY=100
Redim m_arrData(CAPACITY-1)
m_Count=0
m_DefaultComparer=""
End Sub
Public Property Get Count
Count=m_Count
End Property
Public Property Get DefaultComparer
DefaultComparer=m_DefaultComparer
End Property
Public Property Let DefaultComparer(Value)
m_DefaultComparer=Value
End Property
Public Sub Add(value)
If m_Count>UBound(m_arrData) Then
Call MakeMorePlace
End If
If IsObject(value) Then
Set m_arrData(m_Count)=value
Else
m_arrData(m_Count)=value
End If
m_Count=m_Count+1
End Sub
Public Sub Remove(value)
Dim x
For x=0 To m_Count-1
If Compare(m_arrData(x), value, m_DefaultComparer)=0 Then
m_arrData(x)=Empty
End If
Next
Call RemoveEmptyItems
End Sub
Public Default Property Get Item(index)
If Not(IsNumeric(index)) Then
Err.Raise 15000, "ArrayList", "invalid parameter given to Item: not numeric"
End If
If (index<0) Or (index>=m_Count) Then
Err.Raise 15001, "ArrayList", "index out of range: ("&index&") can't be less than zero or greater than count"
End If
If IsObject(m_arrData(index)) Then
Set Item=m_arrData(index)
Else
Item=m_arrData(index)
End If
End Property
Public Function ToArray()
Dim result(), x
If m_Count=0 Then
ToArray=Array() 'empty array
Exit Function
End If
ReDim result(m_Count-1)
For x=0 To m_Count-1
If IsObject(m_arrData(x)) Then
Set result(x)=m_arrData(x)
Else
result(x)=m_arrData(x)
End If
Next
ToArray=result
End Function
Public Sub Sort(strComparer)
Dim x, y, maxValue
Dim maxIndex, curValue, tempValue
For x=0 To m_Count-1
maxIndex=x
If IsObject(m_arrData(x)) Then
Set maxValue=m_arrData(x)
Else
maxValue=m_arrData(x)
End If
For y=x+1 To m_Count-1
If IsObject(m_arrData(y)) Then
Set curValue=m_arrData(y)
Else
curValue=m_arrData(y)
End If
If Compare(curValue, maxValue, strComparer)>0 Then
If IsObject(curValue) Then
Set maxValue=curValue
Else
maxValue=curValue
End If
maxIndex=y
End If
Next
If maxIndex<>x Then
If IsObject(m_arrData(maxIndex)) Then
Set tempValue=m_arrData(maxIndex)
Else
tempValue=m_arrData(maxIndex)
End If
If IsObject(m_arrData(x)) Then
Set m_arrData(maxIndex)=m_arrData(x)
Else
m_arrData(maxIndex)=m_arrData(x)
End If
If IsObject(tempValue) Then
Set m_arrData(x)=tempValue
Else
m_arrData(x)=tempValue
End If
End If
Next
End Sub
Private Sub MakeMorePlace
ReDim Preserve m_arrData(UBound(m_arrData)+CAPACITY)
End Sub
Private Sub RemoveEmptyItems
Dim blnContinue, x, delIndex
blnContinue=True
Do Until blnContinue=False
delIndex=-1
For x=0 To m_Count-1
If IsEmpty(m_arrData(x)) Then
delIndex=x
End If
Next
If delIndex>=0 Then
For x=delIndex To m_Count-2
If IsObject(m_arrData(x+1)) Then
Set m_arrData(x)=m_arrData(x+1)
Else
m_arrData(x)=m_arrData(x+1)
End If
Next
m_arrData(m_Count-1)=Empty
m_Count=m_Count-1
End If
blnContinue=(delIndex >= 0)
Loop
End Sub
Private Function Compare(item1, item2, strComparer)
'look for custom comparer:
If Len(strComparer)>0 Then
On Error Resume Next
Compare=Eval(strComparer&"(item1, item2)")
If Err.Number<>0 Then
On Error Goto 0
Err.Raise 15010, "ArrayList", "Can't compare items: invalid comparer or error on custom comparison: "&Err.Description&" ("&strComparer&")"
End If
On Error Goto 0
Exit Function
End If
'no default comparer, use built in comparison, if possible:
If IsObject(item1) Or IsObject(item2) Then
Err.Raise 15002, "ArrayList", "Can't compare items: no custom comparer is defined and complex items found."
End If
'If TypeName(item1)=TypeName(item2) Then
If item1=item2 Then
Compare=0
Else
If item1>item2 Then
Compare=1
Else
Compare=-1
End If
End If
'End If
End Function
Private Sub Class_Terminate
Dim x
For x=0 To UBound(m_arrData)
If IsObject(m_arrData(x)) Then
Set m_arrData(x)=Nothing
End If
Next
Erase m_arrData
End Sub
End Class
'must return zero if items are equal, positive number if item1 bigger then item2 and negative number if item2 is bigger than item1.
Function MyComparer(item1, item2)
If TypeName(item1)="String" And TypeName(item2)="String" Then
MyComparer=StrComp(item1, item2)
Exit Function
End If
If TypeName(item1)="Integer" And TypeName(item2)="Integer" Then
MyComparer=CLng(item1)-CLng(item2)
Exit Function
End If
If TypeName(item1)="String" And TypeName(item2)="Integer" Then
If IsNumeric(item1) Then
MyComparer=CLng(item1)-CLng(item2)
Exit Function
End If
MyComparer=1
Exit Function
End If
If TypeName(item1)="Integer" And TypeName(item2)="String" Then
If IsNumeric(item2) Then
MyComparer=CLng(item1)-CLng(item2)
Exit Function
End If
MyComparer=-1
Exit Function
End If
MyComparer=0
End Function
Function NameComparer(file1, file2)
NameComparer=StrComp(file1.Name, file2.Name, 1)
End Function
Function SizeComparer(file1, file2)
SizeComparer=file1.Size-file2.Size
End Function
Function DateCreatedComparer(file1, file2)
DateCreatedComparer=DateDiff("s", file2.DateCreated, file1.DateCreated)
End Function
Function DateLastModifiedComparer(file1, file2)
DateLastModifiedComparer=DateDiff("s", file2.DateLastModified, file1.DateLastModified)
End Function
Function ExtensionComparer(file1, file2)
Dim strExtension1, strExtension2, arrTemp
arrTemp=Split(file1.Name, ".")
strExtension1=arrTemp(UBound(arrTemp))
Erase arrTemp
arrTemp=Split(file2.Name, ".")
strExtension2=arrTemp(UBound(arrTemp))
Erase arrTemp
ExtensionComparer=StrComp(strExtension1, strExtension2, 1)
End Function
'usage:
Dim myList
Set myList=New ArrayList
Call myList.Add(5)
Call myList.Add(8)
Call myList.Add("hello")
Response.Write "ordinary ArrayList with numbers and string, before sorting:<br />"
Response.Write Join(myList.ToArray(), "<br />")
Response.Write "<hr />"
Call myList.Sort("MyComparer")
Response.Write "ordinary ArrayList with numbers and string, after sorting with custom comparer:<br />"
Response.Write Join(myList.ToArray(), "<br />")
Response.Write "<hr />"
Call myList.Remove(8)
Response.Write "ordinary ArrayList with numbers and string, after the item 8 has been removed:<br />"
Response.Write Join(myList.ToArray(), "<br />")
Response.Write "<hr />"
Dim FOLDER_PATH :FOLDER_PATH=Server.MapPath("images")
Dim filesList, objFSO, objFolder
Dim objFile, strData, x
Set filesList=New ArrayList
Set objFSO=Server.CreateObject("Scripting.FileSystemObject")
Set objFolder=objFSO.GetFolder(FOLDER_PATH)
For Each objFile In objFolder.Files
Call filesList.Add(objFile)
Next
Set objFolder=Nothing
Set objFSO=Nothing
strData="Contents of the folder "&FOLDER_PATH&", raw: <br />"
For x=0 To filesList.Count-1
strData=strData&filesList(x).Name&"<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("NameComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by name: <br />"
For x=0 To filesList.Count-1
strData=strData&filesList(x).Name&"<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("SizeComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by size: <br />"
For x=0 To filesList.Count-1
strData=strData&filesList(x).Name&" ("&filesList(x).Size&" bytes)<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("DateCreatedComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by date created: <br />"
For x=0 To filesList.Count-1
strData=strData&filesList(x).Name&" (created at: "&filesList(x).DateCreated&")<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("DateLastModifiedComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by date last modified: <br />"
For x=0 To filesList.Count-1
strData=strData&filesList(x).Name&" (date last modified: "&filesList(x).DateLastModified&")<br />"
Next
Response.Write strData
Response.Write("<hr />")
Call filesList.Sort("ExtensionComparer")
strData="Contents of the folder "&FOLDER_PATH&", sorted by extension: <br />"
For x=0 To filesList.Count-1
strData=strData&filesList(x).Name&"<br />"
Next
Response.Write strData
%>
seriously now... this component is the most useful I know. very generic, very flexible and can make life lot easier. quick guide for those unfamiliar with such component: http://forums.aspfree.com/showpost....205&postcount=4 (due to 10k characters limit in single post, I can't have it all in the same post) Happy Coding! ![]() Last edited by Shadow Wizard : July 9th, 2005 at 10:50 AM. Reason: new version |
|
#2
|
||||
|
||||
|
What about adding a Sort method, to automatically sort the elements?
![]() |
|
#3
|
||||
|
||||
|
yeah, I mentioned this - intend to add this in the next version.
![]() however, this will require the user (programmer using the code) to implement custom comparer - not going to be simple to explain such thing. ![]() |
|
#4
|
||||
|
||||
|
Quick Guide:
1. have the Class code in seperate file called ArrayList.asp and include this file whenever you need to use ArrayList. 2. to create instance of the component, simply have this: Code:
Set myList=New ArrayList myList would be the ArrayList object. 3. to add items, have such syntax: Code:
Call myList.Add("hello")
you can add whatever you want including objects of all kinds e.g. recordset however you won't be able to Remove such objects. 4. to remove items, have such code: Code:
Call myList.Remove("hello")
this will remove all items equal to this. 5. to iterate over all items have such code: Code:
For x=0 To myList.Count-1
Response.Write("item #"&(x+1)&": "&myList(x)&"<br />")
Next
(this is actually shortcut for myList.Item(x)) 6. to get single item at desired index: Code:
myList(index) 7. to get ordinary one dimension array out of the ArrayList: Code:
myArray=myList.ToArray() --Added at July 9th, 2005: Sort has been added, now the ArrayList can be sorted using custom comparer. The comparer is getting two items from the ArrayList and must return integer number. it must return 0 if the items are both the same, positive number if the first item is bigger than the second item and negative number if the second item is bigger than the first item. the return value itself (e.g. if it's 5 or 10) does not matter. this comparer is used when comparing items to find the biggest and smallest items when sorting. Two basic examples has been added as well: one for list with basic type items (numbers and strings) and one for Files, where the ArrayList hold files from specific folder on the server. this demonstrates how simple it becomes to sort files using the ArrayList sort method - only 3 lines for most comparers. --- that's it, for now... feel free to comment, suggest and point problems. this is the final version, I plan to add method AddRange that would get array of items and add those items to the ArrayList one by one, it would be useful for example when storing the ArrayList, as Array (using the ToArray() method), in the Session, then you have to build the ArrayList back in some other page. Happy Coding! ![]() |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Classic ASP and vbscript ArrayList Version 1.0 |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|