summaryrefslogtreecommitdiff
path: root/libtnsl/box/iterator.tnsl
blob: 1ccdd534f40348eaaa1704f4fb586b53ed47d1d2 (plain)
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
/##
	Copyright 2021-2022 Kyle Gunger

	Dual licensed under the CDDL 1.0 and BSD 3-Clause licenses.

	This file may only be used in accordance with one of the two
	licenses.  You should have received a copy of each license with
	the source code.  In the event that you did not recieve a copy
	of the licenses, they may be found at the following URLs:

	CDDL 1.0:
	https://opensource.org/licenses/CDDL-1.0

	BSD 3-Clause:
	https://opensource.org/licenses/BSD-3-Clause

	THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO
	WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE
	EXPRESS OR IMPLIED
#/

# Interface for moving through a list
/; interface Iterator (type T)

	# Advance to the next element in the list
	/; next [bool] ;/

	# Get the current element in the list
	/; current [T] ;/

	# Seek to the end of the iterator
	# Returns true if this was successfull
	/; end [bool] ;/
;/

# Interface for moving through a list forwards or backwards
/; interface DoubleIterator (type T) extends Iterator(T)

	# Move to the previous element in the list
	/; prev [bool] ;/

	# Seek to the start of the iterator
	# Returns true if this was successfull
	/; start [bool] ;/
;/

# Iterate over an array
;raw struct ArrayIterator (type T) extends DoubleIterator(T) {
	uint place,
	~{}T data
}

/; method ArrayIterator
	/; ArrayIterator (~{}T arr)
		;self.place = 0
		;self.data = arr
	;/

	/; override next [bool]
		/; if (self.place + 1 >= len self.data`)
			;self.place = len self.data`
			;return false
		;/
		;self.place++
		;return true
	;/

	/; override current [T]
		;return self.data`{self.place}
	;/

	/; override prev [bool]
		/; if (self.place == 0)
			;return false
		;/
		;self.place--
		;return true
	;/

	/; override end [bool]
		;self.place = len self.data`
		;return true
	;/

	/; override start [bool]
		;self.place = 0
		;return true
	;/
;/

# Iterate over a pointer
;raw struct PointerIterator (type T) extends DoubleIterator(T) {
	uint
		place,
		length,
	
	~T data
}

/; method PointerIterator
	/; PointerIterator (~T ptr, uint length)
		;self.place = 0
		;self.length = length
		;self.data = ptr
	;/

	/; override next [bool]
		/; if (self.place + 1 >= self.length)
			;self.place = self.length
			;return false
		;/
		;self.place++
		;return true
	;/

	/; override current [T]
		;return self.data{self.place}
	;/

	/; override prev [bool]
		/; if (self.place == 0)
			;return false
		;/
		;self.place--
		;return true
	;/

	/; override end [bool]
		;self.place = self.length
		;return true
	;/

	/; override start [bool]
		;self.place = 0
		;return true
	;/
;/

;struct ReverseIterator (type T) extends DoubleIterator(T) {
	DoubleIterator(T) it
}

/; method ReverseIterator
	/; ReverseIterator (DoubleIterator(T) it)
		;self.it = it
		;self.it.end()
	;/

	/; override next [bool]
		;return self.it.prev()
	;/

	/; override current [T]
		;return self.it.current()
	;/

	/; override prev [bool]
		;return self.it.next()
	;/

	/; override end [bool]
		;return self.it.start()
	;/

	/; override start [bool]
		;return self.it.end()
	;/
;/