/
Array.boo
executable file
·229 lines (170 loc) · 5.49 KB
/
Array.boo
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
namespace UnityScript.Lang
import System
import System.Collections
class Array(CollectionBase, Boo.Lang.Runtime.ICoercible):
def constructor():
pass
def constructor([required(capacity >= 0)] capacity as int):
EnsureCapacity(capacity)
def constructor(collection as IEnumerable):
if collection isa String:
Add(collection)
else:
AddRange(collection)
def constructor(*items):
if len(items) == 1 and items[0] isa IEnumerable:
AddRange(items[0])
else:
AddRange(items)
static def op_Implicit(e as IEnumerable) as Array:
if e is null: return null
return Array(e)
static def op_Implicit(a as System.Array) as Array:
if a is null: return null
return Array((a as IEnumerable))
length:
get:
return self.Count
set:
EnsureCapacity(value)
if value < self.Count:
self.InnerList.RemoveRange(value, InnerList.Count - value)
def clear():
Clear()
def Coerce(toType as System.Type):
if toType.IsArray: return ToBuiltin(toType.GetElementType())
return self
def ToBuiltin(type as System.Type):
return self.InnerList.ToArray(type)
def Add(value as object, *items):
self.AddImpl(value, items)
def Add(value as object):
self.InnerList.Add(value)
def Push(value as object, *items):
return self.AddImpl(value, items)
def push(value as object, *items):
return self.AddImpl(value, items)
// workaround for mono reflection bug
def push(value as object):
self.InnerList.Add(value)
return self.InnerList.Count
def Push(value as object):
return self.push(value)
def Unshift(value as object, *items):
return self.UnshiftImpl(value, items)
def unshift(value as object, *items):
return self.UnshiftImpl(value, items)
def Splice (index as int, howmany as int, *items):
SpliceImpl (index, howmany, items)
def splice (index as int, howmany as int, *items):
SpliceImpl (index, howmany, items)
# How do i allow the vararg only for ICollections?
def Concat(value as ICollection, *items):
return ConcatImpl(value, items)
# How do i allow the vararg only for ICollections?
def concat(value as ICollection, *items):
return ConcatImpl(value, items)
# Pop last and return
def Pop():
last = self.InnerList.Count - 1
popped = self.InnerList[last]
self.InnerList.RemoveAt(last)
return popped
def pop():
return self.Pop()
# Remove first element
def Shift():
first = 0
popped = self.InnerList[first]
self.InnerList.RemoveAt(0)
return popped
def shift():
return self.Shift()
# Convert to string
def ToString ():
return self.Join(",")
def toString ():
return ToString()
# Returns array with elements removed
def Slice(start as int, end as int):
normalStart = NormalizeIndex(start)
normalEnd = NormalizeIndex(end)
return Array(InnerList.GetRange(normalStart, normalEnd - normalStart))
def Slice(start as int):
return self.Slice(start, self.InnerList.Count)
def slice(start as int, end as int):
return self.Slice(start, end)
def slice(start as int):
return self.Slice(start)
# Revert
def Reverse() as Array:
self.InnerList.Reverse()
return self
def reverse() as Array:
self.Reverse()
return self
callable Comparison(lhs as object, rhs as object) as int
private class ComparisonComparer(IComparer):
def constructor(comparison as Comparison):
_comparison = comparison
def Compare(lhs, rhs):
return _comparison(lhs, rhs)
_comparison as Comparison
def Sort(comparison as Comparison):
self.InnerList.Sort(ComparisonComparer(comparison))
def sort(comparison as Comparison):
Sort(comparison)
# Sort
def Sort() as Array:
self.InnerList.Sort()
return self
def sort() as Array:
self.Sort()
return self
# Combine to a string
def Join (seperator as String):
return Builtins.join(self.InnerList, seperator)
def join (seperator as String):
return Builtins.join(self.InnerList, seperator)
# Remove
def Remove (obj as Object):
self.InnerList.Remove(obj)
def remove (obj as Object):
self.Remove(obj)
self[index as int] as object:
get:
return self.InnerList[index]
set:
EnsureCapacity(index+1)
self.InnerList[index] = value
def AddRange([required] collection as IEnumerable):
for item in collection:
self.InnerList.Add(item)
override protected def OnValidate(newValue):
pass // anything goes
private def AddImpl(value as object, [required] items as IEnumerable):
self.InnerList.Add(value)
for item in items:
self.InnerList.Add(item)
return self.InnerList.Count
private def UnshiftImpl(value as object, [required] items as IEnumerable):
self.InnerList.InsertRange(0, items)
self.InnerList.Insert(0, value)
return self.InnerList.Count
private def SpliceImpl (index as int, howmany as int, [required] items as IEnumerable):
if howmany != 0:
self.InnerList.RemoveRange(index, howmany)
self.InnerList.InsertRange(index, items)
private def ConcatImpl(value as ICollection, [required] items as IEnumerable):
arr = Array (self.InnerList)
arr.InnerList.AddRange(value)
for item as ICollection in items:
arr.InnerList.AddRange(item)
return arr
private def EnsureCapacity(capacity as int):
return if capacity < Count
for i in range(capacity-self.Count):
InnerList.Add(null)
private def NormalizeIndex(index as int):
return index if index >= 0
return index + InnerList.Count