collection.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package fp
  2. import (
  3. "bytes"
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. )
  9. // IntList is a list of integers
  10. type IntList []uint64
  11. // IntSet is a set of integers
  12. type IntSet map[uint64]bool
  13. // NewIntList returns a string list parsed from a string.
  14. func NewIntList(s string) (IntList, error) {
  15. var a IntList
  16. err := a.Parse(s)
  17. return a, err
  18. }
  19. // Parse an int list from a string and return an error on failure
  20. func (a *IntList) Parse(s string) error {
  21. *a = nil
  22. var split []string
  23. if len(s) > 0 {
  24. split = strings.Split(s, ",")
  25. }
  26. for _, v := range split {
  27. if len(v) == 0 {
  28. return fmt.Errorf("invalid int list format: '%s'", s)
  29. }
  30. elem, err := strconv.ParseUint(v, 16, 16)
  31. if err != nil {
  32. return err
  33. }
  34. *a = append(*a, elem)
  35. }
  36. return nil
  37. }
  38. // String returns a comma-separated string of list elements
  39. func (a IntList) String() string {
  40. var buf bytes.Buffer
  41. for idx, elem := range a {
  42. if idx != 0 {
  43. buf.WriteString(",")
  44. }
  45. buf.WriteString(fmt.Sprintf("%x", elem))
  46. }
  47. return buf.String()
  48. }
  49. // Contains returns true if b is an ordered subsequence of a
  50. func (a IntList) Contains(b IntList) bool {
  51. bIdx := 0
  52. bLen := len(b)
  53. if bLen == 0 {
  54. return true
  55. }
  56. for _, elem := range a {
  57. if elem == b[bIdx] {
  58. bIdx++
  59. if bIdx == bLen {
  60. return true
  61. }
  62. }
  63. }
  64. return false
  65. }
  66. // Equals returns true if a and b are equal
  67. func (a IntList) Equals(b IntList) bool {
  68. if len(a) != len(b) {
  69. return false
  70. }
  71. for idx := range a {
  72. if a[idx] != b[idx] {
  73. return false
  74. }
  75. }
  76. return true
  77. }
  78. // Set returns a set representation of a list
  79. func (a IntList) Set() IntSet {
  80. set := make(IntSet, len(a))
  81. for _, elem := range a {
  82. set[elem] = true
  83. }
  84. return set
  85. }
  86. // List returns a list representation of a set in sorted order
  87. func (a IntSet) List() IntList {
  88. list := make(IntList, len(a))
  89. idx := 0
  90. for elem := range a {
  91. list[idx] = elem
  92. idx++
  93. }
  94. sort.Slice(list, func(i, j int) bool { return list[i] < list[j] })
  95. return list
  96. }
  97. // Inter returns the set intersection (a & b)
  98. func (a IntSet) Inter(b IntSet) IntSet {
  99. inter := make(IntSet, len(a))
  100. for elem := range a {
  101. if b[elem] {
  102. inter[elem] = true
  103. }
  104. }
  105. return inter
  106. }
  107. // Diff returns the set difference (a - b)
  108. func (a IntSet) Diff(b IntSet) IntSet {
  109. diff := make(IntSet, len(a))
  110. for elem := range a {
  111. if !b[elem] {
  112. diff[elem] = true
  113. }
  114. }
  115. return diff
  116. }
  117. // Union returns the set union (a | b)
  118. func (a IntSet) Union(b IntSet) IntSet {
  119. union := make(IntSet, len(a)+len(b))
  120. for elem := range a {
  121. union[elem] = true
  122. }
  123. for elem := range b {
  124. union[elem] = true
  125. }
  126. return union
  127. }
  128. // StringList is a list of strings
  129. type StringList []string
  130. // StringSet is a set of strings
  131. type StringSet map[string]bool
  132. // NewStringList returns a string list parsed from a string.
  133. func NewStringList(s string) (StringList, error) {
  134. var a StringList
  135. err := a.Parse(s)
  136. return a, err
  137. }
  138. // Parse a stringlist from a string and return an error on failure
  139. func (a *StringList) Parse(s string) error {
  140. *a = nil
  141. if len(s) > 0 {
  142. *a = strings.Split(s, ",")
  143. }
  144. return nil
  145. }
  146. // String returns a comma-separated string of list elements
  147. func (a StringList) String() string {
  148. var buf bytes.Buffer
  149. for idx, elem := range a {
  150. if idx != 0 {
  151. buf.WriteString(",")
  152. }
  153. buf.WriteString(elem)
  154. }
  155. return buf.String()
  156. }
  157. // Contains returns true if b is an ordered subsequence of a
  158. func (a StringList) Contains(b StringList) bool {
  159. bIdx := 0
  160. bLen := len(b)
  161. if bLen == 0 {
  162. return true
  163. }
  164. for _, elem := range a {
  165. if elem == b[bIdx] {
  166. bIdx++
  167. if bIdx == bLen {
  168. return true
  169. }
  170. }
  171. }
  172. return false
  173. }
  174. // Equals returns true if a and b are equal
  175. func (a StringList) Equals(b StringList) bool {
  176. if len(a) != len(b) {
  177. return false
  178. }
  179. for idx := range a {
  180. if a[idx] != b[idx] {
  181. return false
  182. }
  183. }
  184. return true
  185. }
  186. // Set returns a set representation of a list
  187. func (a StringList) Set() StringSet {
  188. set := make(StringSet, len(a))
  189. for _, elem := range a {
  190. set[elem] = true
  191. }
  192. return set
  193. }
  194. // List returns a list representation of a set in sorted order
  195. func (a StringSet) List() StringList {
  196. list := make(StringList, len(a))
  197. idx := 0
  198. for elem := range a {
  199. list[idx] = elem
  200. idx++
  201. }
  202. sort.Slice(list, func(i, j int) bool { return list[i] < list[j] })
  203. return list
  204. }
  205. // Inter returns the set intersection (a & b)
  206. func (a StringSet) Inter(b StringSet) StringSet {
  207. inter := make(StringSet, len(a))
  208. for elem := range a {
  209. if b[elem] {
  210. inter[elem] = true
  211. }
  212. }
  213. return inter
  214. }
  215. // Diff returns the set difference (a - b)
  216. func (a StringSet) Diff(b StringSet) StringSet {
  217. diff := make(StringSet, len(a))
  218. for elem := range a {
  219. if !b[elem] {
  220. diff[elem] = true
  221. }
  222. }
  223. return diff
  224. }
  225. // Union returns the set union (a | b)
  226. func (a StringSet) Union(b StringSet) StringSet {
  227. union := make(StringSet, len(a)+len(b))
  228. for elem := range a {
  229. union[elem] = true
  230. }
  231. for elem := range b {
  232. union[elem] = true
  233. }
  234. return union
  235. }