Merge pull request #87 from bminer/master

Added `Set(index, value)` method to all Lists (closes #86)
pull/88/head
Emir Pasic 6 years ago committed by GitHub
commit a6e6101bb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,9 +11,10 @@ package arraylist
import (
"fmt"
"strings"
"github.com/emirpasic/gods/lists"
"github.com/emirpasic/gods/utils"
"strings"
)
func assertListImplementation() {
@ -162,6 +163,22 @@ func (list *List) Insert(index int, values ...interface{}) {
copy(list.elements[index:], values)
}
// Set the value at specified index
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List) Set(index int, value interface{}) {
if !list.withinRange(index) {
// Append
if index == list.size {
list.Add(value)
}
return
}
list.elements[index] = value
}
// String returns a string representation of container
func (list *List) String() string {
str := "ArrayList\n"

@ -178,6 +178,27 @@ func TestListInsert(t *testing.T) {
}
}
func TestListSet(t *testing.T) {
list := New()
list.Set(0, "a")
list.Set(1, "b")
if actualValue := list.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
list.Set(2, "c") // append
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
list.Set(4, "d") // ignore
list.Set(1, "bb") // update
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abbc"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListEach(t *testing.T) {
list := New()
list.Add("a", "b", "c")

@ -11,9 +11,10 @@ package doublylinkedlist
import (
"fmt"
"strings"
"github.com/emirpasic/gods/lists"
"github.com/emirpasic/gods/utils"
"strings"
)
func assertListImplementation() {
@ -292,6 +293,37 @@ func (list *List) Insert(index int, values ...interface{}) {
}
}
// Sets value at specified index position
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List) Set(index int, value interface{}) {
if !list.withinRange(index) {
// Append
if index == list.size {
list.Add(value)
}
return
}
var foundElement *element
// determine traversal direction, last to first or first to last
if list.size-index < index {
foundElement = list.last
for e := list.size - 1; e != index; {
fmt.Println("Set last", index, value, foundElement, foundElement.prev)
e, foundElement = e-1, foundElement.prev
}
} else {
foundElement = list.first
for e := 0; e != index; {
e, foundElement = e+1, foundElement.next
}
}
foundElement.value = value
}
// String returns a string representation of container
func (list *List) String() string {
str := "DoublyLinkedList\n"

@ -6,8 +6,9 @@ package doublylinkedlist
import (
"fmt"
"github.com/emirpasic/gods/utils"
"testing"
"github.com/emirpasic/gods/utils"
)
func TestListAdd(t *testing.T) {
@ -177,6 +178,32 @@ func TestListInsert(t *testing.T) {
}
}
func TestListSet(t *testing.T) {
list := New()
list.Set(0, "a")
list.Set(1, "b")
if actualValue := list.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
list.Set(2, "c") // append
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
list.Set(4, "d") // ignore
list.Set(1, "bb") // update
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abbc"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
list.Set(2, "cc") // last to first traversal
list.Set(0, "aa") // first to last traversal
if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "aabbcc"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListEach(t *testing.T) {
list := New()
list.Add("a", "b", "c")

@ -23,6 +23,7 @@ type List interface {
Sort(comparator utils.Comparator)
Swap(index1, index2 int)
Insert(index int, values ...interface{})
Set(index int, value interface{})
containers.Container
// Empty() bool

@ -11,9 +11,10 @@ package singlylinkedlist
import (
"fmt"
"strings"
"github.com/emirpasic/gods/lists"
"github.com/emirpasic/gods/utils"
"strings"
)
func assertListImplementation() {
@ -260,6 +261,26 @@ func (list *List) Insert(index int, values ...interface{}) {
}
}
// Sets value at specified index
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List) Set(index int, value interface{}) {
if !list.withinRange(index) {
// Append
if index == list.size {
list.Add(value)
}
return
}
foundElement := list.first
for e := 0; e != index; {
e, foundElement = e+1, foundElement.next
}
foundElement.value = value
}
// String returns a string representation of container
func (list *List) String() string {
str := "SinglyLinkedList\n"

@ -6,8 +6,9 @@ package singlylinkedlist
import (
"fmt"
"github.com/emirpasic/gods/utils"
"testing"
"github.com/emirpasic/gods/utils"
)
func TestListAdd(t *testing.T) {
@ -177,6 +178,27 @@ func TestListInsert(t *testing.T) {
}
}
func TestListSet(t *testing.T) {
list := New()
list.Set(0, "a")
list.Set(1, "b")
if actualValue := list.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
list.Set(2, "c") // append
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
list.Set(4, "d") // ignore
list.Set(1, "bb") // update
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abbc"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListEach(t *testing.T) {
list := New()
list.Add("a", "b", "c")

Loading…
Cancel
Save