From bc82528e1e6ee24eb7292cc10ac772281bfb7648 Mon Sep 17 00:00:00 2001 From: emirpasic Date: Fri, 21 Sep 2018 02:27:02 +0200 Subject: [PATCH] Sets bulk intialization --- sets/hashset/hashset.go | 10 +++++++--- sets/hashset/hashset_test.go | 17 +++++++++++++++++ sets/linkedhashset/linkedhashset.go | 10 +++++++--- sets/linkedhashset/linkedhashset_test.go | 14 ++++++++++++++ sets/treeset/treeset.go | 24 ++++++++++++++++++------ sets/treeset/treeset_test.go | 14 ++++++++++++++ 6 files changed, 77 insertions(+), 12 deletions(-) diff --git a/sets/hashset/hashset.go b/sets/hashset/hashset.go index 9669d38..815d049 100644 --- a/sets/hashset/hashset.go +++ b/sets/hashset/hashset.go @@ -26,9 +26,13 @@ type Set struct { var itemExists = struct{}{} -// New instantiates a new empty set -func New() *Set { - return &Set{items: make(map[interface{}]struct{})} +// New instantiates a new empty set and adds the passed values, if any, to the set +func New(values ...interface{}) *Set { + set := &Set{items: make(map[interface{}]struct{})} + if len(values) > 0 { + set.Add(values...) + } + return set } // Add adds the items (one or more) to the set. diff --git a/sets/hashset/hashset_test.go b/sets/hashset/hashset_test.go index 280a6b3..cf63c89 100644 --- a/sets/hashset/hashset_test.go +++ b/sets/hashset/hashset_test.go @@ -8,6 +8,23 @@ import ( "testing" ) +func TestSetNew(t *testing.T) { + set := New(2, 1) + + if actualValue := set.Size(); actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } + if actualValue := set.Contains(1); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } + if actualValue := set.Contains(2); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } + if actualValue := set.Contains(3); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, true) + } +} + func TestSetAdd(t *testing.T) { set := New() set.Add() diff --git a/sets/linkedhashset/linkedhashset.go b/sets/linkedhashset/linkedhashset.go index a9fb2ae..6bce5f6 100644 --- a/sets/linkedhashset/linkedhashset.go +++ b/sets/linkedhashset/linkedhashset.go @@ -30,12 +30,16 @@ type Set struct { var itemExists = struct{}{} -// New instantiates a new empty set -func New() *Set { - return &Set{ +// New instantiates a new empty set and adds the passed values, if any, to the set +func New(values ...interface{}) *Set { + set := &Set{ items: make(map[interface{}]struct{}), list: doublylinkedlist.New(), } + if len(values) > 0 { + set.Add(values...) + } + return set } // Add adds the items (one or more) to the set. diff --git a/sets/linkedhashset/linkedhashset_test.go b/sets/linkedhashset/linkedhashset_test.go index cf3746e..10b6da2 100644 --- a/sets/linkedhashset/linkedhashset_test.go +++ b/sets/linkedhashset/linkedhashset_test.go @@ -9,6 +9,20 @@ import ( "testing" ) +func TestSetNew(t *testing.T) { + set := New(2, 1) + if actualValue := set.Size(); actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } + values := set.Values() + if actualValue := values[0]; actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } + if actualValue := values[1]; actualValue != 1 { + t.Errorf("Got %v expected %v", actualValue, 1) + } +} + func TestSetAdd(t *testing.T) { set := New() set.Add() diff --git a/sets/treeset/treeset.go b/sets/treeset/treeset.go index 9ead634..7efbf2d 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -29,18 +29,30 @@ type Set struct { var itemExists = struct{}{} // NewWith instantiates a new empty set with the custom comparator. -func NewWith(comparator utils.Comparator) *Set { - return &Set{tree: rbt.NewWith(comparator)} +func NewWith(comparator utils.Comparator, values ...interface{}) *Set { + set := &Set{tree: rbt.NewWith(comparator)} + if len(values) > 0 { + set.Add(values...) + } + return set } // NewWithIntComparator instantiates a new empty set with the IntComparator, i.e. keys are of type int. -func NewWithIntComparator() *Set { - return &Set{tree: rbt.NewWithIntComparator()} +func NewWithIntComparator(values ...interface{}) *Set { + set := &Set{tree: rbt.NewWithIntComparator()} + if len(values) > 0 { + set.Add(values...) + } + return set } // NewWithStringComparator instantiates a new empty set with the StringComparator, i.e. keys are of type string. -func NewWithStringComparator() *Set { - return &Set{tree: rbt.NewWithStringComparator()} +func NewWithStringComparator(values ...interface{}) *Set { + set := &Set{tree: rbt.NewWithStringComparator()} + if len(values) > 0 { + set.Add(values...) + } + return set } // Add adds the items (one or more) to the set. diff --git a/sets/treeset/treeset_test.go b/sets/treeset/treeset_test.go index 0dfe39b..b3cfca9 100644 --- a/sets/treeset/treeset_test.go +++ b/sets/treeset/treeset_test.go @@ -9,6 +9,20 @@ import ( "testing" ) +func TestSetNew(t *testing.T) { + set := NewWithIntComparator(2, 1) + if actualValue := set.Size(); actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } + values := set.Values() + if actualValue := values[0]; actualValue != 1 { + t.Errorf("Got %v expected %v", actualValue, 1) + } + if actualValue := values[1]; actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } +} + func TestSetAdd(t *testing.T) { set := NewWithIntComparator() set.Add()