-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStableArray_sample.lua
More file actions
272 lines (200 loc) · 5.4 KB
/
StableArray_sample.lua
File metadata and controls
272 lines (200 loc) · 5.4 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
--- Sample code for StableArray plugin.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
local StableArray = require("plugin.StableArray").New
local sa = StableArray()
local i1 = sa:Insert("bob")
local i2 = sa:Insert(-73) -- Integers (not ideal, but possible)
local i3 = sa:Insert{} -- Table
local i4 = sa:Insert(newproxy()) -- Userdata
local i5 = sa:Insert(coroutine.create(function() end)) -- Coroutine
local i6 = sa:Insert(37) -- Another integer
local i7 = sa:Insert(0 / 0) -- Handles NaN
local i8 = sa:Insert(nil) -- Handles nil
local t = {}
local i9 = sa:Insert(t)
print("Examining array, using raw iteration (index, element, type)...")
for i = 1, #sa do
print(i, sa:Get(i), type(sa:Get(i)))
end
print("")
print("...and using iterator (index, element)")
for i, elem in sa:Ipairs() do
print(i, elem)
end
print("")
print("Removing element at index 2")
sa:RemoveAt(2)
print("")
print("Raw iteration (sans type)...")
for i = 1, #sa do
print(i, sa:Get(i))
end
print("")
print("...and using iterator")
for i, elem in sa:Ipairs() do
print(i, elem)
end
print("")
print("Found t at index:", sa:Find(t))
print("Found nil at index:", sa:Find(nil)) -- possible to search for nil
print("Did not find \"BLARGH\", alas:", sa:Find("BLARGH"))
print("Found NaN at index:", sa:Find(0 / 0)) -- possible to search for NaN (not different ones, though)
print("And \"bob\"? What about him?", sa:Find("bob"))
print("")
print("Trying to remove -73 (already gone)")
sa:FindAndRemove(-73)
print("")
print("Raw iteration...")
for i = 1, #sa do
print(i, sa:Get(i))
end
print("")
print("...and iterator")
for i, elem in sa:Ipairs() do
print(i, elem)
end
print("")
print("Examining items in use (index, used?)")
for i = 1, #sa do
print(i, sa:InUse(i))
end
print("")
print("Updating elements")
print("Failure, updating unused slot:", sa:Update(2, {}))
print("Failure, updating too-high slot:", sa:Update(1000, true))
print("Updating slot 5 (what was old value?):", sa:Update(5, "New result!"))
print("")
print("Using iterator")
for i, elem in sa:Ipairs() do
print(i, elem)
end
print("")
print("Out-of-bounds accesses")
print("Index too low:", sa:Get(-3))
print("Index too high:", sa:Get(#sa + 27))
print("???", sa[-3])
print("????", sa[#sa + 27])
print("?!", sa[3])
print("")
print("Adding another entry (\"dog\") at:", sa:Insert("dog"))
print("And one more (false) at:", sa:Insert(false))
print("")
print("Raw iteration")
for i, elem in sa:Ipairs() do
print(i, elem)
end
print("")
print("Removing 37")
sa:FindAndRemove(37)
print("")
print("Using iterator")
for i, elem in sa:Ipairs() do
print(i, elem)
end
print("")
print("Removing item at index i4")
sa:RemoveAt(i4)
print("")
print("Using iterator")
for i, elem in sa:Ipairs() do
print(i, elem)
end
print("")
print("Serializing array")
local NULL, NIL = {}, {} -- Make two nonces to substitute for unused and nil slots
local arr = sa:GetArray(NULL, NIL)
print("")
print("Null, nil:", NULL, NIL)
print("Contents:")
for i = 1, #sa do
print(i, arr[i])
end
print("")
print("Clearing array")
sa:Clear()
print("")
print("Anything to iterate?")
print("")
for i, elem in sa:Ipairs() do
print("STILL HERE?!", i, elem)
end
print("Deserializing array")
sa:SetArray(arr, NULL, NIL)
print("")
print("Using iterator")
for i, elem in sa:Ipairs() do
print(i, elem)
end
-- Can iterate while removing current element
do
local sa = StableArray()
print("")
print("Adding several elements to new array")
for i = 1, 10 do
sa:Insert(i * 1.3)
end
for i = 1, #sa do
print(sa:Get(i))
end
print("")
print("While iterating...")
for i, elem in sa:Ipairs() do
if i % 2 == 0 then
print("Removing even element", sa:Get(i))
sa:RemoveAt(i)
end
end
print("")
print("Contents now?")
for i, elem in sa:Ipairs() do
print(i, elem)
end
end
-- Can iterate while removing elements before or ahead
do
local sa = StableArray()
print("")
print("Adding several elements to another new array")
for i = 1, 10 do
sa:Insert(i * .7)
end
for i = 1, #sa do
print(sa:Get(i))
end
print("")
print("While iterating...")
for i, elem in sa:Ipairs() do
local index = #sa - i
print("Visiting", i, elem)
if index % 3 == 0 and sa:InUse(index) then
print("Removing element", index, sa:Get(index))
sa:RemoveAt(index)
end
end
print("")
print("Contents now?")
for i, elem in sa:Ipairs() do
print(i, elem)
end
end