From 12451bdcc697ceb2d3e8bfbefff6036fc2b9b889 Mon Sep 17 00:00:00 2001 From: Spriithy Date: Thu, 28 Sep 2017 14:22:00 +0200 Subject: [PATCH 1/2] Added bulk constructors for arraylists & (doubly)-linked-lists --- lists/arraylist/arraylist.go | 10 +++++++++- lists/doublylinkedlist/doublylinkedlist.go | 10 +++++++++- lists/singlylinkedlist/singlylinkedlist.go | 10 +++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index f9e3543..c1af31f 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -11,9 +11,10 @@ package arraylist import ( "fmt" + "strings" + "github.com/emirpasic/gods/lists" "github.com/emirpasic/gods/utils" - "strings" ) func assertListImplementation() { @@ -36,6 +37,13 @@ func New() *List { return &List{} } +// Of instantiates a new list of the given values +func Of(values ...interface{}) *List { + list := New() + list.Add(values) + return list +} + // Add appends a value at the end of the list func (list *List) Add(values ...interface{}) { list.growBy(len(values)) diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index bd85b18..51e4aee 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -11,9 +11,10 @@ package doublylinkedlist import ( "fmt" + "strings" + "github.com/emirpasic/gods/lists" "github.com/emirpasic/gods/utils" - "strings" ) func assertListImplementation() { @@ -38,6 +39,13 @@ func New() *List { return &List{} } +// Of instantiates a new list of the given values +func Of(values ...interface{}) *List { + list := New() + list.Add(values) + return list +} + // Add appends a value (one or more) at the end of the list (same as Append()) func (list *List) Add(values ...interface{}) { for _, value := range values { diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index 139925b..d48afc8 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -11,9 +11,10 @@ package singlylinkedlist import ( "fmt" + "strings" + "github.com/emirpasic/gods/lists" "github.com/emirpasic/gods/utils" - "strings" ) func assertListImplementation() { @@ -37,6 +38,13 @@ func New() *List { return &List{} } +// Of instantiates a new list of the given values +func Of(values ...interface{}) *List { + list := New() + list.Add(values) + return list +} + // Add appends a value (one or more) at the end of the list (same as Append()) func (list *List) Add(values ...interface{}) { for _, value := range values { From c6630349c445d3a0bd1dda57ac17bfcb9b308e2d Mon Sep 17 00:00:00 2001 From: emirpasic Date: Thu, 20 Sep 2018 18:40:32 +0200 Subject: [PATCH 2/2] - bulk initialization for lists --- lists/arraylist/arraylist.go | 15 ++++------ lists/arraylist/arraylist_test.go | 29 +++++++++++++++++-- lists/doublylinkedlist/doublylinkedlist.go | 15 ++++------ .../doublylinkedlist/doublylinkedlist_test.go | 26 +++++++++++++++++ lists/singlylinkedlist/singlylinkedlist.go | 15 ++++------ .../singlylinkedlist/singlylinkedlist_test.go | 26 +++++++++++++++++ 6 files changed, 97 insertions(+), 29 deletions(-) diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index 34eeb4e..15fe5f6 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -32,15 +32,12 @@ const ( shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink) ) -// New instantiates a new empty list -func New() *List { - return &List{} -} - -// Of instantiates a new list of the given values -func Of(values ...interface{}) *List { - list := New() - list.Add(values) +// New instantiates a new list and adds the passed values, if any, to the list +func New(values ...interface{}) *List { + list := &List{} + if len(values) > 0 { + list.Add(values...) + } return list } diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index ced20c5..0d7eae4 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -6,11 +6,36 @@ package arraylist import ( "fmt" - "testing" - "github.com/emirpasic/gods/utils" + "testing" ) +func TestListNew(t *testing.T) { + list1 := New() + + if actualValue := list1.Empty(); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } + + list2 := New(1, "b") + + if actualValue := list2.Size(); actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } + + if actualValue, ok := list2.Get(0); actualValue != 1 || !ok { + t.Errorf("Got %v expected %v", actualValue, 1) + } + + if actualValue, ok := list2.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } + + if actualValue, ok := list2.Get(2); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } +} + func TestListAdd(t *testing.T) { list := New() list.Add("a") diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index 5daf5eb..be28251 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -34,15 +34,12 @@ type element struct { next *element } -// New instantiates a new empty list -func New() *List { - return &List{} -} - -// Of instantiates a new list of the given values -func Of(values ...interface{}) *List { - list := New() - list.Add(values) +// New instantiates a new list and adds the passed values, if any, to the list +func New(values ...interface{}) *List { + list := &List{} + if len(values) > 0 { + list.Add(values...) + } return list } diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 1f0d5a6..596bd2e 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -11,6 +11,32 @@ import ( "github.com/emirpasic/gods/utils" ) +func TestListNew(t *testing.T) { + list1 := New() + + if actualValue := list1.Empty(); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } + + list2 := New(1, "b") + + if actualValue := list2.Size(); actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } + + if actualValue, ok := list2.Get(0); actualValue != 1 || !ok { + t.Errorf("Got %v expected %v", actualValue, 1) + } + + if actualValue, ok := list2.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } + + if actualValue, ok := list2.Get(2); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } +} + func TestListAdd(t *testing.T) { list := New() list.Add("a") diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index 86e58a9..803d70c 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -33,15 +33,12 @@ type element struct { next *element } -// New instantiates a new empty list -func New() *List { - return &List{} -} - -// Of instantiates a new list of the given values -func Of(values ...interface{}) *List { - list := New() - list.Add(values) +// New instantiates a new list and adds the passed values, if any, to the list +func New(values ...interface{}) *List { + list := &List{} + if len(values) > 0 { + list.Add(values...) + } return list } diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index de1cd0d..9b6fe59 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -11,6 +11,32 @@ import ( "github.com/emirpasic/gods/utils" ) +func TestListNew(t *testing.T) { + list1 := New() + + if actualValue := list1.Empty(); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } + + list2 := New(1, "b") + + if actualValue := list2.Size(); actualValue != 2 { + t.Errorf("Got %v expected %v", actualValue, 2) + } + + if actualValue, ok := list2.Get(0); actualValue != 1 || !ok { + t.Errorf("Got %v expected %v", actualValue, 1) + } + + if actualValue, ok := list2.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "b") + } + + if actualValue, ok := list2.Get(2); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } +} + func TestListAdd(t *testing.T) { list := New() list.Add("a")