-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patharray.vbs
147 lines (118 loc) · 3.27 KB
/
array.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'
' Array module
'
' Author: Jardel Weyrich <[email protected]>
'
Option Explicit
Function IsArray(ByRef value)
If ((VarType(value) And vbArray) = vbArray) Then
IsArray = True
Else
IsArray = False
End If
End Function
Function IsArrayOf(ByRef value, ByVal valueType)
If ((VarType(value) And vbArray) = vbArray) And ((VarType(value) And valueType) = valueType) Then
IsArrayOf = True
Else
IsArrayOf = False
End If
End Function
Function ArraySize(ByRef array())
ArraySize = 0
If VarType(array) = vbNull Or VarType(array) = vbEmpty Then
Exit Function
End If
If Not IsArray(array) Then
LogDebug("ArraySize: the parameter is not an array (array=" & array & ")")
Exit Function
End If
ArraySize = UBound(array) + 1
End Function
Function ArrayCountNotEmptyOrNull(ByRef array())
Dim i, count: count = 0
For i = LBound(array) To UBound(array)
If Not (IsEmpty(array(i)) Or IsNull(array(i))) Then
count = count + 1
End If
Next
ArrayCountNotEmptyOrNull = count
End Function
Function ArrayFind(ByRef array(), ByRef value)
ArrayFind = -1
Dim found: found = False
Dim i
For i = LBound(array) To UBound(array)
If array(i) = value Then
found = True
ArrayFind = i
Exit For
End If
Next
End Function
' Code from http://www.4guysfromrolla.com/webtech/032800-1.shtml#postadlink
Class DynamicArray
Private data_
Private size_
Private used_
Private Sub Class_Initialize()
ReDim data_(7) ' Valid indices ragen from 0 to 7
size_ = 8
used_ = 0
End Sub
Public Property Get HasItems()
HasItems = used_ > 0
End Property
Public Property Get Size()
Size = size_
End Property
Public Property Get Used()
Used = used_
End Property
Public Property Get Items()
Items = data_
End Property
Public Property Get StartIndex()
StartIndex = 0
End Property
Public Property Get EndIndex()
EndIndex = used_
End Property
Public Property Get Item(ByVal index)
If index < 0 Then Call Err.Raise(5000, "DynamicArray.(Get Item)", "Index out of range (< 0)")
If index >= used_ Then Call Err.Raise(5000, "DynamicArray.(Get Item)", "Index out of range (>= used_)")
Item = data_(index)
End Property
Public Property Let Item(ByVal index, ByRef value)
If index < 0 Then Call Err.Raise(5000, "DynamicArray.(Let Item)", "Index out of range (< 0)")
If index >= size_ Then Call Err.Raise(5000, "DynamicArray.(Let Item)", "Index out of range (>= size_)")
data_(index) = value
End Property
Public Sub Resize(ByVal newSize)
If newSize = size_ Then Exit Sub ' Same size
If (newSize > size_) Then ' Increasing size
Redim Preserve data_(newSize)
size_ = newSize
Else ' Decreasing size
If newSize < used_ Then
Call Err.Raise(5000, "DynamicArray.Resize", "newSize can't be smaller than used_")
End If
Redim Preserve data_(newSize)
size_ = newSize
End If
End Sub
' Add to the end
Public Sub Add(ByRef value)
If size_ <= used_ Then
Resize(size_ * 2) ' Amortized resize
End If
data_(used_) = value
used_ = used_ + 1
End Sub
' Remove from the end
Public Sub Remove()
If index < 0 Then Call Err.Raise(5000, "DynamicArray.Remove", "Index out of range (< 0)")
If index >= used_ Then Call Err.Raise(5000, "DynamicArray.Remove", "Index out of range (>= used_)")
data_(used_ - 1) = Empty
End Sub
End Class