github.com/anacrolix/torrent@v1.61.0/internal/indexed/table2.go (about)

     1  package indexed
     2  
     3  import (
     4  	"iter"
     5  
     6  	g "github.com/anacrolix/generics"
     7  )
     8  
     9  type Table2[K, V comparable] struct {
    10  	*Table[Pair[K, V]]
    11  }
    12  
    13  func (me *Table2[K, V]) Init(cmpFunc CompareFunc[Pair[K, V]]) {
    14  	g.InitNew(&me.Table)
    15  	me.Table.Init(cmpFunc)
    16  }
    17  
    18  func (me *Table2[K, V]) IterKeysFrom(start K) iter.Seq[K] {
    19  	return func(yield func(K) bool) {
    20  		for mr := range me.table.IterFrom(Pair[K, V]{Left: start}) {
    21  			if !yield(mr.Left) {
    22  				return
    23  			}
    24  		}
    25  	}
    26  }
    27  
    28  func Iter2[K, V any](me tableInterface[Pair[K, V]]) iter.Seq2[K, V] {
    29  	return func(yield func(K, V) bool) {
    30  		for r := range me.Iter {
    31  			if !yield(r.Left, r.Right) {
    32  				return
    33  			}
    34  		}
    35  	}
    36  }
    37  
    38  func IterFirst[K, V any](me tableInterface[Pair[K, V]]) iter.Seq[K] {
    39  	return func(yield func(K) bool) {
    40  		for first := range Iter2(me) {
    41  			if !yield(first) {
    42  				return
    43  			}
    44  		}
    45  	}
    46  }
    47  
    48  func IterSecond[K, V any](me tableInterface[Pair[K, V]]) iter.Seq[V] {
    49  	return func(yield func(V) bool) {
    50  		for _, second := range Iter2(me) {
    51  			if !yield(second) {
    52  				return
    53  			}
    54  		}
    55  	}
    56  }
    57  
    58  func (me *Table2[K, V]) Create(first K, second V) bool {
    59  	return me.table.Create(Pair[K, V]{first, second})
    60  }