github.com/tursom/GoCollections@v0.3.10/collections/ArrayList.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package collections
     8  
     9  import (
    10  	"github.com/tursom/GoCollections/exceptions"
    11  	"github.com/tursom/GoCollections/lang"
    12  )
    13  
    14  type (
    15  	ArrayList[T lang.Object] struct {
    16  		lang.BaseObject
    17  		array []T
    18  	}
    19  	arrayListIterator[T lang.Object] struct {
    20  		arrayList *ArrayList[T]
    21  		index     int
    22  	}
    23  )
    24  
    25  func NewArrayList[T lang.Object]() *ArrayList[T] {
    26  	return NewArrayListByCapacity[T](16)
    27  }
    28  
    29  func NewArrayListByCapacity[T lang.Object](cap int) *ArrayList[T] {
    30  	return &ArrayList[T]{
    31  		BaseObject: lang.NewBaseObject(),
    32  		array:      make([]T, 0, cap),
    33  	}
    34  }
    35  
    36  // NewArrayListFrom create a new ArrayList from list by index from [from] until [to]
    37  func NewArrayListFrom[T lang.Object](list List[T], from, to int) *ArrayList[T] {
    38  	newList := NewArrayListByCapacity[T](to - from)
    39  	iterator, err := SkipIterator[T](list.ListIterator(), to-from)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	for i := 0; i < to-from; i++ {
    45  		next, err := iterator.Next()
    46  		if err != nil {
    47  			panic(err)
    48  		}
    49  
    50  		// newList wont throw any exception in this place
    51  		_ = newList.Add(next)
    52  	}
    53  
    54  	return newList
    55  }
    56  
    57  func (a *ArrayList[T]) String() string {
    58  	return String[T](a)
    59  }
    60  
    61  func (a *ArrayList[T]) Iterator() Iterator[T] {
    62  	return a.MutableIterator()
    63  }
    64  
    65  func (a *ArrayList[T]) ListIterator() ListIterator[T] {
    66  	return a.MutableListIterator()
    67  }
    68  
    69  func (a *ArrayList[T]) MutableListIterator() MutableListIterator[T] {
    70  	return &arrayListIterator[T]{a, 0}
    71  }
    72  
    73  func (a *ArrayList[T]) Size() int {
    74  	return lang.Len(a.array)
    75  }
    76  
    77  func (a *ArrayList[T]) IsEmpty() bool {
    78  	return a.Size() == 0
    79  }
    80  
    81  func (a *ArrayList[T]) Contains(element T) bool {
    82  	return Contains[T](a, element)
    83  }
    84  
    85  func (a *ArrayList[T]) ContainsAll(c Collection[T]) bool {
    86  	return ContainsAll[T](a, c)
    87  }
    88  
    89  func (a *ArrayList[T]) Add(element T) bool {
    90  	a.array = lang.Append(a.array, element)
    91  	return true
    92  }
    93  
    94  func (a *ArrayList[T]) IndexOf(element T) int {
    95  	for i := 0; i < a.Size(); i++ {
    96  		if lang.Equals(element, a.array[i]) {
    97  			return i
    98  		}
    99  	}
   100  	return -1
   101  }
   102  
   103  func (a *ArrayList[T]) Remove(element T) exceptions.Exception {
   104  	index := a.IndexOf(element)
   105  	if index < 0 {
   106  		return exceptions.NewElementNotFoundException("", nil)
   107  	} else {
   108  		return a.RemoveAt(index)
   109  	}
   110  }
   111  
   112  func (a *ArrayList[T]) AddAll(c Collection[T]) bool {
   113  	return AddAll[T](a, c)
   114  }
   115  
   116  func (a *ArrayList[T]) RemoveAll(c Collection[T]) bool {
   117  	return RemoveAll[T](a, c)
   118  }
   119  
   120  func (a *ArrayList[T]) RetainAll(c Collection[T]) bool {
   121  	return RetainAll[T](a, c)
   122  }
   123  
   124  func (a *ArrayList[T]) Clear() {
   125  	a.array = []T{}
   126  }
   127  
   128  func (a *ArrayList[T]) Get(index int) (T, exceptions.Exception) {
   129  	if index >= a.Size() {
   130  		return lang.Nil[T](), exceptions.NewIndexOutOfBound("", nil)
   131  	} else {
   132  		return a.array[index], nil
   133  	}
   134  }
   135  
   136  func (a *ArrayList[T]) SubList(from, to int) List[T] {
   137  	return a.SubMutableList(from, to)
   138  }
   139  
   140  func (a *ArrayList[T]) Set(index int, element T) exceptions.Exception {
   141  	if index >= a.Size() {
   142  		return exceptions.NewIndexOutOfBound("", nil)
   143  	}
   144  	a.array[index] = element
   145  	return nil
   146  }
   147  
   148  func (a *ArrayList[T]) AddAtIndex(index int, element T) bool {
   149  	if index >= a.Size() {
   150  		return false
   151  	}
   152  
   153  	array := a.array
   154  	a.array = lang.Append[T](array[:index], element)
   155  	a.array = lang.Append[T](a.array, array[index:]...)
   156  	return true
   157  }
   158  
   159  func (a *ArrayList[T]) RemoveAt(index int) exceptions.Exception {
   160  	if index >= a.Size() {
   161  		return exceptions.NewIndexOutOfBound("", nil)
   162  	}
   163  
   164  	a.array = lang.Append[T](a.array[:index], a.array[index+1:]...)
   165  	return nil
   166  }
   167  
   168  func (a *ArrayList[T]) SubMutableList(from, to int) MutableList[T] {
   169  	return &ArrayList[T]{
   170  		array: a.array[from:to],
   171  	}
   172  }
   173  
   174  func (a *ArrayList[T]) MutableIterator() MutableIterator[T] {
   175  	return &arrayListIterator[T]{a, 0}
   176  }
   177  
   178  func (a *ArrayList[T]) RemoveLast() (T, exceptions.Exception) {
   179  	v, _ := a.Get(a.Size() - 1)
   180  	return v, a.RemoveAt(a.Size() - 1)
   181  }
   182  
   183  func (a *arrayListIterator[T]) HasNext() bool {
   184  	return a.index < a.arrayList.Size()
   185  }
   186  
   187  func (a *arrayListIterator[T]) Next() (T, exceptions.Exception) {
   188  	value, err := a.arrayList.Get(a.index)
   189  	if err != nil {
   190  		return lang.Nil[T](), err
   191  	}
   192  	a.index++
   193  	return value, nil
   194  }
   195  
   196  func (a *arrayListIterator[T]) Remove() exceptions.Exception {
   197  	err := a.arrayList.RemoveAt(a.index - 1)
   198  	if err != nil {
   199  		return err
   200  	}
   201  	a.index--
   202  	return nil
   203  }
   204  
   205  func (a *arrayListIterator[T]) HasPrevious() bool {
   206  	return a.index > 0
   207  }
   208  
   209  func (a *arrayListIterator[T]) Previous() (T, exceptions.Exception) {
   210  	if a.index <= 0 || a.index >= len(a.arrayList.array) {
   211  		return lang.Nil[T](), exceptions.NewIndexOutOfBound("", nil)
   212  	}
   213  	a.index--
   214  	return a.arrayList.array[a.index], a.arrayList.RemoveAt(a.index)
   215  }
   216  
   217  func (a *arrayListIterator[T]) NextIndex() int {
   218  	return a.index
   219  }
   220  
   221  func (a *arrayListIterator[T]) PreviousIndex() int {
   222  	return a.index - 1
   223  }
   224  
   225  func (a *arrayListIterator[T]) Set(value T) exceptions.Exception {
   226  	if a.index <= 0 {
   227  		return exceptions.NewIndexOutOfBound("", nil)
   228  	}
   229  	a.arrayList.array[a.index-1] = value
   230  	return nil
   231  }
   232  
   233  func (a *arrayListIterator[T]) Add(value T) exceptions.Exception {
   234  	a.arrayList.AddAtIndex(a.index, value)
   235  	a.index++
   236  	return nil
   237  }