forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayUtilities.rvb
242 lines (226 loc) · 6.94 KB
/
ArrayUtilities.rvb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ArrayUtilities.rvb -- January 2011
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Verifies that the array has been dimmed.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function IsArrayDim(ByVal arr)
IsArrayDim = False
If IsArray(arr) Then
On Error Resume Next
Call UBound(arr)
If Err.Number = 0 Then IsArrayDim = True
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Safely returns the upper bounds of an array
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function SafeUBound(ByVal arr)
Dim ub
SafeUBound = Null
If IsArray(arr) Then
On Error Resume Next
ub = UBound(arr)
If Err.Number = 0 Then
SafeUBound = ub
Else
SafeUBound = -1
End If
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Returns the number of dimensions to an array
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetArrayDim(ByVal arr)
Dim i
If IsArray(arr) Then
For i = 1 To 60
On Error Resume Next
Call UBound(arr, i)
If Err.Number <> 0 Then
GetArrayDim = i - 1
Exit Function
End If
Next
GetArrayDim = i
Else
GetArrayDim = Null
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Adds a new element to the end of the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayAdd(ByRef arr, ByVal val)
Dim ub
If IsArray(arr) Then
On Error Resume Next
ub = UBound(arr)
If Err.Number <> 0 Then ub = -1
ReDim Preserve arr(ub + 1)
arr(UBound(arr)) = val
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Appends another array to the end of the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayAppend(ByRef arr, ByVal arr0)
Dim i, ub
If IsArray(arr) And IsArray(arr0) Then
On Error Resume Next
ub = UBound(arr)
If Err.Number <> 0 Then ub = -1
ReDim Preserve arr(ub + UBound(arr0) + 1)
For i = 0 To UBound(arr0)
arr(ub + 1 + i) = arr0(i)
Next
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Inserts a new element at a given position in the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val)
Dim i
If IsArray(arr) Then
If pos > UBound(arr) Then
Call ArrayAdd(arr, val)
ElseIf pos >= 0 Then
ReDim Preserve arr(UBound(arr) + 1)
For i = UBound(arr) To pos + 1 Step -1
arr(i) = arr(i - 1)
Next
arr(pos) = val
End If
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Removes an element from the end of the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayRemove(ByRef arr)
If IsArray(arr) Then
If UBound(arr) > -1 Then
ReDim Preserve arr(UBound(arr) - 1)
End If
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Removes an element at a given position from the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayRemoveAt(ByRef arr, ByVal pos)
Dim i
If IsArray(arr) Then
If pos >= 0 And pos <= UBound(arr) Then
For i = pos To UBound(arr) - 1
arr(i) = arr(i + 1)
Next
ReDim Preserve arr(UBound(arr) - 1)
End If
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Removes all instances of a value from the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayRemoveVal(ByRef arr, ByVal val)
Dim i, j
If IsArray(arr) Then
i = 0 : j = -1
For i = 0 To UBound(arr)
If arr(i) <> val Then
j = j + 1
arr(j) = arr(i)
End If
Next
ReDim Preserve Array(j)
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Removes duplicate items from the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayCull(ByRef arr)
Dim i, dict
If IsArray(arr) Then
Set dict = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(arr)
If Not dict.Exists(arr(i)) Then
Call dict.Add(arr(i), arr(i))
End If
Next
arr = dict.Items
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purges null elements from an array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayPurge(ByRef arr)
Dim i, j
If IsArray(arr) Then
i = 0
j = -1
For i = 0 To UBound(arr)
If Not IsNull(arr(i)) Then
j = j + 1
arr(j) = arr(i)
End If
Next
ReDim Preserve Array(j)
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Find the first occurance of a value in an array
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ArraySearch(ByRef arr, ByVal val)
Dim i
ArraySearch = -1
If IsArray(arr) Then
For i = 0 To UBound(arr)
If arr(i) = val Then
ArraySearch = i
Exit Function
End If
Next
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Sorts the elements in the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArraySort(ByRef arr)
Dim i, j, tmp
If IsArray(arr) Then
For i = UBound(arrShort) - 1 To 0 Step -1
For j = 0 To i
If arr(j) > arr(j + 1) Then
tmp = arr(j + 1)
arr(j + 1) = arr(j)
arr(j) = tmp
End If
Next
Next
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Reverse the order of the elements in the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayReverse(ByRef arr)
Dim i, ub, ub2, tmp
ub = UBound(arr)
ub2 = Int(ub / 2)
For i = 0 To ub2
tmp = arr(i)
arr(i) = arr(ub - i)
arr(ub - i) = tmp
Next
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Dumps the array to the Rhino command window.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ArrayDump(ByVal arr)
Dim i
If IsArray(arr) Then
For i = 0 To UBound(arr)
Call Rhino.Print("Item(" & CStr(i) & ") = " & CStr(arr(i)))
Next
End If
End Sub