diff --git a/README.md b/README.md index 443c37d..4095445 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ type Interface interface { Contains(elements ...interface{}) bool Sort(comparator utils.Comparator) Swap(index1, index2 int) + Insert(index int, elements ...interface{}) containers.Interface // Empty() bool @@ -180,6 +181,8 @@ func main() { _ = list.Size() // 0 list.Add("a") // ["a"] list.Clear() // [] + list.Insert(0, "b") // ["b"] + list.Insert(0, "a") // ["a","b"] } ``` diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index f98b5e5..c3b9f0f 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -149,6 +149,32 @@ func (list *List) Swap(i, j int) { } } +// Inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right. +// 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) Insert(index int, elements ...interface{}) { + + if !list.withinRange(index) { + // Append + if index == list.size { + list.Add(elements...) + } + return + } + + l := len(elements) + list.growBy(l) + list.size += l + // Shift old to right + for i := list.size - 1; i >= index+l; i-- { + list.elements[i] = list.elements[i-l] + } + // Insert new + for i, element := range elements { + list.elements[index+i] = element + } +} + func (list *List) String() string { str := "ArrayList\n" values := []string{} @@ -161,7 +187,7 @@ func (list *List) String() string { // Check that the index is withing bounds of the list func (list *List) withinRange(index int) bool { - return index >= 0 && index < list.size && list.size != 0 + return index >= 0 && index < list.size } func (list *List) resize(cap int) { diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index 4eb239f..ab07f67 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -27,6 +27,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package arraylist import ( + "fmt" "github.com/emirpasic/gods/utils" "testing" ) @@ -120,6 +121,19 @@ func TestArrayList(t *testing.T) { t.Errorf("Got %v expected %v", actualValue, true) } + list.Insert(0, "h") + list.Insert(0, "e") + list.Insert(1, "f") + list.Insert(2, "g") + list.Insert(4, "i") + list.Insert(0, "a", "b") + list.Insert(list.Size(), "j", "k") + list.Insert(2, "c", "d") + + if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s%s", list.Values()...), "abcdefghijk"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + } func BenchmarkArrayList(b *testing.B) { diff --git a/lists/lists.go b/lists/lists.go index 7e03285..7de3cb0 100644 --- a/lists/lists.go +++ b/lists/lists.go @@ -30,6 +30,7 @@ type Interface interface { Contains(elements ...interface{}) bool Sort(comparator utils.Comparator) Swap(index1, index2 int) + Insert(index int, elements ...interface{}) containers.Interface // Empty() bool