GoCollections/collections/ArrayList.go

177 lines
3.3 KiB
Go
Raw Normal View History

2021-05-21 09:41:58 +08:00
package collections
2022-03-21 11:02:41 +08:00
import (
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang"
)
2021-05-21 09:41:58 +08:00
2022-03-21 11:02:41 +08:00
type ArrayList[T lang.Object] struct {
array []T
used int
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func NewArrayList[T lang.Object]() *ArrayList[T] {
return &ArrayList[T]{
make([]T, 16),
2021-05-21 09:41:58 +08:00
0,
}
}
2022-03-21 11:02:41 +08:00
func NewArrayListByCapacity[T lang.Object](cap int) *ArrayList[T] {
return &ArrayList[T]{
make([]T, cap),
2021-05-22 17:10:26 +08:00
0,
}
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) String() string {
return String[T](a)
2021-05-21 10:31:49 +08:00
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) Iterator() Iterator[T] {
2021-05-21 09:41:58 +08:00
return a.MutableIterator()
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) Size() int {
2021-05-21 09:41:58 +08:00
return a.used
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) IsEmpty() bool {
2021-05-21 09:41:58 +08:00
return a.Size() == 0
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) Contains(element T) bool {
return Contains[T](a, element)
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) ContainsAll(c Collection[T]) bool {
return ContainsAll[T](a, c)
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) Add(element T) bool {
if a.used >= len(a.array) {
2021-05-21 09:41:58 +08:00
oldArray := a.array
2022-03-21 11:02:41 +08:00
a.array = make([]T, a.used*2)
2021-05-21 09:41:58 +08:00
copy(a.array, oldArray)
}
a.array[a.used] = element
a.used++
return true
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) IndexOf(element T) int {
for i := 0; i < a.used; i++ {
if lang.Equals(element, a.array[i]) {
2021-05-21 09:41:58 +08:00
return int(i)
}
}
return -1
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) Remove(element T) exceptions.Exception {
2021-05-21 09:41:58 +08:00
index := a.IndexOf(element)
if index < 0 {
2022-03-21 11:02:41 +08:00
return exceptions.NewElementNotFoundException("", nil)
2021-05-21 09:41:58 +08:00
} else {
2022-03-21 11:02:41 +08:00
return a.RemoveAt(int(index))
2021-05-21 09:41:58 +08:00
}
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) AddAll(c Collection[T]) bool {
return AddAll[T](a, c)
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) RemoveAll(c Collection[T]) bool {
return RemoveAll[T](a, c)
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) RetainAll(c Collection[T]) bool {
return RetainAll[T](a, c)
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) Clear() {
2021-05-21 09:41:58 +08:00
a.used = 0
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) Get(index int) (T, exceptions.Exception) {
2021-05-21 09:41:58 +08:00
if index >= a.used {
2022-03-21 11:02:41 +08:00
return lang.Nil[T](), exceptions.NewIndexOutOfBound("", nil)
2021-05-21 09:41:58 +08:00
} else {
return a.array[index], nil
}
}
2022-03-21 11:02:41 +08:00
func (a ArrayList[T]) SubList(from, to int) List[T] {
return NewSubList[T](a, from, to)
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) Set(index int, element T) exceptions.Exception {
2021-05-21 09:41:58 +08:00
if index >= a.used {
2022-03-21 11:02:41 +08:00
return exceptions.NewIndexOutOfBound("", nil)
2021-05-21 09:41:58 +08:00
}
a.array[index] = element
return nil
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) AddAtIndex(index int, element T) bool {
2021-05-21 10:31:49 +08:00
if !a.Add(element) {
return false
}
array := a.array
for i := a.used - 1; i > index; i++ {
array[i] = array[i-1]
}
array[index] = element
a.used++
return true
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) RemoveAt(index int) exceptions.Exception {
2021-05-21 10:31:49 +08:00
if index >= a.used {
2022-03-21 11:02:41 +08:00
return exceptions.NewIndexOutOfBound("", nil)
2021-05-21 10:31:49 +08:00
}
array := a.array
for i := index + 1; i < a.used; i++ {
array[i-1] = array[i]
}
a.used--
return nil
2021-05-21 09:41:58 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) SubMutableList(from, to int) MutableList[T] {
2021-05-21 09:41:58 +08:00
panic("implement me")
}
2021-05-21 10:31:49 +08:00
2022-03-21 11:02:41 +08:00
func (a *ArrayList[T]) MutableIterator() MutableIterator[T] {
return &arrayListIterator[T]{a, 0}
2021-05-21 10:31:49 +08:00
}
2022-03-21 11:02:41 +08:00
type arrayListIterator[T lang.Object] struct {
arrayList *ArrayList[T]
index int
2021-05-21 10:31:49 +08:00
}
2022-03-21 11:02:41 +08:00
func (a *arrayListIterator[T]) HasNext() bool {
2021-05-21 10:31:49 +08:00
return a.index < a.arrayList.used
}
2022-03-21 11:02:41 +08:00
func (a *arrayListIterator[T]) Next() (T, exceptions.Exception) {
2021-05-21 10:31:49 +08:00
value, err := a.arrayList.Get(a.index)
if err != nil {
2022-03-21 11:02:41 +08:00
return lang.Nil[T](), err
2021-05-21 10:31:49 +08:00
}
a.index++
return value, nil
}
2022-03-21 11:02:41 +08:00
func (a *arrayListIterator[T]) Remove() exceptions.Exception {
2021-05-21 10:31:49 +08:00
err := a.arrayList.RemoveAt(a.index - 1)
if err != nil {
return err
}
a.index--
return nil
}