forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicArray.rvb
160 lines (145 loc) · 5.69 KB
/
DynamicArray.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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DynamicArray.rvb -- May 2012
' 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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Class: DynamicArray
' Purpose: VBScript dynamic array class
' Usage:
' Set arr = New DynamicArray ' Create an instance of DynamicArray
' arr.Add "Hello Rhino" ' Add a new element
' Rhino.Print arr.Data(0) ' Retrieve the value
' Set arr = Nothing ' Destroy the instance
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Class DynamicArray
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Private data member
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private m_aData
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Initialize event.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_Initialize()
m_aData = Array(0)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Terminate event.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_Terminate()
ReDim m_aData(0)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Returns the value at a given position in the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get Data(iPos)
If iPos < LBound(m_aData) Or iPos > UBound(m_aData) Then
Exit Property
End If
If IsObject(m_aData(iPos)) Then
Set Data = m_aData(iPos)
Else
Data = m_aData(iPos)
End If
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Sets the value at a given position in the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Let Data(iPos, vValue)
If iPos < LBound(m_aData) Then Exit Property
If iPos > UBound(m_aData) Then
ReDim Preserve m_aData(iPos)
End If
If IsObject(vValue) Then
Set m_aData(iPos) = vValue
Else
m_aData(iPos) = vValue
End If
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Returns the entire array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get DataArray()
DataArray = m_aData
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Returns the starting index of the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get StartIndex()
StartIndex = LBound(m_aData)
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Returns the ending index of the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get EndIndex()
EndIndex = UBound(m_aData)
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Returns the number of data elements in the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get Count()
Count = UBound(m_aData) - LBound(m_aData)
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Adds a new element to the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Add(vValue)
If IsObject(vValue) Then
Set m_aData(UBound(m_aData)) = vValue
Else
m_aData(UBound(m_aData)) = vValue
End If
Add = UBound(m_aData)
ReDim Preserve m_aData(UBound(m_aData) + 1)
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Inserts a new element at a given position in the array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Insert(iPos, vValue)
If iPos > UBound(m_aData) Then
Call Add(vValue)
Exit Function
ElseIf iPos >= LBound(m_aData) Then
Dim iLoop
For iLoop = UBound(m_aData) To iPos + 1 Step -1
If IsObject(m_aData(iLoop - 1)) Then
Set m_aData(iLoop) = m_aData(iLoop - 1)
Else
m_aData(iLoop) = m_aData(iLoop - 1)
End If
Next
If IsObject(vValue) Then
Set m_aData(iPos) = vValue
Else
m_aData(iPos) = vValue
End If
Insert = UBound(m_aData)
ReDim Preserve m_aData(UBound(m_aData) + 1)
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Deletes an element from the array. Shrinks the array by 1.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub Delete(iPos)
If iPos < LBound(m_aData) Or iPos > UBound(m_aData) Then
Exit Sub
End If
Dim iLoop
For iLoop = iPos To UBound(m_aData) - 1
If IsObject(m_aData(iLoop + 1)) Then
Set m_aData(iLoop) = m_aData(iLoop + 1)
Else
m_aData(iLoop) = m_aData(iLoop + 1)
End If
Next
ReDim Preserve m_aData(UBound(m_aData) - 1)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Reinitializes the array. Removes any existing elements.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub Flush()
ReDim m_aData(0)
End Sub
End Class