Skip to content

Commit

Permalink
replace generic set implemetation with plain set (unstable in go1.19)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman committed Feb 3, 2023
1 parent d19e1be commit 5b629d8
Show file tree
Hide file tree
Showing 13 changed files with 1,037 additions and 174 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ require (
github.com/wagoodman/go-partybus v0.0.0-20200526224238-eb215533f07d
github.com/wagoodman/go-progress v0.0.0-20200621122631-1a2120f0695a
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd
golang.org/x/exp v0.0.0-20230202163644-54bba9f4231b
)

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230202163644-54bba9f4231b h1:EqBVA+nNsObCwQoBEHy4wLU0pi7i8a4AL3pbItPdPkE=
golang.org/x/exp v0.0.0-20230202163644-54bba9f4231b/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
86 changes: 0 additions & 86 deletions internal/set.go

This file was deleted.

76 changes: 76 additions & 0 deletions internal/string_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package internal

import (
"sort"
)

type StringSet map[string]struct{}

func NewStringSet(is ...string) StringSet {
// TODO: replace with single generic implementation that also incorporates other set implementations
s := make(StringSet)
s.Add(is...)
return s
}

func (s StringSet) Size() int {
return len(s)
}

func (s StringSet) Merge(other StringSet) {
for _, i := range other.List() {
s.Add(i)
}
}

func (s StringSet) Add(ids ...string) {
for _, i := range ids {
s[i] = struct{}{}
}
}

func (s StringSet) Remove(ids ...string) {
for _, i := range ids {
delete(s, i)
}
}

func (s StringSet) Contains(i string) bool {
_, ok := s[i]
return ok
}

func (s StringSet) Clear() {
// TODO: replace this with the new 'clear' keyword when it's available in go 1.20 or 1.21
for i := range s {
delete(s, i)
}
}

func (s StringSet) List() []string {
ret := make([]string, 0, len(s))
for i := range s {
ret = append(ret, i)
}
return ret
}

func (s StringSet) Sorted() []string {
ids := s.List()

sort.Slice(ids, func(i, j int) bool {
return ids[i] < ids[j]
})

return ids
}

func (s StringSet) ContainsAny(ids ...string) bool {
for _, i := range ids {
_, ok := s[i]
if ok {
return true
}
}
return false
}
Loading

0 comments on commit 5b629d8

Please sign in to comment.