FAST.Framework  1.0.0
FList.h
1 /******************************************************************************
2 **
3 ** FAST.Framework
4 **
5 ** Created: 2023-10-11
6 ** Author: Boris Fast
7 ** Mail: support@fast-framework.com
8 **
9 ******************************************************************************/
10 
11 #ifndef FLIST_H
12 #define FLIST_H
13 
14 using namespace std;
15 
16 #include "FObject.h"
17 #include "FString.h"
18 #include "FNode.h"
19 
24 template <typename T>
25 class FList : public FObject
26 {
27  public:
28  FList() : head(0), tail(0), count(0) { }
29  virtual ~FList() { Clear(); }
30 
31  inline void Add(const T& item)
32  {
33  if (this->head)
34  {
35  this->tail->next = new FNode<T>(item, this->tail);
36  this->tail = this->tail->next;
37  }
38  else
39  {
40  this->head = this->tail = new FNode<T>(item);
41  }
42 
43  this->count++;
44  }
45 
46  inline ULLong Count() const { return this->count; }
47 
48  inline T operator [](ULLong position) const
49  {
50  if (position >= this->count) return T();
51 
52  if (this->head)
53  {
54  Boolean forward = position <= (this->count / 2) ? true : false;
55  FNode<T>* current = forward ? this->head : this->tail;
56  ULLong index = forward ? 0 : (this->count - 1);
57 
58  if (forward)
59  {
60  while (index < position)
61  {
62  current = current->next;
63  index++;
64  }
65  }
66  else
67  {
68  while (index > position)
69  {
70  current = current->last;
71  index--;
72  }
73  }
74 
75  return current->data;
76  }
77 
78  return T();
79  }
80 
81  inline void Remove(ULLong position)
82  {
83  if (position >= this->count) return;
84 
85  if (this->head)
86  {
87  if (position == 0 && !this->head->next)
88  {
89  delete this->head;
90  this->head = nullptr;
91  }
92  else if (position == 0)
93  {
94  FNode<T>* current = this->head;
95  this->head = current->next;
96 
97  delete current;
98  current = nullptr;
99  }
100  else
101  {
102  Boolean forward = position <= (this->count / 2) ? true : false;
103  FNode<T>* current = forward ? this->head : this->tail;
104  ULLong index = forward ? 0 : (this->count - 1);
105 
106  if (forward)
107  {
108  while (index < position)
109  {
110  current = current->next;
111  index++;
112  }
113  }
114  else
115  {
116  while (index > position)
117  {
118  current = current->last;
119  index--;
120  }
121  }
122 
123  FNode<T>* last = current->last;
124  FNode<T>* next = current->next;
125 
126  if (next) next->last = last;
127  if (last) last->next = next;
128 
129  delete current;
130  current = nullptr;
131  }
132 
133  this->count--;
134  }
135  }
136 
137  inline void Clear()
138  {
139  if (this->head)
140  {
141  FNode<T>* current = this->head;
142  FNode<T>* next = 0;
143 
144  while (current) {
145  next = current;
146  current = current->next;
147 
148  delete next;
149  next = nullptr;
150  }
151  }
152  }
153 
154  inline const type_info& GetType() override
155  {
156  return typeid(FList);
157  }
158 
159  inline FString GetName() const override
160  {
161  return typeid(FList).name();
162  }
163 
164  private:
165  FNode<T>* head;
166  FNode<T>* tail;
167  ULLong count;
168 };
169 
170 #endif // FLIST_H
Definition: FList.h:26
FList()
Constructor list object.
Definition: FList.h:28
FString GetName() const override
< Returns the name of object
Definition: FList.h:159
virtual ~FList()
Destroy list object.
Definition: FList.h:29
ULLong Count() const
Count of items in list.
Definition: FList.h:46
void Remove(ULLong position)
Definition: FList.h:81
void Add(const T &item)
Definition: FList.h:31
const type_info & GetType() override
< Returns the type info over object
Definition: FList.h:154
Definition: FNode.h:24
Definition: FObject.h:50
Definition: FString.h:22