mirror of
https://github.com/emirpasic/gods
synced 2024-11-04 18:00:21 +00:00
- update all lists to use "value" terminology for coherence, e.g. Add(values...) rather than Add(elements...)
This commit is contained in:
parent
dd0fbaa365
commit
56b8a59488
12
README.md
12
README.md
@ -126,7 +126,7 @@ func main() {
|
||||
|
||||
####Lists
|
||||
|
||||
A list is a data structure that can store elements and may have repeated values. There is no ordering in a list. The user can access and remove an element by the index position.
|
||||
A list is a data structure that can store values and may have repeated values. There is no ordering in a list. The user can access and remove a value by the index position.
|
||||
|
||||
All lists implement the list interface with the following methods:
|
||||
|
||||
@ -134,11 +134,11 @@ All lists implement the list interface with the following methods:
|
||||
type Interface interface {
|
||||
Get(index int) (interface{}, bool)
|
||||
Remove(index int)
|
||||
Add(elements ...interface{})
|
||||
Contains(elements ...interface{}) bool
|
||||
Add(values ...interface{})
|
||||
Contains(values ...interface{}) bool
|
||||
Sort(comparator utils.Comparator)
|
||||
Swap(index1, index2 int)
|
||||
Insert(index int, elements ...interface{})
|
||||
Insert(index int, values ...interface{})
|
||||
|
||||
containers.Interface
|
||||
// Empty() bool
|
||||
@ -188,7 +188,7 @@ func main() {
|
||||
|
||||
#####SinglyLinkedList
|
||||
|
||||
This structure implements the _List_ interface and is a linked data structure where each element points to the next in the list.
|
||||
This structure implements the _List_ interface and is a linked data structure where each value points to the next in the list.
|
||||
|
||||
Direct access method _Get(index)_ and _Remove()_ are of linear performance. _Append_ and _Prepend_ are of constant time performance. Checking with _Contains()_ is of quadratic complexity.
|
||||
|
||||
@ -225,7 +225,7 @@ func main() {
|
||||
|
||||
#####DoublyLinkedList
|
||||
|
||||
This structure implements the _List_ interface and is a linked data structure where each element points to the next and previous element in the list.
|
||||
This structure implements the _List_ interface and is a linked data structure where each value points to the next and previous element in the list.
|
||||
|
||||
Direct access method _Get(index)_ and _Remove()_ are of linear performance. _Append_ and _Prepend_ are of constant time performance. Checking with _Contains()_ is of quadratic complexity.
|
||||
|
||||
|
@ -57,10 +57,10 @@ func New() *List {
|
||||
}
|
||||
|
||||
// Appends a value at the end of the list
|
||||
func (list *List) Add(elements ...interface{}) {
|
||||
list.growBy(len(elements))
|
||||
for _, element := range elements {
|
||||
list.elements[list.size] = element
|
||||
func (list *List) Add(values ...interface{}) {
|
||||
list.growBy(len(values))
|
||||
for _, value := range values {
|
||||
list.elements[list.size] = value
|
||||
list.size += 1
|
||||
}
|
||||
}
|
||||
@ -94,12 +94,12 @@ func (list *List) Remove(index int) {
|
||||
// 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.
|
||||
func (list *List) Contains(elements ...interface{}) bool {
|
||||
func (list *List) Contains(values ...interface{}) bool {
|
||||
|
||||
for _, searchElement := range elements {
|
||||
for _, searchValue := range values {
|
||||
found := false
|
||||
for _, element := range list.elements {
|
||||
if element == searchElement {
|
||||
if element == searchValue {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
@ -142,7 +142,7 @@ func (list *List) Sort(comparator utils.Comparator) {
|
||||
utils.Sort(list.elements[:list.size], comparator)
|
||||
}
|
||||
|
||||
// Swaps values of two elements at the given indices.
|
||||
// Swaps the two values at the specified positions.
|
||||
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]
|
||||
@ -152,17 +152,17 @@ 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{}) {
|
||||
func (list *List) Insert(index int, values ...interface{}) {
|
||||
|
||||
if !list.withinRange(index) {
|
||||
// Append
|
||||
if index == list.size {
|
||||
list.Add(elements...)
|
||||
list.Add(values...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
l := len(elements)
|
||||
l := len(values)
|
||||
list.growBy(l)
|
||||
list.size += l
|
||||
// Shift old to right
|
||||
@ -170,8 +170,8 @@ func (list *List) Insert(index int, elements ...interface{}) {
|
||||
list.elements[i] = list.elements[i-l]
|
||||
}
|
||||
// Insert new
|
||||
for i, element := range elements {
|
||||
list.elements[index+i] = element
|
||||
for i, value := range values {
|
||||
list.elements[index+i] = value
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -310,5 +310,5 @@ 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
|
||||
}
|
||||
|
@ -26,11 +26,11 @@ import (
|
||||
type Interface interface {
|
||||
Get(index int) (interface{}, bool)
|
||||
Remove(index int)
|
||||
Add(elements ...interface{})
|
||||
Contains(elements ...interface{}) bool
|
||||
Add(values ...interface{})
|
||||
Contains(values ...interface{}) bool
|
||||
Sort(comparator utils.Comparator)
|
||||
Swap(index1, index2 int)
|
||||
Insert(index int, elements ...interface{})
|
||||
Insert(index int, values ...interface{})
|
||||
|
||||
containers.Interface
|
||||
// Empty() bool
|
||||
|
Loading…
Reference in New Issue
Block a user