Commit 505ad080 authored by Bochao Wang's avatar Bochao Wang

Merge branch '1-add-support-for-a-set-of-ints' into 'master'

Resolve "Add support for a set of ints"

Closes #1

See merge request !1
parents 4fd2ff85 51c461b9
Pipeline #12129 passed with stage
in 8 seconds
//Sets is a package of set data structures that are strongly typed so it is easy
//Package sets is a package of set data structures that are strongly typed so it is easy
//to use in go without the issue of casting from interface{}
package sets
package sets
//Int32s represents a set of Int32s
type Int32s struct {
data map[int32]bool
}
//NewInt32s initializes a new set of Int32s
func NewInt32s(initialValues ...int32) *Int32s {
data := make(map[int32]bool)
for _, v := range initialValues {
data[v] = true
}
return &Int32s{
data: data,
}
}
//Add will add one or more values to the set
func (ss *Int32s) Add(values ...int32) {
for _, v := range values {
ss.data[v] = true
}
}
//Cardinality will return the number of unique elements in the set
func (ss *Int32s) Cardinality() int {
return len(ss.data)
}
//Contains will determine if the string provided exists in the set
func (ss *Int32s) Contains(value int32) bool {
v, ok := ss.data[value]
return ok && v
}
//Intersect will return a new set with only the values found in both sets
func (ss *Int32s) Intersect(ss2 *Int32s) *Int32s {
src, dest := ss, ss2
if len(ss.data) > len(ss2.data) {
src, dest = ss2, ss
}
results := make(map[int32]bool)
for v := range src.data {
if dest.Contains(v) {
results[v] = true
}
}
return &Int32s{
data: results,
}
}
//Slice creates a slice of the values in the set, the values are not sorted
func (ss *Int32s) Slice() []int32 {
results := make([]int32, 0, len(ss.data))
for v := range ss.data {
results = append(results, v)
}
return results
}
package sets
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestInt32s(t *testing.T) {
i := NewInt32s()
i.Add(1)
i.Add(2)
require.Equal(t, 2, i.Cardinality())
require.True(t, i.Contains(2))
require.False(t, i.Contains(3))
i2 := NewInt32s()
i2.Add(2)
i2.Add(3)
i3 := i.Intersect(i2)
require.Equal(t, 1, i3.Cardinality())
require.True(t, i3.Contains(2))
require.False(t, i3.Contains(1))
require.False(t, i3.Contains(3))
s1Slice := i.Slice()
require.ElementsMatch(t, s1Slice, []int32{1, 2})
i4 := NewInt32s(1, 2, 2, 2, 3, 2, 2)
require.Equal(t, 3, i4.Cardinality())
require.True(t, i4.Contains(1))
require.True(t, i4.Contains(2))
require.True(t, i4.Contains(3))
require.False(t, i4.Contains(4))
i4.Add(3)
require.Equal(t, 3, i4.Cardinality())
i5 := i4.Intersect(i3)
require.Equal(t, 1, i5.Cardinality())
require.True(t, i5.Contains(2))
}
package sets
//Int64s represents a set of Int64s
type Int64s struct {
data map[int64]bool
}
//NewInt64s initializes a new set of Int64s
func NewInt64s(initialValues ...int64) *Int64s {
data := make(map[int64]bool)
for _, v := range initialValues {
data[v] = true
}
return &Int64s{
data: data,
}
}
//Add will add one or more values to the set
func (ss *Int64s) Add(values ...int64) {
for _, v := range values {
ss.data[v] = true
}
}
//Cardinality will return the number of unique elements in the set
func (ss *Int64s) Cardinality() int {
return len(ss.data)
}
//Contains will determine if the string provided exists in the set
func (ss *Int64s) Contains(value int64) bool {
v, ok := ss.data[value]
return ok && v
}
//Intersect will return a new set with only the values found in both sets
func (ss *Int64s) Intersect(ss2 *Int64s) *Int64s {
src, dest := ss, ss2
if len(ss.data) > len(ss2.data) {
src, dest = ss2, ss
}
results := make(map[int64]bool)
for v := range src.data {
if dest.Contains(v) {
results[v] = true
}
}
return &Int64s{
data: results,
}
}
//Slice creates a slice of the values in the set, the values are not sorted
func (ss *Int64s) Slice() []int64 {
results := make([]int64, 0, len(ss.data))
for v := range ss.data {
results = append(results, v)
}
return results
}
package sets
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestInt64s(t *testing.T) {
i := NewInt64s()
i.Add(1)
i.Add(2)
require.Equal(t, 2, i.Cardinality())
require.True(t, i.Contains(2))
require.False(t, i.Contains(3))
i2 := NewInt64s()
i2.Add(2)
i2.Add(3)
i3 := i.Intersect(i2)
require.Equal(t, 1, i3.Cardinality())
require.True(t, i3.Contains(2))
require.False(t, i3.Contains(1))
require.False(t, i3.Contains(3))
s1Slice := i.Slice()
require.ElementsMatch(t, s1Slice, []int64{1, 2})
i4 := NewInt64s(1, 2, 2, 2, 3, 2, 2)
require.Equal(t, 3, i4.Cardinality())
require.True(t, i4.Contains(1))
require.True(t, i4.Contains(2))
require.True(t, i4.Contains(3))
require.False(t, i4.Contains(4))
i4.Add(3)
require.Equal(t, 3, i4.Cardinality())
i5 := i4.Intersect(i3)
require.Equal(t, 1, i5.Cardinality())
require.True(t, i5.Contains(2))
}
package sets
//Ints represents a set of ints
type Ints struct {
data map[int]bool
}
//NewInts initializes a new set of ints
func NewInts(initialValues ...int) *Ints {
data := make(map[int]bool)
for _, v := range initialValues {
data[v] = true
}
return &Ints{
data: data,
}
}
//Add will add one or more values to the set
func (ss *Ints) Add(values ...int) {
for _, v := range values {
ss.data[v] = true
}
}
//Cardinality will return the number of unique elements in the set
func (ss *Ints) Cardinality() int {
return len(ss.data)
}
//Contains will determine if the string provided exists in the set
func (ss *Ints) Contains(value int) bool {
v, ok := ss.data[value]
return ok && v
}
//Intersect will return a new set with only the values found in both sets
func (ss *Ints) Intersect(ss2 *Ints) *Ints {
src, dest := ss, ss2
if len(ss.data) > len(ss2.data) {
src, dest = ss2, ss
}
results := make(map[int]bool)
for v := range src.data {
if dest.Contains(v) {
results[v] = true
}
}
return &Ints{
data: results,
}
}
//Slice creates a slice of the values in the set, the values are not sorted
func (ss *Ints) Slice() []int {
results := make([]int, 0, len(ss.data))
for v := range ss.data {
results = append(results, v)
}
return results
}
package sets
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestInts(t *testing.T) {
i := NewInts()
i.Add(1)
i.Add(2)
require.Equal(t, 2, i.Cardinality())
require.True(t, i.Contains(2))
require.False(t, i.Contains(3))
i2 := NewInts()
i2.Add(2)
i2.Add(3)
i3 := i.Intersect(i2)
require.Equal(t, 1, i3.Cardinality())
require.True(t, i3.Contains(2))
require.False(t, i3.Contains(1))
require.False(t, i3.Contains(3))
s1Slice := i.Slice()
require.ElementsMatch(t, s1Slice, []int{1, 2})
i4 := NewInts(1, 2, 2, 2, 3, 2, 2)
require.Equal(t, 3, i4.Cardinality())
require.True(t, i4.Contains(1))
require.True(t, i4.Contains(2))
require.True(t, i4.Contains(3))
require.False(t, i4.Contains(4))
i4.Add(3)
require.Equal(t, 3, i4.Cardinality())
i5 := i4.Intersect(i3)
require.Equal(t, 1, i5.Cardinality())
require.True(t, i5.Contains(2))
require.False(t, i5.Contains(1))
require.False(t, i5.Contains(3))
require.False(t, i5.Contains(4))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment