2016-06-27 02:21:09 +00:00
|
|
|
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2015-03-07 16:09:47 +00:00
|
|
|
|
2016-06-25 15:02:21 +00:00
|
|
|
// Package arraylist implements the array list.
|
|
|
|
//
|
2015-03-07 16:09:47 +00:00
|
|
|
// Structure is not thread safe.
|
2016-06-25 15:02:21 +00:00
|
|
|
//
|
|
|
|
// Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
|
2015-03-07 16:09:47 +00:00
|
|
|
package arraylist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/emirpasic/gods/lists"
|
2015-03-08 02:13:26 +00:00
|
|
|
"github.com/emirpasic/gods/utils"
|
2015-03-07 16:09:47 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-06-27 02:02:52 +00:00
|
|
|
func assertListImplementation() {
|
2016-06-22 01:09:48 +00:00
|
|
|
var _ lists.List = (*List)(nil)
|
2015-03-07 16:09:47 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// List holds the elements in a slice
|
2015-03-07 16:09:47 +00:00
|
|
|
type List struct {
|
|
|
|
elements []interface{}
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-06-24 19:52:16 +00:00
|
|
|
growthFactor = float32(2.0) // growth by 100%
|
|
|
|
shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
|
2015-03-07 16:09:47 +00:00
|
|
|
)
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// New instantiates a new empty list
|
2015-03-07 16:09:47 +00:00
|
|
|
func New() *List {
|
|
|
|
return &List{}
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Add appends a value at the end of the list
|
2016-06-21 01:39:47 +00:00
|
|
|
func (list *List) Add(values ...interface{}) {
|
|
|
|
list.growBy(len(values))
|
|
|
|
for _, value := range values {
|
|
|
|
list.elements[list.size] = value
|
2016-06-24 19:52:16 +00:00
|
|
|
list.size++
|
2015-03-07 16:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Get returns the element at index.
|
2015-03-07 16:09:47 +00:00
|
|
|
// Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.
|
|
|
|
func (list *List) Get(index int) (interface{}, bool) {
|
|
|
|
|
|
|
|
if !list.withinRange(index) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.elements[index], true
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Remove removes one or more elements from the list with the supplied indices.
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) Remove(index int) {
|
|
|
|
|
|
|
|
if !list.withinRange(index) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-07 17:26:03 +00:00
|
|
|
list.elements[index] = nil // cleanup reference
|
2016-06-22 02:53:38 +00:00
|
|
|
copy(list.elements[index:], list.elements[index+1:list.size]) // shift to the left by one (slow operation, need ways to optimize this)
|
2016-06-24 19:52:16 +00:00
|
|
|
list.size--
|
2015-03-07 16:09:47 +00:00
|
|
|
|
|
|
|
list.shrink()
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Contains checks if elements (one or more) are present in the set.
|
2015-03-07 16:09:47 +00:00
|
|
|
// All elements have to be present in the set for the method to return true.
|
|
|
|
// Performance time complexity of n^2.
|
|
|
|
// Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.
|
2016-06-21 01:39:47 +00:00
|
|
|
func (list *List) Contains(values ...interface{}) bool {
|
2015-03-07 16:09:47 +00:00
|
|
|
|
2016-06-21 01:39:47 +00:00
|
|
|
for _, searchValue := range values {
|
2015-03-07 16:09:47 +00:00
|
|
|
found := false
|
|
|
|
for _, element := range list.elements {
|
2016-06-21 01:39:47 +00:00
|
|
|
if element == searchValue {
|
2015-03-07 16:09:47 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Values returns all elements in the list.
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) Values() []interface{} {
|
|
|
|
newElements := make([]interface{}, list.size, list.size)
|
|
|
|
copy(newElements, list.elements[:list.size])
|
|
|
|
return newElements
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Empty returns true if list does not contain any elements.
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) Empty() bool {
|
|
|
|
return list.size == 0
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Size returns number of elements within the list.
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) Size() int {
|
|
|
|
return list.size
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Clear removes all elements from the list.
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) Clear() {
|
|
|
|
list.size = 0
|
|
|
|
list.elements = []interface{}{}
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:51:41 +00:00
|
|
|
// Sort sorts values (in-place) using.
|
2015-03-08 02:13:26 +00:00
|
|
|
func (list *List) Sort(comparator utils.Comparator) {
|
|
|
|
if len(list.elements) < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
utils.Sort(list.elements[:list.size], comparator)
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Swap swaps the two values at the specified positions.
|
2015-03-12 23:06:49 +00:00
|
|
|
func (list *List) Swap(i, j int) {
|
|
|
|
if list.withinRange(i) && list.withinRange(j) {
|
|
|
|
list.elements[i], list.elements[j] = list.elements[j], list.elements[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
|
2016-06-21 00:22:20 +00:00
|
|
|
// 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.
|
2016-06-21 01:39:47 +00:00
|
|
|
func (list *List) Insert(index int, values ...interface{}) {
|
2016-06-21 00:22:20 +00:00
|
|
|
|
|
|
|
if !list.withinRange(index) {
|
|
|
|
// Append
|
|
|
|
if index == list.size {
|
2016-06-21 01:39:47 +00:00
|
|
|
list.Add(values...)
|
2016-06-21 00:22:20 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-21 01:39:47 +00:00
|
|
|
l := len(values)
|
2016-06-21 00:22:20 +00:00
|
|
|
list.growBy(l)
|
|
|
|
list.size += l
|
|
|
|
// Shift old to right
|
2016-06-22 02:53:38 +00:00
|
|
|
for i := list.size - 1; i >= index+l; i-- {
|
|
|
|
list.elements[i] = list.elements[i-l]
|
2016-06-21 00:22:20 +00:00
|
|
|
}
|
|
|
|
// Insert new
|
2016-06-21 01:39:47 +00:00
|
|
|
for i, value := range values {
|
2016-06-22 02:53:38 +00:00
|
|
|
list.elements[index+i] = value
|
2016-06-21 00:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// String returns a string representation of container
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) String() string {
|
|
|
|
str := "ArrayList\n"
|
|
|
|
values := []string{}
|
2015-03-08 02:13:26 +00:00
|
|
|
for _, value := range list.elements[:list.size] {
|
2015-03-07 16:09:47 +00:00
|
|
|
values = append(values, fmt.Sprintf("%v", value))
|
|
|
|
}
|
|
|
|
str += strings.Join(values, ", ")
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2016-06-24 18:27:34 +00:00
|
|
|
// Check that the index is within bounds of the list
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) withinRange(index int) bool {
|
2016-06-21 00:22:20 +00:00
|
|
|
return index >= 0 && index < list.size
|
2015-03-07 16:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (list *List) resize(cap int) {
|
|
|
|
newElements := make([]interface{}, cap, cap)
|
|
|
|
copy(newElements, list.elements)
|
|
|
|
list.elements = newElements
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand the array if necessary, i.e. capacity will be reached if we add n elements
|
|
|
|
func (list *List) growBy(n int) {
|
2016-06-24 19:52:16 +00:00
|
|
|
// When capacity is reached, grow by a factor of growthFactor and add number of elements
|
2015-03-07 16:09:47 +00:00
|
|
|
currentCapacity := cap(list.elements)
|
2016-06-22 02:53:38 +00:00
|
|
|
if list.size+n >= currentCapacity {
|
2016-06-24 19:52:16 +00:00
|
|
|
newCapacity := int(growthFactor * float32(currentCapacity+n))
|
2015-03-07 16:09:47 +00:00
|
|
|
list.resize(newCapacity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// Shrink the array if necessary, i.e. when size is shrinkFactor percent of current capacity
|
2015-03-07 16:09:47 +00:00
|
|
|
func (list *List) shrink() {
|
2016-06-24 19:52:16 +00:00
|
|
|
if shrinkFactor == 0.0 {
|
2015-03-07 16:09:47 +00:00
|
|
|
return
|
|
|
|
}
|
2016-06-24 19:52:16 +00:00
|
|
|
// Shrink when size is at shrinkFactor * capacity
|
2015-03-07 16:09:47 +00:00
|
|
|
currentCapacity := cap(list.elements)
|
2016-06-24 19:52:16 +00:00
|
|
|
if list.size <= int(float32(currentCapacity)*shrinkFactor) {
|
2015-03-07 16:09:47 +00:00
|
|
|
list.resize(list.size)
|
|
|
|
}
|
|
|
|
}
|