List

Use to manage dynamic arrays that frequently change lengths

Provides more functionality for arrays, like searching in arrays, removing elements...

Constructors

this
this(uinteger extraCount)

constructor

Members

Functions

append
void append(T dat)

appends an element to the list

append
void append(T[] dat)

appends an array to the list

clear
void clear()

empties the list

clearFreeSpace
void clearFreeSpace()

removes the free space, if any, for adding new elements. Call this when done with adding to list.

indexOf
integer indexOf(T dat, integer i)
insert
bool insert(uinteger index, T[] dat)

Inserts an array into this list

insert
bool insert(uinteger index, T dat)

Inserts an element into this list

loadArray
void loadArray(T[] newList)

Loads list from an array

read
T read()

Reads an element at index=seek. Seek is increased by one

read
uinteger read(T[] buffer)

Reads a number of elements starting at index=seek. Seek is increased by number of elements

read
T read(uinteger index)

Reads an element at an index

read
T[] read(uinteger index, uinteger i2)

Read a slice from the list.

readLast
T readLast()

Reads the last element in list.

readLast
T[] readLast(uinteger count)

Reads number of elements from end of list

readPtr
T* readPtr(uinteger index)
remove
bool remove(uinteger index, uinteger count)

Removes last elements(s) starting from an index

removeLast
bool removeLast(uinteger count)

Removes number of elements from end of list

saveFile
bool saveFile(string s, T sp)

Writes the list to a file.

set
bool set(uinteger index, T dat)

Changes the value of element at an index.

setFreeSpace
bool setFreeSpace(integer n)

make more free space for new elements, or reduce it. To reduce, use n as negative. To decrease by 2, n=-2

shrink
bool shrink(uinteger newSize)

shrinks the size of the list, removing last elements.

toArray
T[] toArray()

Exports this list into a array

write
bool write(T value)

Changes the value of element at seek. Seek is increased by one

write
bool write(T[] elements)

Changes the value of elements starting at index=seek. Seek is increased by number of elements affected

Properties

freeSpace
uinteger freeSpace [@property getter]
length
integer length [@property getter]
seek
uinteger seek [@property getter]
uinteger seek [@property setter]

The seek position

Examples

1 List!ubyte list = new List!ubyte(4);
2 //`List.insert` and `List.add` and `List.toArray`
3 list.append(0);
4 list.append(1);
5 list.insert(1, 2);
6 assert(list.toArray() == [0, 2, 1]);
7 //`List.indexOf`
8 assert(list.indexOf(1) == 2);
9 //`List.clear`
10 list.clear;
11 assert(list.length == 0);
12 //`List.loadArray`
13 list.loadArray([0, 1, 2, 3]);
14 assert(list.length == 4);
15 assert(list.indexOf(3) == 3);
16 //`List.addArray`
17 list.append([4, 5, 6, 7, 8]);
18 assert(list.length == 9);
19 //`List.set` and `List.read`
20 list.set(0, 1);
21 assert(list.read(0) == 1);
22 //`List.readLast`
23 assert(list.readLast() == 8);
24 assert(list.readLast(2) == [7, 8]);
25 //`List.readRange`
26 assert(list.read(0, 2) == [1, 1]);
27 //`List.remove`
28 list.remove(0, 2);
29 assert(list.read(0) == 2);
30 //`List.removeLast`
31 list.removeLast(2);
32 assert(list.readLast() == 6);
33 //`List.freeSpace`
34 list.clear;
35 foreach (i; cast(ubyte[])[0,1,2])
36 	list.append(i);
37 assert(list.freeSpace == 1, to!string(list.freeSpace));
38 list.append(3);
39 assert(list.freeSpace == 0);
40 list.setFreeSpace(6);
41 assert(list.freeSpace == 6 && list.length == 4);
42 list.setFreeSpace(-3);
43 assert(list.freeSpace == 3);
44 assert(list.setFreeSpace(-10) == false);
45 //reading/writing with seek
46 list.clear;
47 assert(list.seek == 0);
48 list.append([0,1,2,3,4,5,6,7,8]);
49 assert(list.seek == 0);
50 ubyte[] buffer;
51 buffer.length = 4;
52 assert(list.read(buffer) == 4);
53 assert(buffer == [0,1,2,3]);
54 assert(list.seek == 4);
55 assert(list.read == 4);
56 assert(list.write(5) == true);
57 assert(list.read(buffer) == 3);
58 assert(buffer[0 .. 3] == [6,7,8]);
59 assert(list.seek == 9);
60 //`List.readPtr`
61 list.clear;
62 list.append ([0,1,2,3,4,5]);
63 ubyte* ptr = list.readPtr(5);
64 *ptr = 4;
65 assert (list.toArray == [0,1,2,3,4,4]);
66 
67 destroy(list);

Meta