|
| 1 | +package docsfilter |
| 2 | + |
| 3 | +type TombstonesIterator interface { |
| 4 | + Next() (uint32, bool) |
| 5 | +} |
| 6 | + |
| 7 | +func NewNMergedIterators(iterators []TombstonesIterator) TombstonesIterator { |
| 8 | + if len(iterators) == 0 { |
| 9 | + return &EmptyIterator{} |
| 10 | + } |
| 11 | + |
| 12 | + if len(iterators) == 1 { |
| 13 | + return iterators[0] |
| 14 | + } |
| 15 | + |
| 16 | + merged := NewMergedIterator(iterators[0], iterators[1]) |
| 17 | + for _, s := range iterators[2:] { |
| 18 | + merged = NewMergedIterator(merged, s) |
| 19 | + } |
| 20 | + return merged |
| 21 | +} |
| 22 | + |
| 23 | +type MergedIterator struct { |
| 24 | + a, b TombstonesIterator |
| 25 | + curA, curB uint32 |
| 26 | + init bool |
| 27 | +} |
| 28 | + |
| 29 | +func NewMergedIterator( |
| 30 | + a, b TombstonesIterator, |
| 31 | +) TombstonesIterator { |
| 32 | + return &MergedIterator{ |
| 33 | + a: a, |
| 34 | + b: b, |
| 35 | + init: false, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (it *MergedIterator) Next() (uint32, bool) { |
| 40 | + if !it.init { |
| 41 | + it.readA() |
| 42 | + it.readB() |
| 43 | + it.init = true |
| 44 | + } |
| 45 | + |
| 46 | + if it.a == nil && it.b == nil { |
| 47 | + return 0, false |
| 48 | + } |
| 49 | + if it.a == nil { |
| 50 | + return it.readB(), true |
| 51 | + } |
| 52 | + if it.b == nil { |
| 53 | + return it.readA(), true |
| 54 | + } |
| 55 | + |
| 56 | + if it.curA == it.curB { |
| 57 | + it.readA() // skip duplicate |
| 58 | + if it.a == nil { |
| 59 | + return it.readB(), true |
| 60 | + } |
| 61 | + } |
| 62 | + if it.curB < it.curA { |
| 63 | + return it.readB(), true |
| 64 | + } |
| 65 | + return it.readA(), true |
| 66 | +} |
| 67 | + |
| 68 | +func (it *MergedIterator) readA() uint32 { |
| 69 | + var has bool |
| 70 | + current := it.curA |
| 71 | + |
| 72 | + if it.curA, has = it.a.Next(); !has { |
| 73 | + it.a = nil // stop reading a |
| 74 | + } |
| 75 | + |
| 76 | + return current |
| 77 | +} |
| 78 | + |
| 79 | +func (it *MergedIterator) readB() uint32 { |
| 80 | + var has bool |
| 81 | + current := it.curB |
| 82 | + |
| 83 | + if it.curB, has = it.b.Next(); !has { |
| 84 | + it.b = nil // stop reading b |
| 85 | + } |
| 86 | + |
| 87 | + return current |
| 88 | +} |
| 89 | + |
| 90 | +type EmptyIterator struct{} |
| 91 | + |
| 92 | +func (it *EmptyIterator) Next() (uint32, bool) { |
| 93 | + return 0, false |
| 94 | +} |
0 commit comments