mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
added string and int set implementations
.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package set
|
||||
|
||||
// StringSet maintains a deduped list of strings added to it
|
||||
type StringSet map[string]bool
|
||||
|
||||
// NewStringSet initializes a set with the values form the input string slice
|
||||
func NewStringSet(stringSlice []string) StringSet {
|
||||
set := StringSet{}
|
||||
for _, v := range stringSlice {
|
||||
set[v] = true
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// Has returns true if the value if found in the underlying set
|
||||
func (s StringSet) Has(val string) bool {
|
||||
_, ok := s[val]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Add a value to the set
|
||||
func (s StringSet) Add(val string) {
|
||||
s[val] = true
|
||||
}
|
||||
|
||||
// Remove a value from the set
|
||||
func (s StringSet) Remove(val string) {
|
||||
delete(s, val)
|
||||
}
|
||||
|
||||
// Keys returns a slice of all keys in the set
|
||||
func (s StringSet) Keys() []string {
|
||||
var keys []string
|
||||
for k := range s {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// IntSet maintains a deduped list of strings added to it
|
||||
type IntSet map[int]bool
|
||||
|
||||
// NewIntSet initializes a set with the values form the input int slice
|
||||
func NewIntSet(intSlice []int) IntSet {
|
||||
set := IntSet{}
|
||||
for _, v := range intSlice {
|
||||
set[v] = true
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// Has returns true if the value if found in the underlying set
|
||||
func (s IntSet) Has(val int) bool {
|
||||
_, ok := s[val]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Add a value to the set
|
||||
func (s IntSet) Add(val int) {
|
||||
s[val] = true
|
||||
}
|
||||
|
||||
// Remove a value from the set
|
||||
func (s IntSet) Remove(val int) {
|
||||
delete(s, val)
|
||||
}
|
||||
|
||||
// Keys returns a slice of all keys in the set
|
||||
func (s IntSet) Keys() []int {
|
||||
var keys []int
|
||||
for k := range s {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package set_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/data-structures/set"
|
||||
)
|
||||
|
||||
func TestIntSet(t *testing.T) {
|
||||
tests := []struct {
|
||||
valToAdd int
|
||||
wantHas []int
|
||||
}{
|
||||
{5, []int{5}},
|
||||
{5, []int{5}},
|
||||
{10, []int{5, 10}},
|
||||
{20, []int{5, 10, 20}},
|
||||
{20, []int{5, 10, 20}},
|
||||
{2230, []int{5, 10, 20, 2230}},
|
||||
{123, []int{5, 10, 20, 123, 2230}},
|
||||
}
|
||||
|
||||
intSet := set.NewIntSet(nil)
|
||||
for _, tt := range tests {
|
||||
intSet.Add(tt.valToAdd)
|
||||
for _, want := range tt.wantHas {
|
||||
if !intSet.Has(want) {
|
||||
t.Errorf("want IntSet.Has(%d) = true, got false", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
got := intSet.Keys()
|
||||
sort.Ints(got)
|
||||
want := []int{5, 10, 20, 123, 2230}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("want intSet.Keys() to be %v, got %v", want, got)
|
||||
}
|
||||
|
||||
valsToRemove := []int{5, 10, 20, 123, 2230}
|
||||
for _, tt := range valsToRemove {
|
||||
intSet.Remove(tt)
|
||||
if intSet.Has(tt) {
|
||||
t.Errorf("want IntSet.Has(%d) = false, got true", tt)
|
||||
}
|
||||
}
|
||||
|
||||
got = intSet.Keys()
|
||||
if len(got) != 0 {
|
||||
t.Errorf("want zero-length slice after removing all keys, got %v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user