diff --git a/containers/containers_test.go b/containers/containers_test.go index bef88f4..a2da719 100644 --- a/containers/containers_test.go +++ b/containers/containers_test.go @@ -34,28 +34,28 @@ import ( ) // For testing purposes -type Container struct { +type ContainerTest struct { values []interface{} } -func (container Container) Empty() bool { +func (container ContainerTest) Empty() bool { return len(container.values) == 0 } -func (container Container) Size() int { +func (container ContainerTest) Size() int { return len(container.values) } -func (container Container) Clear() { +func (container ContainerTest) Clear() { container.values = []interface{}{} } -func (container Container) Values() []interface{} { +func (container ContainerTest) Values() []interface{} { return container.values } func TestGetSortedValuesInts(t *testing.T) { - container := Container{} + container := ContainerTest{} container.values = []interface{}{5, 1, 3, 2, 4} values := GetSortedValues(container, utils.IntComparator) for i := 1; i < container.Size(); i++ { @@ -66,7 +66,7 @@ func TestGetSortedValuesInts(t *testing.T) { } func TestGetSortedValuesStrings(t *testing.T) { - container := Container{} + container := ContainerTest{} container.values = []interface{}{"g", "a", "d", "e", "f", "c", "b"} values := GetSortedValues(container, utils.StringComparator) for i := 1; i < container.Size(); i++ { diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index ca01d41..bdcf1ad 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -55,19 +55,20 @@ type Iterator struct { } func (list *List) Iterator() Iterator { - return Iterator{list: list, current: 0} + return Iterator{list: list, current: -1} } func (iterator *Iterator) Next() bool { - return false + iterator.current += 1 + return iterator.list.withinRange(iterator.current) } func (iterator *Iterator) Value() interface{} { - return nil + return iterator.list.elements[iterator.current] } func (iterator *Iterator) Index() interface{} { - return nil + return iterator.list.elements[iterator.current] } const ( @@ -108,7 +109,7 @@ func (list *List) Remove(index int) { } list.elements[index] = nil // cleanup reference - copy(list.elements[index:], list.elements[index+1:list.size]) // shift to the left by one (slow operation, need ways to optimize this) + copy(list.elements[index:], list.elements[index + 1:list.size]) // shift to the left by one (slow operation, need ways to optimize this) list.size -= 1 list.shrink() @@ -190,12 +191,12 @@ func (list *List) Insert(index int, values ...interface{}) { 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] + for i := list.size - 1; i >= index + l; i-- { + list.elements[i] = list.elements[i - l] } // Insert new for i, value := range values { - list.elements[index+i] = value + list.elements[index + i] = value } } @@ -275,8 +276,8 @@ func (list *List) resize(cap int) { func (list *List) growBy(n int) { // When capacity is reached, grow by a factor of GROWTH_FACTOR and add number of elements currentCapacity := cap(list.elements) - if list.size+n >= currentCapacity { - newCapacity := int(GROWTH_FACTOR * float32(currentCapacity+n)) + if list.size + n >= currentCapacity { + newCapacity := int(GROWTH_FACTOR * float32(currentCapacity + n)) list.resize(newCapacity) } } @@ -288,7 +289,7 @@ func (list *List) shrink() { } // Shrink when size is at SHRINK_FACTOR * capacity currentCapacity := cap(list.elements) - if list.size <= int(float32(currentCapacity)*SHRINK_FACTOR) { + if list.size <= int(float32(currentCapacity) * SHRINK_FACTOR) { list.resize(list.size) } }