github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/keyset/keyset_internal.go (about)

     1  // Copyright 2020 Insolar Network Ltd.
     2  // All rights reserved.
     3  // This material is licensed under the Insolar License version 1.0,
     4  // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
     5  
     6  package keyset
     7  
     8  type internalKeySet interface {
     9  	KeyList
    10  	enumRawKeys(exclusive bool, fn func(k Key, exclusive bool) bool) bool
    11  	copy(n int) basicKeySet
    12  }
    13  
    14  // read-only access only
    15  type copyKeySet interface {
    16  	KeySet
    17  	copy(n int) basicKeySet
    18  }
    19  
    20  // mutable access
    21  type mutableKeySet interface {
    22  	copyKeySet
    23  	retainAll(ks KeySet) mutableKeySet
    24  	removeAll(ks KeySet) mutableKeySet
    25  	addAll(ks KeySet) mutableKeySet
    26  	removeKeys(k []Key)
    27  	addKeys(k []Key)
    28  	remove(k Key)
    29  	add(k Key)
    30  }
    31  
    32  var _ internalKeySet = listSet{}
    33  
    34  type listSet struct {
    35  	KeyList
    36  }
    37  
    38  func (v listSet) enumRawKeys(exclusive bool, fn func(k Key, exclusive bool) bool) bool {
    39  	return v.KeyList.EnumKeys(func(k Key) bool {
    40  		return fn(k, exclusive)
    41  	})
    42  }
    43  
    44  func (v listSet) copy(n int) basicKeySet {
    45  	if nn := v.Count(); n < nn {
    46  		n = nn
    47  	}
    48  	if n == 0 {
    49  		return nil
    50  	}
    51  	r := make(basicKeySet, n)
    52  	v.KeyList.EnumKeys(func(k Key) bool {
    53  		r.add(k)
    54  		return false
    55  	})
    56  	return r
    57  }
    58  
    59  func (v basicKeySet) copy(n int) basicKeySet {
    60  	if nn := len(v); n < nn {
    61  		n = nn
    62  	}
    63  	if n == 0 {
    64  		return nil
    65  	}
    66  	r := make(basicKeySet, n)
    67  	for k := range v {
    68  		r[k] = struct{}{}
    69  	}
    70  	return r
    71  }