1 /*
2 Copyright (c) 2015 Timur Gafarov 
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dgl.util.linkedlist;
30 
31 import dlib.core.memory;
32 
33 public:
34 
35 struct LinkedListElement(T)
36 {
37     LinkedListElement!(T)* next = null;
38     T datum;
39 
40     this(LinkedListElement!(T)* n)
41     {
42         next = n;
43         datum = T.init;
44     }
45 }
46 
47 struct LinkedList(T, bool ordered = true)
48 {
49     LinkedListElement!(T)* head = null;
50     LinkedListElement!(T)* tail = null;
51     size_t length = 0;
52 
53     @property bool empty()
54     {
55         return length == 0;
56     }
57 
58     void clear()
59     {
60         LinkedListElement!(T)* element = head;
61         while (element !is null)
62         {
63             auto e = element;
64             element = element.next;
65             Delete(e);
66         }
67         head = null;
68         tail = null;
69         length = 0;
70     }
71 
72     void traverse(void delegate(ref T v, LinkedListElement!(T)* elem) func)
73     {
74         LinkedListElement!(T)* element = head;
75         while (element !is null)
76         {
77             func(element.datum, element);
78             element = element.next;
79         }
80     }
81 
82     int opApply(int delegate(uint, ref T) dg)
83     {
84         int result = 0;
85         uint index = 0;
86 
87         LinkedListElement!(T)* element = head;
88         while (element !is null)
89         {
90             result = dg(index, element.datum);
91             if (result)
92                 break;
93             element = element.next;
94             index++;
95         }
96 
97         return result;
98     }
99 
100     LinkedListElement!(T)* append(T v)
101     {
102         length++;
103         
104         if (tail is null)
105         {
106             tail = New!(LinkedListElement!(T))(null);
107             tail.datum = v;
108         }
109         else
110         {
111             tail.next = New!(LinkedListElement!(T))(null);
112             tail.next.datum = v;
113             tail = tail.next;
114         }
115 
116         if (head is null) head = tail;
117         
118         return tail;
119     }
120 
121     LinkedListElement!(T)* insertAfter(LinkedListElement!(T)* element, T v)
122     {
123         length++;
124         auto newElement = New!(LinkedListElement!(T))(null);
125         newElement.datum = v;
126         newElement.next = element.next;
127         element.next = newElement;
128         if (element is tail) tail = newElement;
129         return newElement;
130     }
131 
132     LinkedListElement!(T)* insertBeginning(T v)
133     {
134         length++;
135         auto newElement = New!(LinkedListElement!(T))(null);
136         newElement.datum = v;
137         newElement.next = head;
138         head = newElement;
139         return newElement;
140     }
141 
142     void removeAfter(LinkedListElement!(T)* element)
143     {
144         length--;
145         auto obsolete = element.next;
146         if (obsolete !is null)
147         {
148             if (obsolete is tail) tail = element;
149             element.next = obsolete.next;
150             Delete(obsolete);
151         }
152     }
153 
154     void removeBeginning()
155     {
156         length--;
157         auto obsolete = head;
158         if (obsolete !is null)
159         {
160             head = obsolete.next;
161             Delete(obsolete);
162         }
163     }
164 
165     void appendList(LinkedList!(T) list)
166     {
167         length += list.length;
168         tail.next = list.head;
169         tail = list.tail;
170     }
171 
172     LinkedListElement!(T)* search(T v)
173     {
174         LinkedListElement!(T)* element = head;
175         LinkedListElement!(T)* prevElement = head;
176         while (element !is null)
177         {
178             if (element.datum == v)
179             {
180                 static if (!ordered)
181                 {
182                    /*
183                     * Move-to-front heuristic:
184                     * Move an element to the beginning of the list once it is found. 
185                     * This scheme ensures that the most recently used items are also 
186                     * the quickest to find again.
187                     */
188                     prevElement.next = element.next;
189                     element.next = head;
190                     head = element;
191                 }
192 
193                 return element;
194             }
195 
196             prevElement = element;
197             element = element.next;
198         }
199 
200         return null;
201     }
202 
203     T[] toArray()
204     {
205         T[] arr = New!(T[])(length);
206         foreach(i, v; this)
207             arr[i] = v;
208         return arr;
209     }
210 }
211