LinkedList

A linked list, used where only reading in the forward direction is required

Constructors

this
this()
Undocumented in source.

Destructor

~this
~this()
Undocumented in source.

Members

Functions

append
void append(T item)

adds a new node at the end of the list

append
void append(T[] items)

adds new nodes at end of list from an array

clear
void clear()

clears/resets the list. Frees all the occupied memory, & removes all items

clearBookmarks
void clearBookmarks()

Removes all bookmarks

hasElement
bool hasElement(T node)

Returns true if list contains a node, i.e searches for a node and returns true if found

hasElements
bool hasElements(T[] nodes)

Returns true if list contains all elements provided in an array, else, false

insert
void insert(T node)

Inserts a node after the position of last-read-node To insert at beginning, call resetRead before inserting

insert
void insert(T[] nodes)

Inserts an array of nodes after the position of last-read-node If there is no last-read-item, the item is inserted at beginning. To do this, call resetRead before inserting Returns true on success, false on failure

moveToBookmark
bool moveToBookmark(uinteger id)

moves read/insert position back to a bookmark using the bookmark ID Does NOT delete the bookmark. Use LinkedList.removeBookmark to delete Retutns true if successful false if the bookmark ID no longer exists

placeBookmark
uinteger placeBookmark()

Sets a "bookmark", and returns the bookmark-ID, throws Exception if there is no last-read-item to place bookmark on

read
T* read()

returns pointer of next node to be read, null if there are no more nodes

readFirst
T* readFirst()

Returns the pointer to the first node in the list

readLast
T* readLast()

Returns the pointer to the last node in the list

removeBookmark
bool removeBookmark(uinteger id)

removes a bookmark using the bookmark id returns true if successful false if the bookmark doesn't exist

removeFirst
void removeFirst()

removes the first node in list

removeLastRead
bool removeLastRead()

removes the node that was last read using LinkedList.read. The last node cannot be removed using this. returns true on success

resetRead
void resetRead()

resets the read position, i.e: set reading position to first node, and nulls the last-read-ptr

toArray
T[] toArray()

Reads the list into an array, and returns the array

Properties

count
uinteger count [@property getter]

number of items that the list is holding

Examples

Unittests for utils.lists.LinkedList

1 LinkedList!ubyte list = new LinkedList!ubyte;
2 //`LinkedList.append` and `LinkedList.read` and `LinkedList.readFirst` and `LinkedList.readLast` and `LinkedList.resetRead`
3 list.append(0);
4 list.append(1);
5 list.append(2);
6 assert(*(list.readFirst()) == 0);
7 assert(*(list.readLast()) == 2);
8 assert(list.count == 3);
9 list.read();// to skip, we wanna read the node at index 1 (2nd node)
10 assert(*(list.read()) == 1);
11 list.resetRead();
12 assert(*(list.read()) == 0);
13 // `LinkedList.append(T[])`:
14 list.clear();
15 list.append(0);
16 list.append([1, 2, 3]);
17 assert(list.count == 4);
18 assert(list.toArray ==[0, 1, 2, 3]);
19 list.clear;
20 list.append([0, 1, 2]);
21 list.append(3);
22 assert(list.count == 4);
23 assert(list.toArray == [0, 1, 2, 3]);
24 //`LinkedList.clear`
25 list.clear();
26 list.append(3);
27 list.append(4);
28 assert(*(list.read()) == 3);
29 assert(list.count == 2);
30 list.clear();
31 //`LinkedList.removeLastRead` and `Linkedlist.removeFirst`
32 list.append(0);
33 list.append(1);
34 list.append(2);
35 list.read();
36 list.read();
37 list.removeLastRead();
38 list.resetRead();
39 assert(*(list.read()) == 0);
40 assert(*(list.read()) == 2);
41 assert(list.count == 2);
42 list.removeFirst();
43 list.resetRead();
44 assert(*(list.read()) == 2);
45 assert(list.count == 1);
46 list.removeLastRead();
47 assert(list.count == 0);
48 //`LinkedList.toArray` and `LinkedList.insertNode` and `LinkedList.insertNodes`
49 list.clear();// to reset stuff
50 list.append(0);
51 list.append(4);
52 list.read();
53 list.insert(1);
54 assert(*(list.read()) == 1);
55 list.insert([2, 3]);
56 list.resetRead();
57 assert(list.count == 5);
58 assert(list.toArray == [0, 1, 2, 3, 4]);
59 //`Linkedlist.hasElement` and `LinkedList.hasElements`
60 assert(list.hasElement(0) == true);
61 assert(list.hasElement(4) == true);
62 assert(list.hasElement(5) == false);
63 assert(list.hasElement(7) == false);
64 assert(list.hasElements([3, 1, 2, 0, 4]) == true);
65 assert(list.hasElements([0, 1, 2, 6]) == false);
66 // `LinkedList.insert` at beginning
67 list.clear;
68 list.insert([1, 2]);
69 list.insert(0);
70 assert(list.count == 3);
71 assert(list.toArray == [0, 1, 2]);
72 //destroying last item
73 list.clear();
74 list.append(0);
75 list.append(1);
76 list.append(2);
77 list.read();
78 list.read();
79 list.read();
80 assert(list.removeLastRead() == true);
81 assert(list.toArray() == [0, 1]);
82 //bookmarks
83 list.clear;
84 list.append([0, 1, 2, 3, 4, 5]);
85 assert(*list.read == 0);
86 assert(*list.read == 1);
87 assert(*list.read == 2);
88 {
89 	uinteger id = list.placeBookmark;
90 	assert(*list.read == 3);
91 	assert(list.moveToBookmark(id + 1) == false);
92 	assert(list.moveToBookmark(id) == true);
93 	assert(*list.read == 3);
94 	assert(list.removeBookmark(id) == true);
95 }
96 destroy(list);

Meta