github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/ident/iterator.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package ident 22 23 // NewIDsIterator returns a new Iterator over the given IDs. 24 func NewIDsIterator(ids ...ID) Iterator { 25 return NewIDSliceIterator(ids) 26 } 27 28 // NewIDSliceIterator returns a new Iterator over a slice. 29 func NewIDSliceIterator(ids []ID) Iterator { 30 iter := &idSliceIter{ 31 backingSlice: ids, 32 currentIdx: -1, 33 } 34 return iter 35 } 36 37 type idSliceIter struct { 38 backingSlice []ID 39 currentIdx int 40 currentID ID 41 } 42 43 func (i *idSliceIter) Next() bool { 44 i.currentIdx++ 45 if i.currentIdx < len(i.backingSlice) { 46 i.currentID = i.backingSlice[i.currentIdx] 47 return true 48 } 49 i.currentID = nil 50 return false 51 } 52 53 func (i *idSliceIter) Current() ID { 54 return i.currentID 55 } 56 57 func (i *idSliceIter) CurrentIndex() int { 58 if i.currentIdx >= 0 { 59 return i.currentIdx 60 } 61 return 0 62 } 63 64 func (i *idSliceIter) Err() error { 65 return nil 66 } 67 68 func (i *idSliceIter) Close() { 69 i.backingSlice = nil 70 i.currentIdx = 0 71 i.currentID = nil 72 } 73 74 func (i *idSliceIter) Len() int { 75 return len(i.backingSlice) 76 } 77 78 func (i *idSliceIter) Remaining() int { 79 if r := len(i.backingSlice) - 1 - i.currentIdx; r >= 0 { 80 return r 81 } 82 return 0 83 } 84 85 func (i *idSliceIter) Duplicate() Iterator { 86 return &idSliceIter{ 87 backingSlice: i.backingSlice, 88 currentIdx: i.currentIdx, 89 currentID: i.currentID, 90 } 91 } 92 93 // NewStringIDsIterator returns a new Iterator over the given IDs. 94 func NewStringIDsIterator(ids ...string) Iterator { 95 return NewStringIDsSliceIterator(ids) 96 } 97 98 // NewStringIDsSliceIterator returns a new Iterator over a slice of strings. 99 func NewStringIDsSliceIterator(ids []string) Iterator { 100 iter := &stringSliceIter{ 101 backingSlice: ids, 102 currentIdx: -1, 103 } 104 return iter 105 } 106 107 type stringSliceIter struct { 108 backingSlice []string 109 currentIdx int 110 currentID ID 111 } 112 113 func (i *stringSliceIter) Next() bool { 114 i.currentIdx++ 115 if i.currentIdx < len(i.backingSlice) { 116 i.currentID = StringID(i.backingSlice[i.currentIdx]) 117 return true 118 } 119 i.currentID = nil 120 return false 121 } 122 123 func (i *stringSliceIter) Current() ID { 124 return i.currentID 125 } 126 127 func (i *stringSliceIter) CurrentIndex() int { 128 if i.currentIdx >= 0 { 129 return i.currentIdx 130 } 131 return 0 132 } 133 134 func (i *stringSliceIter) Err() error { 135 return nil 136 } 137 138 func (i *stringSliceIter) Close() { 139 i.backingSlice = nil 140 i.currentIdx = 0 141 i.currentID = nil 142 } 143 144 func (i *stringSliceIter) Len() int { 145 return len(i.backingSlice) 146 } 147 148 func (i *stringSliceIter) Remaining() int { 149 if r := len(i.backingSlice) - 1 - i.currentIdx; r >= 0 { 150 return r 151 } 152 return 0 153 } 154 155 func (i *stringSliceIter) Duplicate() Iterator { 156 return &stringSliceIter{ 157 backingSlice: i.backingSlice, 158 currentIdx: i.currentIdx, 159 currentID: i.currentID, 160 } 161 }