int64.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package sets
  2. import (
  3. "reflect"
  4. "sort"
  5. )
  6. // Int64 is sets.Int64 is a set of int64s, implemented via map[int64]struct{} for minimal memory consumption.
  7. type Int64 map[int64]Empty
  8. // NewInt64 creates a Int64 from a list of values.
  9. func NewInt64(items ...int64) Int64 {
  10. ss := Int64{}
  11. ss.Insert(items...)
  12. return ss
  13. }
  14. // Int64KeySet creates a Int64 from a keys of a map[int64](? extends interface{}).
  15. // If the value passed in is not actually a map, this will panic.
  16. func Int64KeySet(theMap interface{}) Int64 {
  17. v := reflect.ValueOf(theMap)
  18. ret := Int64{}
  19. for _, keyValue := range v.MapKeys() {
  20. ret.Insert(keyValue.Interface().(int64))
  21. }
  22. return ret
  23. }
  24. // Insert adds items to the set.
  25. func (s Int64) Insert(items ...int64) {
  26. for _, item := range items {
  27. s[item] = Empty{}
  28. }
  29. }
  30. // Delete removes all items from the set.
  31. func (s Int64) Delete(items ...int64) {
  32. for _, item := range items {
  33. delete(s, item)
  34. }
  35. }
  36. // Has returns true if and only if item is contained in the set.
  37. func (s Int64) Has(item int64) bool {
  38. _, contained := s[item]
  39. return contained
  40. }
  41. // HasAll returns true if and only if all items are contained in the set.
  42. func (s Int64) HasAll(items ...int64) bool {
  43. for _, item := range items {
  44. if !s.Has(item) {
  45. return false
  46. }
  47. }
  48. return true
  49. }
  50. // HasAny returns true if any items are contained in the set.
  51. func (s Int64) HasAny(items ...int64) bool {
  52. for _, item := range items {
  53. if s.Has(item) {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. // Difference returns a set of objects that are not in s2
  60. // For example:
  61. // s1 = {a1, a2, a3}
  62. // s2 = {a1, a2, a4, a5}
  63. // s1.Difference(s2) = {a3}
  64. // s2.Difference(s1) = {a4, a5}
  65. func (s Int64) Difference(s2 Int64) Int64 {
  66. result := NewInt64()
  67. for key := range s {
  68. if !s2.Has(key) {
  69. result.Insert(key)
  70. }
  71. }
  72. return result
  73. }
  74. // Union returns a new set which includes items in either s1 or s2.
  75. // For example:
  76. // s1 = {a1, a2}
  77. // s2 = {a3, a4}
  78. // s1.Union(s2) = {a1, a2, a3, a4}
  79. // s2.Union(s1) = {a1, a2, a3, a4}
  80. func (s1 Int64) Union(s2 Int64) Int64 {
  81. result := NewInt64()
  82. for key := range s1 {
  83. result.Insert(key)
  84. }
  85. for key := range s2 {
  86. result.Insert(key)
  87. }
  88. return result
  89. }
  90. // Intersection returns a new set which includes the item in BOTH s1 and s2
  91. // For example:
  92. // s1 = {a1, a2}
  93. // s2 = {a2, a3}
  94. // s1.Intersection(s2) = {a2}
  95. func (s1 Int64) Intersection(s2 Int64) Int64 {
  96. var walk, other Int64
  97. result := NewInt64()
  98. if s1.Len() < s2.Len() {
  99. walk = s1
  100. other = s2
  101. } else {
  102. walk = s2
  103. other = s1
  104. }
  105. for key := range walk {
  106. if other.Has(key) {
  107. result.Insert(key)
  108. }
  109. }
  110. return result
  111. }
  112. // IsSuperset returns true if and only if s1 is a superset of s2.
  113. func (s1 Int64) IsSuperset(s2 Int64) bool {
  114. for item := range s2 {
  115. if !s1.Has(item) {
  116. return false
  117. }
  118. }
  119. return true
  120. }
  121. // Equal returns true if and only if s1 is equal (as a set) to s2.
  122. // Two sets are equal if their membership is identical.
  123. // (In practice, this means same elements, order doesn't matter)
  124. func (s1 Int64) Equal(s2 Int64) bool {
  125. return len(s1) == len(s2) && s1.IsSuperset(s2)
  126. }
  127. type sortableSliceOfInt64 []int64
  128. func (s sortableSliceOfInt64) Len() int { return len(s) }
  129. func (s sortableSliceOfInt64) Less(i, j int) bool { return lessInt64(s[i], s[j]) }
  130. func (s sortableSliceOfInt64) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  131. // List returns the contents as a sorted int64 slice.
  132. func (s Int64) List() []int64 {
  133. res := make(sortableSliceOfInt64, 0, len(s))
  134. for key := range s {
  135. res = append(res, key)
  136. }
  137. sort.Sort(res)
  138. return []int64(res)
  139. }
  140. // UnsortedList returns the slice with contents in random order.
  141. func (s Int64) UnsortedList() []int64 {
  142. res := make([]int64, 0, len(s))
  143. for key := range s {
  144. res = append(res, key)
  145. }
  146. return res
  147. }
  148. // Returns a single element from the set.
  149. func (s Int64) PopAny() (int64, bool) {
  150. for key := range s {
  151. s.Delete(key)
  152. return key, true
  153. }
  154. var zeroValue int64
  155. return zeroValue, false
  156. }
  157. // Len returns the size of the set.
  158. func (s Int64) Len() int {
  159. return len(s)
  160. }
  161. func lessInt64(lhs, rhs int64) bool {
  162. return lhs < rhs
  163. }