gonum.org/v1/gonum@v0.15.1-0.20240517103525-f853624cb1bb/graph/internal/set/set.go (about)

     1  // Copyright ©2014 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package set
     6  
     7  import "gonum.org/v1/gonum/graph"
     8  
     9  type Int interface{ ~int | ~int64 }
    10  
    11  type Ints[T Int] map[T]struct{}
    12  
    13  // Add inserts an element into the set.
    14  func (s Ints[T]) Add(e T) {
    15  	s[e] = struct{}{}
    16  }
    17  
    18  // Has reports the existence of the element in the set.
    19  func (s Ints[T]) Has(e T) bool {
    20  	_, ok := s[e]
    21  	return ok
    22  }
    23  
    24  // Remove deletes the specified element from the set.
    25  func (s Ints[T]) Remove(e T) {
    26  	delete(s, e)
    27  }
    28  
    29  // Count reports the number of elements stored in the set.
    30  func (s Ints[T]) Count() int {
    31  	return len(s)
    32  }
    33  
    34  // IntsEqual reports set equality between the parameters. Sets are equal if
    35  // and only if they have the same elements.
    36  func IntsEqual[T Int](a, b Ints[T]) bool {
    37  	if intsSame(a, b) {
    38  		return true
    39  	}
    40  
    41  	if len(a) != len(b) {
    42  		return false
    43  	}
    44  
    45  	for e := range a {
    46  		if _, ok := b[e]; !ok {
    47  			return false
    48  		}
    49  	}
    50  
    51  	return true
    52  }
    53  
    54  // Nodes is a set of nodes keyed in their integer identifiers.
    55  type Nodes map[int64]graph.Node
    56  
    57  // NewNodes returns a new Nodes.
    58  func NewNodes() Nodes {
    59  	return make(Nodes)
    60  }
    61  
    62  // NewNodesSize returns a new Nodes with the given size hint, n.
    63  func NewNodesSize(n int) Nodes {
    64  	return make(Nodes, n)
    65  }
    66  
    67  // The simple accessor methods for Nodes are provided to allow ease of
    68  // implementation change should the need arise.
    69  
    70  // Add inserts an element into the set.
    71  func (s Nodes) Add(n graph.Node) {
    72  	s[n.ID()] = n
    73  }
    74  
    75  // Remove deletes the specified element from the set.
    76  func (s Nodes) Remove(e graph.Node) {
    77  	delete(s, e.ID())
    78  }
    79  
    80  // Count returns the number of element in the set.
    81  func (s Nodes) Count() int {
    82  	return len(s)
    83  }
    84  
    85  // Has reports the existence of the elements in the set.
    86  func (s Nodes) Has(n graph.Node) bool {
    87  	_, ok := s[n.ID()]
    88  	return ok
    89  }
    90  
    91  // CloneNodes returns a clone of src.
    92  func CloneNodes(src Nodes) Nodes {
    93  	dst := make(Nodes, len(src))
    94  	for e, n := range src {
    95  		dst[e] = n
    96  	}
    97  	return dst
    98  }
    99  
   100  // Equal reports set equality between the parameters. Sets are equal if
   101  // and only if they have the same elements.
   102  func Equal(a, b Nodes) bool {
   103  	if same(a, b) {
   104  		return true
   105  	}
   106  
   107  	if len(a) != len(b) {
   108  		return false
   109  	}
   110  
   111  	for e := range a {
   112  		if _, ok := b[e]; !ok {
   113  			return false
   114  		}
   115  	}
   116  
   117  	return true
   118  }
   119  
   120  // UnionOfNodes returns the union of a and b.
   121  //
   122  // The union of two sets, a and b, is the set containing all the
   123  // elements of each, for instance:
   124  //
   125  //	{a,b,c} UNION {d,e,f} = {a,b,c,d,e,f}
   126  //
   127  // Since sets may not have repetition, unions of two sets that overlap
   128  // do not contain repeat elements, that is:
   129  //
   130  //	{a,b,c} UNION {b,c,d} = {a,b,c,d}
   131  func UnionOfNodes(a, b Nodes) Nodes {
   132  	if same(a, b) {
   133  		return CloneNodes(a)
   134  	}
   135  
   136  	dst := make(Nodes)
   137  	for e, n := range a {
   138  		dst[e] = n
   139  	}
   140  	for e, n := range b {
   141  		dst[e] = n
   142  	}
   143  
   144  	return dst
   145  }
   146  
   147  // IntersectionOfNodes returns the intersection of a and b.
   148  //
   149  // The intersection of two sets, a and b, is the set containing all
   150  // the elements shared between the two sets, for instance:
   151  //
   152  //	{a,b,c} INTERSECT {b,c,d} = {b,c}
   153  //
   154  // The intersection between a set and itself is itself, and thus
   155  // effectively a copy operation:
   156  //
   157  //	{a,b,c} INTERSECT {a,b,c} = {a,b,c}
   158  //
   159  // The intersection between two sets that share no elements is the empty
   160  // set:
   161  //
   162  //	{a,b,c} INTERSECT {d,e,f} = {}
   163  func IntersectionOfNodes(a, b Nodes) Nodes {
   164  	if same(a, b) {
   165  		return CloneNodes(a)
   166  	}
   167  	dst := make(Nodes)
   168  	if len(a) > len(b) {
   169  		a, b = b, a
   170  	}
   171  	for e, n := range a {
   172  		if _, ok := b[e]; ok {
   173  			dst[e] = n
   174  		}
   175  	}
   176  	return dst
   177  }