blob: 9dfecade4255c4e56343df5a6ef0e969ebcb06a3 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
2Open Source Initiative OSI - The MIT License (MIT):Licensing
3
4The MIT License (MIT)
5Copyright (c) 2013 Ralph Caraveo (deckarep@gmail.com)
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11of the Software, and to permit persons to whom the Software is furnished to do
12so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24*/
25
26package mapset
27
28// Iterator defines an iterator over a Set, its C channel can be used to range over the Set's
29// elements.
30type Iterator struct {
31 C <-chan interface{}
32 stop chan struct{}
33}
34
35// Stop stops the Iterator, no further elements will be received on C, C will be closed.
36func (i *Iterator) Stop() {
37 // Allows for Stop() to be called multiple times
38 // (close() panics when called on already closed channel)
39 defer func() {
40 recover()
41 }()
42
43 close(i.stop)
44
45 // Exhaust any remaining elements.
46 for range i.C {
47 }
48}
49
50// newIterator returns a new Iterator instance together with its item and stop channels.
51func newIterator() (*Iterator, chan<- interface{}, <-chan struct{}) {
52 itemChan := make(chan interface{})
53 stopChan := make(chan struct{})
54 return &Iterator{
55 C: itemChan,
56 stop: stopChan,
57 }, itemChan, stopChan
58}