-
Notifications
You must be signed in to change notification settings - Fork 1
/
collections.rid
197 lines (156 loc) · 4.09 KB
/
collections.rid
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
# This example shows standard collections
# with their methods
print '--- Str ---'
# Str is the most simple collection
# It gathers only characters :
str = 'Hello'
print str
# We can access an item of a collection with
# its index (Riddim is 0 based)
print 'First char :', str[0]
print 'Last char :', str[-1]
# Str supports slices
print '2nd to 4th char (included - excluded) :', str[1 -> 5]
print str[1 ->= 5], str[0 -> 42 .. 2], str[len(str) - 1 ->= 0 .. -1]
# Str provides chr and ord like in Python
print Str.chr(0x41)
print Str.ord('A')
print 'Hello' + ' ' + 'world'
print ()
print '--- Vec ---'
# Vec is a dynamic array, all items are stored
# in a contiguous memory space
vec = [1, 2, 3, 5]
print vec
# Add / pop (push back / pop back)
vec.add(8)
print vec
# Returns the removed item
print vec.pop()
print vec
vec.pop(0)
print vec
vec[1 -> 3] = 42
print vec
# Tuple like binding
vec = ['A', 'B']
let a, b = vec
print a, b
# It is possible to iterate through any collection
vec = [1, 2, 3]
mysum = 0
for item in vec {
mysum += item
}
print 'mysum:', mysum
print [1, 2] + [3, 4]
print ()
print '--- HashMap ---'
# Maps provide a mapping between keys and values
mymap = {1: 2, 'a': 'b', [0]: [1]}
print mymap
print mymap[1]
print mymap.pop('a')
print ()
print '--- TreeMap ---'
# This map contains only comparable keys
# (usually of the same type)
# All keys are sorted
mymap = TreeMap({1: 2, 4: -8, 8: 64, -16: -2})
# Iterate with structured binding
for let key, value in mymap {
print key, '->', value
}
print ()
print '--- HashSet ---'
# Sets are just maps with dummy keys internally
# HashSet is like an HashMap but some methods / slots
# are removed
set = HashSet([1, 2, 1, 4, 'Hello', 'Hallo', 'Hallo'])
print set
print 'Is Hello in this set ?', 'Hello' in set
print 'Is Hullo in this set ?', 'Hullo' in set
print ()
print '--- TreeSet ---'
# Like TreeMap, items are ordered
set = TreeSet([1, 2, 1, 4])
for e in set {
print '-', e
}
print ()
print '--- Deque ---'
# A Deque (double ended queue) is like a Vec but is optimized to
# add / pop items also in the front of the collection
q = Deque([1, 2, 3])
# Push back
q.add(4)
q.add(5)
# Pop back
print q.pop()
# Push front
q.add_front(0)
q.add_front(-1)
# Pop front
print q.pop_front()
print q
print ()
print '--- SegTree ---'
# A Segment Tree is useful to query function
# applications over a data range like the
# sum / min / max function...
# Optionally, you can add the init value (0 by default for sums etc.)
seg = SegTree([1, 3, 4, 2, 3, 4], sum)
print 'Sum SegTree :'
print seg
# Every operation takes O(log N) time
print seg.query(0, 2) # 1 + 3 = 4
print seg.query(3, 6) # 2 + 3 + 4 = 9
print seg.query(0, 0) # 0
seg[3] = -1
print seg # [1, 3, 4, -1, 3, 4]
print seg.query(3, 6) # -1 + 3 + 4 = 6
print 'Min SegTree :'
# inf is used to avoid having 0 at each query
seg = SegTree([1, 3, 4, 2], min, inf)
print seg.query(0, len(seg)) # 1
print ()
print '--- Methods ---'
# Default collections provides multiple methods
str = 'Hey'
vec = [1, 2, 3, 4, 3, 2, 1]
set = TreeSet(vec)
# Size
print 'len:', len(vec)
print 'empty:', vec.empty(), [].empty(), vec.not_empty()
# Searching
print 'index:', vec.index(3), vec.index(0), vec.last_index(3)
# If a is sorted, binary search can be done
a = [1, 3, 4, 4, 8, 9]
print 'bin_search:', a.bin_search(1), a.bin_search(4), a.bin_search(42)
# Functional
# (print) refers to the print method (not the macro keyword)
myprint = (print)
set.for_each(myprint)
sorted = []
set.for_each(sorted.add)
print 'sorted:', sorted
print 'map:', vec.map(|x| 2 * x)
print 'filter:', vec.filter(|x| x >= 3)
print 'reduce:', vec.reduce(|a, b| a + b, init: 0), vec.reduce(|a, b| a * b, init: 1)
print 'chain:', vec.map(|x| -x).filter(|x| x <= -3)
# This is done inplace
vec.sort()
print 'sort:', vec
# Remember that str is a collection
str = 'Hello'
str.sort()
print 'sort:', str
# The sum can be done with various types
a = [1, 2, 3]
print 'sum:', a.sum()
a = ['h', 'e', 'l', 'l', 'o']
print 'sum:', a.sum()
a = [[1, 2], [3, 4]]
print 'sum:', a.sum()
# Note that every functional method has its corresponding function
print map(1 ->= 10, |x| 2 * x)