@@ -17,18 +17,115 @@ namespace FuselUtil
1717 {}
1818
1919 Element* next = nullptr ;
20+ Element* prev = nullptr ;
2021 T storage;
2122 };
2223
2324 public:
25+ class Iterator
26+ {
27+ public:
28+ Iterator () = default ;
29+ Iterator (Element* element) :
30+ m_element (element)
31+ {}
32+
33+ inline void Fwd ()
34+ {
35+ m_element = m_element->next ;
36+ }
37+ inline void Bwd ()
38+ {
39+ m_element = m_element->prev ;
40+ }
41+ inline T& Data ()
42+ {
43+ return m_element->storage ;
44+ }
45+ inline const T& Data () const
46+ {
47+ return m_element->storage ;
48+ }
49+
50+ T& operator *()
51+ {
52+ return Data ();
53+ }
54+ const T& operator *() const
55+ {
56+ return Data ();
57+ }
58+
59+ friend static bool operator ==(const Iterator& lhs, const Iterator& rhs)
60+ {
61+ return lhs.m_element == rhs.m_element ;
62+ }
63+
64+ Iterator& operator ++()
65+ {
66+ Fwd ();
67+ return *this ;
68+ }
69+ Iterator operator ++(int )
70+ {
71+ auto copy = *this ;
72+ Fwd ();
73+ return copy;
74+ }
75+
76+ Iterator& operator --()
77+ {
78+ Bwd ();
79+ return *this ;
80+ }
81+ Iterator operator --(int )
82+ {
83+ auto copy = *this ;
84+ Bwd ();
85+ return copy;
86+ }
87+
88+ private:
89+ Element* m_element = nullptr ;
90+ };
91+
92+ public:
93+ LinkedList () = default ;
94+ LinkedList (const LinkedList& other)
95+ {
96+ for (auto * element = other.m_first ; element; element = element->next )
97+ {
98+ Append (element->storage );
99+ }
100+ }
101+ LinkedList (LinkedList&& other) noexcept :
102+ m_first (other.m_first), m_count(other.m_count)
103+ {
104+ other.m_first = nullptr ;
105+ other.m_count = 0 ;
106+ }
24107 ~LinkedList ()
25108 {
26- Element* next;
27- for (Element* element = m_first; element; element = next)
109+ Clear ();
110+ }
111+
112+ LinkedList& operator =(const LinkedList& other)
113+ {
114+ if (this != &other)
28115 {
29- next = element->next ;
30- delete element;
116+ this ->~LinkedList ();
117+ new (this )LinkedList (other);
118+ }
119+ return *this ;
120+ }
121+ LinkedList& operator =(LinkedList&& other) noexcept
122+ {
123+ if (this != &other)
124+ {
125+ this ->~LinkedList ();
126+ new (this )LinkedList (std::move (other));
31127 }
128+ return *this ;
32129 }
33130
34131 void Append (const T& element)
@@ -42,17 +139,39 @@ namespace FuselUtil
42139
43140 void Erase (size_t index)
44141 {
142+ // Seek element
45143 Element** element = &m_first;
46144 for (size_t i = 0 ; i < index; i++)
47145 {
48146 element = &(*element)->next ;
49147 }
148+ // Getting next pointer
50149 Element* next = (*element)->next ;
150+ // Fixing backward linking of next element
151+ if (next)
152+ {
153+ next->prev = (*element)->prev ;
154+ }
155+ // Deletion of element
51156 delete *element;
157+ // Fixing forward linking
52158 (*element) = next;
159+
53160 m_count--;
54161 }
55162
163+ void Clear ()
164+ {
165+ Element* next;
166+ for (Element* element = m_first; element; element = next)
167+ {
168+ next = element->next ;
169+ delete element;
170+ }
171+ m_first = nullptr ;
172+ m_count = 0 ;
173+ }
174+
56175 T& At (size_t index)
57176 {
58177 Element* element = m_first;
@@ -73,6 +192,15 @@ namespace FuselUtil
73192 return element->storage ;
74193 }
75194
195+ inline Iterator begin ()
196+ {
197+ return Iterator (m_first);
198+ }
199+ inline Iterator end ()
200+ {
201+ return Iterator ();
202+ }
203+
76204 inline size_t Count () const noexcept
77205 {
78206 return m_count;
@@ -93,14 +221,15 @@ namespace FuselUtil
93221 Element** insertionPosition = &m_first;
94222 while (*insertionPosition)
95223 {
224+ element->prev = *insertionPosition;
96225 insertionPosition = &(*insertionPosition)->next ;
97226 }
98227 *insertionPosition = element;
99228 m_count++;
100229 }
101230
102231 private:
103- Element* m_first = nullptr ;
232+ mutable Element* m_first = nullptr ;
104233 size_t m_count = 0 ;
105234 };
106235}
0 commit comments