[Gambas-user] Would an ArrayList datatype be helpful for gambas users?

Martin Fischer martin.fischer6 at web.de
Sun Sep 10 19:04:16 CEST 2023


Hi all,

I was quite surprised that gambas does not provide a general-purpose
list implementation (I know, there is data.List, but that is not general
purpose I think...)
So I created one:

--------------------------
### A list implementation backed by an array of Variant.

An ArrayList can keep up to MAX_INT entries.\
It grows automatically if the initial capacity of the backing array is
exceeded.\
It will also shrink if you remove existing elements.

Iteration-order is defined as insertion-order (for the classical "For"
loop as well as for the "For-Each" loop).\

Usually this implementation is space and time-efficient and cache-friendly:
- Add, Clear, Slice, SliceOfSize: O(1)
- GetAt, SetAt: O(1)
- Contains, Remove, RemoveAt, AsArray: O(n)

Note that elements of this list can be accessed with the
array-index-access operator ([]).

This class is Serializable.

Usage Example:
     Dim list As ArrayList = New ArrayList(10) ' optional initial
capacity: 10

     list.Add("e1") ' add some elements
     list.Add("e2")
     list.Add("e3")
     list.AddAll("e4", "e5", "e6") ' add many elements explicitly
     list.AddAllFromEnumerable(list2) ' add all elements from anything
that can be traversed via For-Each loop, e.g an ArrayList or an array

     Dim specialElement = list[0] ' retrieve elements of list with
array-index access
     list[1] = "e2_new" ' existing list elements can be changed

     For Each elem As String In list ' classic "For" and "For In" loops
are supported
        process(elem)
     Next

     Dim sublist As SlicedArrayList = list.Slice(2, 4) ' get a sublist
as view of original list. No copying involved here

     list.Sort(gb.Descent) ' sort the list in-place


What do you think?


____________________________________
Martin Fischer


More information about the User mailing list