Skip to content

Commit

Permalink
Add Sets Intersection and Union Operations (#48)
Browse files Browse the repository at this point in the history
* update deps

* union and intersection operations added to sets

* tests were added

---------

Co-authored-by: Michael Sverdlov <[email protected]>
Co-authored-by: Michael Sverdlov <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2024
1 parent a2862b4 commit 6fbd5ec
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
35 changes: 17 additions & 18 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,36 @@ on:
- "**"
pull_request:
jobs:
go-analysis:
Static-Check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout Source
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v5
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
cache: false

- name: Static Code Analysis
uses: golangci/golangci-lint-action@v3
with:
args: |
--timeout 5m --out-${NO_FUTURE}format colored-line-number --enable gosec,errcheck,gosimple,govet,ineffassign,staticcheck,typecheck,unused,gocritic,asasalint,asciicheck,errchkjson,exportloopref,forcetypeassert,makezero,nilerr,unparam,unconvert,wastedassign,usestdlibvars
--timeout 5m --out-${NO_FUTURE}format colored-line-number --enable errcheck,gosimple,govet,ineffassign,staticcheck,typecheck,unused,gocritic,asasalint,asciicheck,errchkjson,exportloopref,makezero,nilerr,unparam,unconvert,wastedassign,usestdlibvars
bash-analysis:
Go-Sec:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout Source
uses: actions/checkout@v3

- name: Run ShellCheck
uses: ludeeus/action-shellcheck@master
- name: Install Go
uses: actions/setup-go@v3
with:
ignore_paths: "*test*"

check-spelling:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
go-version: 1.20.x

- name: Check spelling
uses: crate-ci/typos@master
- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
args: -exclude G204,G304,G404,G401,G505 -tests -exclude-dir \.*test\.* ./...
26 changes: 26 additions & 0 deletions datastructures/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ func (set *Set[T]) ToSlice() []T {

return slice
}

func (setA *Set[T]) Intersect(setB *Set[T]) *Set[T] {
intersectedSet := MakeSet[T]()
bigSet, smallSet := setB, setA
if setA.Size() > setB.Size() {
bigSet, smallSet = setA, setB
}

for key := range smallSet.container {
if bigSet.Exists(key) {
intersectedSet.Add(key)
}
}
return intersectedSet
}

func (setA *Set[T]) Union(setB *Set[T]) *Set[T] {
unionedSet := MakeSet[T]()
for key := range setA.container {
unionedSet.Add(key)
}
for key := range setB.container {
unionedSet.Add(key)
}
return unionedSet
}
22 changes: 22 additions & 0 deletions datastructures/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,25 @@ func TestMakeSetFromElements(t *testing.T) {
stringSet := MakeSetFromElements(stringSlice...)
assert.ElementsMatch(t, stringSet.ToSlice(), stringSlice)
}

func TestSetsIntersection(t *testing.T) {
testSet := generateNewSetWithData()
anotherSet := MakeSet[int]()
intersectedSet := testSet.Intersect(anotherSet)
assert.Equal(t, 0, intersectedSet.Size())

anotherSet.Add(3)
intersectedSet = testSet.Intersect(anotherSet)
assert.Equal(t, 1, intersectedSet.Size())
}

func TestSetsUnion(t *testing.T) {
testSet := generateNewSetWithData()
anotherSet := MakeSet[int]()
unionedSet := testSet.Union(anotherSet)
assert.Equal(t, 3, unionedSet.Size())

anotherSet.Add(4)
unionedSet = testSet.Union(anotherSet)
assert.Equal(t, 4, unionedSet.Size())
}
5 changes: 2 additions & 3 deletions fanout/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestFanoutRead(t *testing.T) {
if err != nil {
t.Error(err)
}
sum1 := results[0].([]byte)
sum1, ok := results[0].([]byte)
assert.True(t, ok)
sum2, ok := results[1].([]byte)
Expand All @@ -57,8 +56,8 @@ func TestFanoutRead(t *testing.T) {
sum2str := hex.EncodeToString(sum2)
sum3str := hex.EncodeToString(sum3)

if !(sum1str == sum2str && sum1str == sum3str) {
t.Errorf("Sum1 %s and sum2 %s and sum3 %s are not the same", sum1str, sum2str, sum3str)
if sum1str != sum2str || sum1str != sum3str {
t.Errorf("Sum1 %s, Sum2 %s, and Sum3 %s are not all the same", sum1str, sum2str, sum3str)
}

if sum1str != sha2sum {
Expand Down
2 changes: 1 addition & 1 deletion io/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func CreateRandomLenFile(maxLen int, filesDir string, prefix string) string {
panic(err)
}
defer created.Close()
//Check that the files were created with expected len
// Check that the files were created with expected len
if created.Info.Size() != int64(len) {
panic(fmt.Errorf("unexpected file length. Expected: %d, Got %d", created.Info.Size(), len))
}
Expand Down
8 changes: 4 additions & 4 deletions parallel/bounded_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func TestFailFastOnTaskError(t *testing.T) {
wg.Wait()
checkResult(expectedTotal, results, t)

//TODO: Fix this test
//This test is fragile since 1 or more goroutines may be executing and failing fast in parallel,
//calling the error handler and increasing the result. So we cannot use accurate comparison.
//Here we only take care of uo to 1 additional concurrent failfast.
// TODO: Fix this test
// This test is fragile since 1 or more goroutines may be executing and failing fast in parallel,
// calling the error handler and increasing the result. So we cannot use accurate comparison.
// Here we only take care of uo to 1 additional concurrent failfast.
errTotal := 0
for {
err := errorsQueue.GetError()
Expand Down

0 comments on commit 6fbd5ec

Please sign in to comment.