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/element to the end of the list

append
void append(T[] items)

adds new nodes/items at end of list

clear
void clear()

clears/resets the list, by deleting all elements

clearBookmarks
void clearBookmarks()

Removes all bookmarks

hasElement
bool hasElement(T node)
hasElements
bool hasElements(T[] nodes)

matches all elements from an array to elements in list, to see if all elements in array are present in the list

insert
void insert(T node)

Inserts a node after the position of last-read-node, i.e, to insert at position from where next item is to be read

insert
void insert(T[] nodes)

Inserts nodes after the position of last-read-node, i.e, to insert at position from where next item is to be read

moveToBookmark
bool moveToBookmark(uinteger id)

moves read position back to a bookmark using the bookmark ID

placeBookmark
uinteger placeBookmark()

Sets a "bookmark"

read
T* read()
readFirst
T* readFirst()
readLast
T* readLast()
remove
bool remove(T toRemove, uinteger count)

finds an element, if found, deletes it

remove
bool remove(T[] toRemove)

searches the whole list, and any element that matches with elements in the array are deleted

removeBookmark
bool removeBookmark(uinteger id)

removes a bookmark using the bookmark id

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.

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

Properties

count
uinteger count [@property getter]

Examples

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

Meta