1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Pipelined filters and operations on iterators.
17 # This module enhance `Iterator`s with some methods that enable a
18 # pipeline-like programing that offers the manupulation of
19 # collections trough connected filters with reasonable memory constraints.
22 redef interface Iterator[E
]
23 # Filter: sort with `default_comparator`.
24 # SEE: `sort_with` for details
25 # REQUIRE: self isa Iterator[Comparable]
27 # assert [1,3,2].iterator.sort.to_a == [1,2,3]
30 assert self isa Iterator[Comparable]
32 default_comparator
.sort
(a
)
36 # Filter: sort with a given `comparator`.
37 # Important: require O(n) memory.
38 fun sort_with
(comparator
: Comparator[E
]): Iterator[E
]
45 # Filter: reject duplicates.
46 # Elements already seen are rejected.
48 # Important: rely on `==` and `hash`
49 # Important: require O(m) in memory, where m is the total number of uniq items.
51 # assert [1,2,1,1,1,3,2].iterator.uniq.to_a == [1,2,3]
53 # REQUIRE: self isa Iterator[Object]
56 assert self isa Iterator[Object]
57 return new PipeUniq[E
](self)
60 # Filter: reject continuous sequences of duplicates
62 # Important: rely on `==`.
64 # assert [1,2,1,1,1,3,2].iterator.seq_uniq.to_a == [1,2,1,3,2]
65 fun seq_uniq
: Iterator[E
]
67 return new PipeSeqUniq[E
](self)
70 # Combine two iterators.
72 # When the first iterator is terminated, the second is started.
74 # assert ([1..20[.iterator + [20..40[.iterator).to_a == ([1..40[).to_a
75 fun +(other
: Iterator[E
]): Iterator[E
]
77 return new PipeJoin[E
](self, other
)
80 # Alternate each item with `e`.
82 # assert [1,2,3].iterator.alternate(0).to_a == [1,0,2,0,3]
83 fun alternate
(e
: E
): Iterator[E
]
85 return new PipeAlternate[E
](self, e
)
88 # Filter: reject a given `item`.
90 # assert [1,1,2,1,3].iterator.skip(1).to_a == [2,3]
91 fun skip
(item
: E
): Iterator[E
]
93 return new PipeSkip[E
](self, item
)
96 # Filter: keep only the first `length` items.
98 # This filter does not always consume `self'.
100 # var i = [1,2,3,4,5].iterator
101 # assert i.head(2).to_a == [1,2]
103 fun head
(length
: Int): Iterator[E
]
105 return new PipeHead[E
](self, length
)
108 # Filter: reject the first `length` items.
110 # assert [1,2,3,4,5].iterator.skip_head(2).to_a == [3,4,5]
112 # ENSURE: self == return
113 fun skip_head
(length
: Int): Iterator[E
]
115 while length
> 0 and self.is_ok
do
122 # Filter: keep only the last `length` items.
124 # assert [1,2,3,4,5].iterator.tail(2).to_a == [4,5]
126 # Important: require O(length) in memory
127 fun tail
(length
: Int): Iterator[E
]
129 var lasts
= new List[E
]
131 while lasts
.length
>= length
do lasts
.shift
132 lasts
.push
(self.item
)
135 return lasts
.iterator
138 # Filter: reject the last `length` items.
140 # assert [1,2,3,4,5].iterator.skip_tail(2).to_a == [1,2,3]
142 # Important: require O(length) in memory
143 fun skip_tail
(length
: Int): Iterator[E
]
145 return new PipeSkipTail[E
](self, length
)
149 ### Specific private iterator classes
151 private class PipeUniq[E
]
154 var source
: Iterator[E
]
156 var seen
= new HashSet[Object] # FIXME HashSet[E]
158 redef fun is_ok
do return source
.is_ok
160 redef fun item
do return source
.item
164 self.seen
.add
(self.item
.as(Object))
166 while source
.is_ok
and self.seen
.has
(source
.item
.as(Object)) do
172 private class PipeSeqUniq[E
]
175 var source
: Iterator[E
]
177 redef fun is_ok
do return source
.is_ok
179 redef fun item
do return source
.item
185 while source
.is_ok
and seen
== source
.item
do
191 private class PipeJoin[E
]
193 var source1
: Iterator[E
]
194 var source2
: Iterator[E
]
198 return source1
.is_ok
or source2
.is_ok
203 if source1
.is_ok
then return source1
.item
else return source2
.item
208 if source1
.is_ok
then source1
.next
else source2
.next
212 private class PipeAlternate[E
]
215 var source
: Iterator[E
]
219 redef fun is_ok
do return source
.is_ok
239 private class PipeSkip[E
]
242 var source
: Iterator[E
]
245 init(source
: Iterator[E
], skip_item
: E
)
248 self.skip_item
= skip_item
255 while source
.is_ok
and source
.item
== skip_item
do source
.next
258 redef fun is_ok
do return source
.is_ok
260 redef fun item
do return source
.item
269 private class PipeHead[E
]
272 var source
: Iterator[E
]
276 redef fun is_ok
do return length
> 0 and source
.is_ok
278 redef fun item
do return source
.item
287 private class PipeSkipTail[E
]
290 var source
: Iterator[E
]
294 var lasts
= new List[E
]
296 init(source
: Iterator[E
], length
: Int)
300 var lasts
= self.lasts
301 while source
.is_ok
and lasts
.length
< length
do
302 lasts
.push
(source
.item
)
307 redef fun is_ok
do return source
.is_ok
309 redef fun item
do return lasts
.first
314 lasts
.push
(source
.item
)