gset_int_set.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. //
  7. package gset
  8. import (
  9. "bytes"
  10. "github.com/gogf/gf/internal/json"
  11. "github.com/gogf/gf/internal/rwmutex"
  12. "github.com/gogf/gf/util/gconv"
  13. )
  14. type IntSet struct {
  15. mu rwmutex.RWMutex
  16. data map[int]struct{}
  17. }
  18. // New create and returns a new set, which contains un-repeated items.
  19. // The parameter <safe> is used to specify whether using set in concurrent-safety,
  20. // which is false in default.
  21. func NewIntSet(safe ...bool) *IntSet {
  22. return &IntSet{
  23. mu: rwmutex.Create(safe...),
  24. data: make(map[int]struct{}),
  25. }
  26. }
  27. // NewIntSetFrom returns a new set from <items>.
  28. func NewIntSetFrom(items []int, safe ...bool) *IntSet {
  29. m := make(map[int]struct{})
  30. for _, v := range items {
  31. m[v] = struct{}{}
  32. }
  33. return &IntSet{
  34. mu: rwmutex.Create(safe...),
  35. data: m,
  36. }
  37. }
  38. // Iterator iterates the set readonly with given callback function <f>,
  39. // if <f> returns true then continue iterating; or false to stop.
  40. func (set *IntSet) Iterator(f func(v int) bool) {
  41. set.mu.RLock()
  42. defer set.mu.RUnlock()
  43. for k, _ := range set.data {
  44. if !f(k) {
  45. break
  46. }
  47. }
  48. }
  49. // Add adds one or multiple items to the set.
  50. func (set *IntSet) Add(item ...int) {
  51. set.mu.Lock()
  52. if set.data == nil {
  53. set.data = make(map[int]struct{})
  54. }
  55. for _, v := range item {
  56. set.data[v] = struct{}{}
  57. }
  58. set.mu.Unlock()
  59. }
  60. // AddIfNotExist checks whether item exists in the set,
  61. // it adds the item to set and returns true if it does not exists in the set,
  62. // or else it does nothing and returns false.
  63. //
  64. // Note that, if <item> is nil, it does nothing and returns false.
  65. func (set *IntSet) AddIfNotExist(item int) bool {
  66. if !set.Contains(item) {
  67. set.mu.Lock()
  68. defer set.mu.Unlock()
  69. if set.data == nil {
  70. set.data = make(map[int]struct{})
  71. }
  72. if _, ok := set.data[item]; !ok {
  73. set.data[item] = struct{}{}
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. // AddIfNotExistFunc checks whether item exists in the set,
  80. // it adds the item to set and returns true if it does not exists in the set and
  81. // function <f> returns true, or else it does nothing and returns false.
  82. //
  83. // Note that, the function <f> is executed without writing lock.
  84. func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool {
  85. if !set.Contains(item) {
  86. if f() {
  87. set.mu.Lock()
  88. defer set.mu.Unlock()
  89. if set.data == nil {
  90. set.data = make(map[int]struct{})
  91. }
  92. if _, ok := set.data[item]; !ok {
  93. set.data[item] = struct{}{}
  94. return true
  95. }
  96. }
  97. }
  98. return false
  99. }
  100. // AddIfNotExistFunc checks whether item exists in the set,
  101. // it adds the item to set and returns true if it does not exists in the set and
  102. // function <f> returns true, or else it does nothing and returns false.
  103. //
  104. // Note that, the function <f> is executed without writing lock.
  105. func (set *IntSet) AddIfNotExistFuncLock(item int, f func() bool) bool {
  106. if !set.Contains(item) {
  107. set.mu.Lock()
  108. defer set.mu.Unlock()
  109. if set.data == nil {
  110. set.data = make(map[int]struct{})
  111. }
  112. if f() {
  113. if _, ok := set.data[item]; !ok {
  114. set.data[item] = struct{}{}
  115. return true
  116. }
  117. }
  118. }
  119. return false
  120. }
  121. // Contains checks whether the set contains <item>.
  122. func (set *IntSet) Contains(item int) bool {
  123. var ok bool
  124. set.mu.RLock()
  125. if set.data != nil {
  126. _, ok = set.data[item]
  127. }
  128. set.mu.RUnlock()
  129. return ok
  130. }
  131. // Remove deletes <item> from set.
  132. func (set *IntSet) Remove(item int) {
  133. set.mu.Lock()
  134. if set.data != nil {
  135. delete(set.data, item)
  136. }
  137. set.mu.Unlock()
  138. }
  139. // Size returns the size of the set.
  140. func (set *IntSet) Size() int {
  141. set.mu.RLock()
  142. l := len(set.data)
  143. set.mu.RUnlock()
  144. return l
  145. }
  146. // Clear deletes all items of the set.
  147. func (set *IntSet) Clear() {
  148. set.mu.Lock()
  149. set.data = make(map[int]struct{})
  150. set.mu.Unlock()
  151. }
  152. // Slice returns the a of items of the set as slice.
  153. func (set *IntSet) Slice() []int {
  154. set.mu.RLock()
  155. var (
  156. i = 0
  157. ret = make([]int, len(set.data))
  158. )
  159. for k, _ := range set.data {
  160. ret[i] = k
  161. i++
  162. }
  163. set.mu.RUnlock()
  164. return ret
  165. }
  166. // Join joins items with a string <glue>.
  167. func (set *IntSet) Join(glue string) string {
  168. set.mu.RLock()
  169. defer set.mu.RUnlock()
  170. if len(set.data) == 0 {
  171. return ""
  172. }
  173. var (
  174. l = len(set.data)
  175. i = 0
  176. buffer = bytes.NewBuffer(nil)
  177. )
  178. for k, _ := range set.data {
  179. buffer.WriteString(gconv.String(k))
  180. if i != l-1 {
  181. buffer.WriteString(glue)
  182. }
  183. i++
  184. }
  185. return buffer.String()
  186. }
  187. // String returns items as a string, which implements like json.Marshal does.
  188. func (set *IntSet) String() string {
  189. return "[" + set.Join(",") + "]"
  190. }
  191. // LockFunc locks writing with callback function <f>.
  192. func (set *IntSet) LockFunc(f func(m map[int]struct{})) {
  193. set.mu.Lock()
  194. defer set.mu.Unlock()
  195. f(set.data)
  196. }
  197. // RLockFunc locks reading with callback function <f>.
  198. func (set *IntSet) RLockFunc(f func(m map[int]struct{})) {
  199. set.mu.RLock()
  200. defer set.mu.RUnlock()
  201. f(set.data)
  202. }
  203. // Equal checks whether the two sets equal.
  204. func (set *IntSet) Equal(other *IntSet) bool {
  205. if set == other {
  206. return true
  207. }
  208. set.mu.RLock()
  209. defer set.mu.RUnlock()
  210. other.mu.RLock()
  211. defer other.mu.RUnlock()
  212. if len(set.data) != len(other.data) {
  213. return false
  214. }
  215. for key := range set.data {
  216. if _, ok := other.data[key]; !ok {
  217. return false
  218. }
  219. }
  220. return true
  221. }
  222. // IsSubsetOf checks whether the current set is a sub-set of <other>.
  223. func (set *IntSet) IsSubsetOf(other *IntSet) bool {
  224. if set == other {
  225. return true
  226. }
  227. set.mu.RLock()
  228. defer set.mu.RUnlock()
  229. other.mu.RLock()
  230. defer other.mu.RUnlock()
  231. for key := range set.data {
  232. if _, ok := other.data[key]; !ok {
  233. return false
  234. }
  235. }
  236. return true
  237. }
  238. // Union returns a new set which is the union of <set> and <other>.
  239. // Which means, all the items in <newSet> are in <set> or in <other>.
  240. func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet) {
  241. newSet = NewIntSet()
  242. set.mu.RLock()
  243. defer set.mu.RUnlock()
  244. for _, other := range others {
  245. if set != other {
  246. other.mu.RLock()
  247. }
  248. for k, v := range set.data {
  249. newSet.data[k] = v
  250. }
  251. if set != other {
  252. for k, v := range other.data {
  253. newSet.data[k] = v
  254. }
  255. }
  256. if set != other {
  257. other.mu.RUnlock()
  258. }
  259. }
  260. return
  261. }
  262. // Diff returns a new set which is the difference set from <set> to <other>.
  263. // Which means, all the items in <newSet> are in <set> but not in <other>.
  264. func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet) {
  265. newSet = NewIntSet()
  266. set.mu.RLock()
  267. defer set.mu.RUnlock()
  268. for _, other := range others {
  269. if set == other {
  270. continue
  271. }
  272. other.mu.RLock()
  273. for k, v := range set.data {
  274. if _, ok := other.data[k]; !ok {
  275. newSet.data[k] = v
  276. }
  277. }
  278. other.mu.RUnlock()
  279. }
  280. return
  281. }
  282. // Intersect returns a new set which is the intersection from <set> to <other>.
  283. // Which means, all the items in <newSet> are in <set> and also in <other>.
  284. func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet) {
  285. newSet = NewIntSet()
  286. set.mu.RLock()
  287. defer set.mu.RUnlock()
  288. for _, other := range others {
  289. if set != other {
  290. other.mu.RLock()
  291. }
  292. for k, v := range set.data {
  293. if _, ok := other.data[k]; ok {
  294. newSet.data[k] = v
  295. }
  296. }
  297. if set != other {
  298. other.mu.RUnlock()
  299. }
  300. }
  301. return
  302. }
  303. // Complement returns a new set which is the complement from <set> to <full>.
  304. // Which means, all the items in <newSet> are in <full> and not in <set>.
  305. //
  306. // It returns the difference between <full> and <set>
  307. // if the given set <full> is not the full set of <set>.
  308. func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) {
  309. newSet = NewIntSet()
  310. set.mu.RLock()
  311. defer set.mu.RUnlock()
  312. if set != full {
  313. full.mu.RLock()
  314. defer full.mu.RUnlock()
  315. }
  316. for k, v := range full.data {
  317. if _, ok := set.data[k]; !ok {
  318. newSet.data[k] = v
  319. }
  320. }
  321. return
  322. }
  323. // Merge adds items from <others> sets into <set>.
  324. func (set *IntSet) Merge(others ...*IntSet) *IntSet {
  325. set.mu.Lock()
  326. defer set.mu.Unlock()
  327. for _, other := range others {
  328. if set != other {
  329. other.mu.RLock()
  330. }
  331. for k, v := range other.data {
  332. set.data[k] = v
  333. }
  334. if set != other {
  335. other.mu.RUnlock()
  336. }
  337. }
  338. return set
  339. }
  340. // Sum sums items.
  341. // Note: The items should be converted to int type,
  342. // or you'd get a result that you unexpected.
  343. func (set *IntSet) Sum() (sum int) {
  344. set.mu.RLock()
  345. defer set.mu.RUnlock()
  346. for k, _ := range set.data {
  347. sum += k
  348. }
  349. return
  350. }
  351. // Pops randomly pops an item from set.
  352. func (set *IntSet) Pop() int {
  353. set.mu.Lock()
  354. defer set.mu.Unlock()
  355. for k, _ := range set.data {
  356. delete(set.data, k)
  357. return k
  358. }
  359. return 0
  360. }
  361. // Pops randomly pops <size> items from set.
  362. // It returns all items if size == -1.
  363. func (set *IntSet) Pops(size int) []int {
  364. set.mu.Lock()
  365. defer set.mu.Unlock()
  366. if size > len(set.data) || size == -1 {
  367. size = len(set.data)
  368. }
  369. if size <= 0 {
  370. return nil
  371. }
  372. index := 0
  373. array := make([]int, size)
  374. for k, _ := range set.data {
  375. delete(set.data, k)
  376. array[index] = k
  377. index++
  378. if index == size {
  379. break
  380. }
  381. }
  382. return array
  383. }
  384. // Walk applies a user supplied function <f> to every item of set.
  385. func (set *IntSet) Walk(f func(item int) int) *IntSet {
  386. set.mu.Lock()
  387. defer set.mu.Unlock()
  388. m := make(map[int]struct{}, len(set.data))
  389. for k, v := range set.data {
  390. m[f(k)] = v
  391. }
  392. set.data = m
  393. return set
  394. }
  395. // MarshalJSON implements the interface MarshalJSON for json.Marshal.
  396. func (set *IntSet) MarshalJSON() ([]byte, error) {
  397. return json.Marshal(set.Slice())
  398. }
  399. // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
  400. func (set *IntSet) UnmarshalJSON(b []byte) error {
  401. set.mu.Lock()
  402. defer set.mu.Unlock()
  403. if set.data == nil {
  404. set.data = make(map[int]struct{})
  405. }
  406. var array []int
  407. if err := json.Unmarshal(b, &array); err != nil {
  408. return err
  409. }
  410. for _, v := range array {
  411. set.data[v] = struct{}{}
  412. }
  413. return nil
  414. }
  415. // UnmarshalValue is an interface implement which sets any type of value for set.
  416. func (set *IntSet) UnmarshalValue(value interface{}) (err error) {
  417. set.mu.Lock()
  418. defer set.mu.Unlock()
  419. if set.data == nil {
  420. set.data = make(map[int]struct{})
  421. }
  422. var array []int
  423. switch value.(type) {
  424. case string, []byte:
  425. err = json.Unmarshal(gconv.Bytes(value), &array)
  426. default:
  427. array = gconv.SliceInt(value)
  428. }
  429. for _, v := range array {
  430. set.data[v] = struct{}{}
  431. }
  432. return
  433. }