github.com/gopherd/gonum@v0.0.4/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 "github.com/gopherd/gonum/graph"
     8  
     9  // Ints is a set of int identifiers.
    10  type Ints map[int]struct{}
    11  
    12  // The simple accessor methods for Ints are provided to allow ease of
    13  // implementation change should the need arise.
    14  
    15  // Add inserts an element into the set.
    16  func (s Ints) Add(e int) {
    17  	s[e] = struct{}{}
    18  }
    19  
    20  // Has reports the existence of the element in the set.
    21  func (s Ints) Has(e int) bool {
    22  	_, ok := s[e]
    23  	return ok
    24  }
    25  
    26  // Remove deletes the specified element from the set.
    27  func (s Ints) Remove(e int) {
    28  	delete(s, e)
    29  }
    30  
    31  // Count reports the number of elements stored in the set.
    32  func (s Ints) Count() int {
    33  	return len(s)
    34  }
    35  
    36  // IntsEqual reports set equality between the parameters. Sets are equal if
    37  // and only if they have the same elements.
    38  func IntsEqual(a, b Ints) bool {
    39  	if intsSame(a, b) {
    40  		return true
    41  	}
    42  
    43  	if len(a) != len(b) {
    44  		return false
    45  	}
    46  
    47  	for e := range a {
    48  		if _, ok := b[e]; !ok {
    49  			return false
    50  		}
    51  	}
    52  
    53  	return true
    54  }
    55  
    56  // Int64s is a set of int64 identifiers.
    57  type Int64s map[int64]struct{}
    58  
    59  // The simple accessor methods for Ints are provided to allow ease of
    60  // implementation change should the need arise.
    61  
    62  // Add inserts an element into the set.
    63  func (s Int64s) Add(e int64) {
    64  	s[e] = struct{}{}
    65  }
    66  
    67  // Has reports the existence of the element in the set.
    68  func (s Int64s) Has(e int64) bool {
    69  	_, ok := s[e]
    70  	return ok
    71  }
    72  
    73  // Remove deletes the specified element from the set.
    74  func (s Int64s) Remove(e int64) {
    75  	delete(s, e)
    76  }
    77  
    78  // Count reports the number of elements stored in the set.
    79  func (s Int64s) Count() int {
    80  	return len(s)
    81  }
    82  
    83  // Int64sEqual reports set equality between the parameters. Sets are equal if
    84  // and only if they have the same elements.
    85  func Int64sEqual(a, b Int64s) bool {
    86  	if int64sSame(a, b) {
    87  		return true
    88  	}
    89  
    90  	if len(a) != len(b) {
    91  		return false
    92  	}
    93  
    94  	for e := range a {
    95  		if _, ok := b[e]; !ok {
    96  			return false
    97  		}
    98  	}
    99  
   100  	return true
   101  }
   102  
   103  // Nodes is a set of nodes keyed in their integer identifiers.
   104  type Nodes map[int64]graph.Node
   105  
   106  // NewNodes returns a new Nodes.
   107  func NewNodes() Nodes {
   108  	return make(Nodes)
   109  }
   110  
   111  // NewNodes returns a new Nodes with the given size hint, n.
   112  func NewNodesSize(n int) Nodes {
   113  	return make(Nodes, n)
   114  }
   115  
   116  // The simple accessor methods for Nodes are provided to allow ease of
   117  // implementation change should the need arise.
   118  
   119  // Add inserts an element into the set.
   120  func (s Nodes) Add(n graph.Node) {
   121  	s[n.ID()] = n
   122  }
   123  
   124  // Remove deletes the specified element from the set.
   125  func (s Nodes) Remove(e graph.Node) {
   126  	delete(s, e.ID())
   127  }
   128  
   129  // Count returns the number of element in the set.
   130  func (s Nodes) Count() int {
   131  	return len(s)
   132  }
   133  
   134  // Has reports the existence of the elements in the set.
   135  func (s Nodes) Has(n graph.Node) bool {
   136  	_, ok := s[n.ID()]
   137  	return ok
   138  }
   139  
   140  // CloneNodes returns a clone of src.
   141  func CloneNodes(src Nodes) Nodes {
   142  	dst := make(Nodes, len(src))
   143  	for e, n := range src {
   144  		dst[e] = n
   145  	}
   146  	return dst
   147  }
   148  
   149  // Equal reports set equality between the parameters. Sets are equal if
   150  // and only if they have the same elements.
   151  func Equal(a, b Nodes) bool {
   152  	if same(a, b) {
   153  		return true
   154  	}
   155  
   156  	if len(a) != len(b) {
   157  		return false
   158  	}
   159  
   160  	for e := range a {
   161  		if _, ok := b[e]; !ok {
   162  			return false
   163  		}
   164  	}
   165  
   166  	return true
   167  }
   168  
   169  // UnionOfNodes returns the union of a and b.
   170  //
   171  // The union of two sets, a and b, is the set containing all the
   172  // elements of each, for instance:
   173  //
   174  //     {a,b,c} UNION {d,e,f} = {a,b,c,d,e,f}
   175  //
   176  // Since sets may not have repetition, unions of two sets that overlap
   177  // do not contain repeat elements, that is:
   178  //
   179  //     {a,b,c} UNION {b,c,d} = {a,b,c,d}
   180  //
   181  func UnionOfNodes(a, b Nodes) Nodes {
   182  	if same(a, b) {
   183  		return CloneNodes(a)
   184  	}
   185  
   186  	dst := make(Nodes)
   187  	for e, n := range a {
   188  		dst[e] = n
   189  	}
   190  	for e, n := range b {
   191  		dst[e] = n
   192  	}
   193  
   194  	return dst
   195  }
   196  
   197  // IntersectionOfNodes returns the intersection of a and b.
   198  //
   199  // The intersection of two sets, a and b, is the set containing all
   200  // the elements shared between the two sets, for instance:
   201  //
   202  //     {a,b,c} INTERSECT {b,c,d} = {b,c}
   203  //
   204  // The intersection between a set and itself is itself, and thus
   205  // effectively a copy operation:
   206  //
   207  //     {a,b,c} INTERSECT {a,b,c} = {a,b,c}
   208  //
   209  // The intersection between two sets that share no elements is the empty
   210  // set:
   211  //
   212  //     {a,b,c} INTERSECT {d,e,f} = {}
   213  //
   214  func IntersectionOfNodes(a, b Nodes) Nodes {
   215  	if same(a, b) {
   216  		return CloneNodes(a)
   217  	}
   218  	dst := make(Nodes)
   219  	if len(a) > len(b) {
   220  		a, b = b, a
   221  	}
   222  	for e, n := range a {
   223  		if _, ok := b[e]; ok {
   224  			dst[e] = n
   225  		}
   226  	}
   227  	return dst
   228  }