Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | Open Source Initiative OSI - The MIT License (MIT):Licensing |
| 3 | |
| 4 | The MIT License (MIT) |
| 5 | Copyright (c) 2013 Ralph Caraveo (deckarep@gmail.com) |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 | this software and associated documentation files (the "Software"), to deal in |
| 9 | the Software without restriction, including without limitation the rights to |
| 10 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 | of the Software, and to permit persons to whom the Software is furnished to do |
| 12 | so, subject to the following conditions: |
| 13 | |
| 14 | The above copyright notice and this permission notice shall be included in all |
| 15 | copies or substantial portions of the Software. |
| 16 | |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | package mapset |
| 27 | |
| 28 | // Iterator defines an iterator over a Set, its C channel can be used to range over the Set's |
| 29 | // elements. |
| 30 | type 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. |
| 36 | func (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. |
| 51 | func 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 | } |