FreeNOS
Sequence.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBSTD_SEQUENCE_H
19 #define __LIBSTD_SEQUENCE_H
20 
21 #include "Container.h"
22 #include "Comparable.h"
23 #include "Types.h"
24 #include "Macros.h"
25 
37 template <class T> class Sequence : public Container, public Comparable<Sequence<T> >
38 {
39  public:
40 
48  virtual int insert(const T & item)
49  {
50  return -1;
51  }
52 
63  virtual bool insert(Size position, const T & item)
64  {
65  return false;
66  }
67 
73  virtual void fill(T value)
74  {
75  Size s = this->size();
76 
77  for (Size i = 0; i < s; i++)
78  insert(i, value);
79  }
80 
88  virtual int remove(T value)
89  {
90  return 0;
91  }
92 
100  virtual bool removeAt(Size position)
101  {
102  return false;
103  }
104 
108  virtual void clear()
109  {
110  Size s = this->size();
111 
112  for (Size i = 0; i < s; i++)
113  removeAt(i);
114  }
115 
123  virtual const T * get(Size position) const = 0;
124 
134  virtual const T & at(Size position) const = 0;
135 
139  virtual bool contains(const T value) const
140  {
141  Size sz = this->size();
142 
143  for (Size i = 0; i < sz; i++)
144  if (at(i) == value)
145  return true;
146 
147  return false;
148  }
149 
153  virtual int compareTo(const Sequence<T> &s) const
154  {
155  Size sz = this->size();
156  Size cnt = this->count();
157 
158  // Size must be equal
159  if (s.size() != sz)
160  return s.size() - sz;
161 
162  // Count must be equal
163  if (s.count() != cnt)
164  return s.count() - cnt;
165 
166  // All elements must be equal
167  for (Size i = 0; i < cnt; i++)
168  {
169  if (at(i) != s.at(i))
170  {
171  return i + 1;
172  }
173  }
174  return 0;
175  }
176 
184  virtual bool equals(const Sequence<T> &s) const
185  {
186  return compareTo(s) == 0;
187  }
188 
196  const T & operator [] (int i) const
197  {
198  return at(i);
199  }
200 
208  const T & operator [] (Size i) const
209  {
210  return at(i);
211  }
212 
220  T & operator [] (int i)
221  {
222  return (T &) at(i);
223  }
224 
233  {
234  return (T &) at(i);
235  }
236 };
237 
243 #endif /* __LIBSTD_SEQUENCE_H */
Container.h
Macros.h
Sequence::insert
virtual bool insert(Size position, const T &item)
Inserts the given item at the given position.
Definition: Sequence.h:63
Types.h
Container::count
virtual Size count() const =0
Returns the number of items inside the Container.
Sequence::remove
virtual int remove(T value)
Remove all items with the given value.
Definition: Sequence.h:88
Sequence::get
virtual const T * get(Size position) const =0
Returns the item at the given position.
Container
Containers provide access to stored items.
Definition: Container.h:35
Sequence::at
virtual const T & at(Size position) const =0
Returns a reference to the item at the given position.
Sequence::contains
virtual bool contains(const T value) const
Check if the given item is stored in this Sequence.
Definition: Sequence.h:139
Sequence::removeAt
virtual bool removeAt(Size position)
Removes the item at the given position.
Definition: Sequence.h:100
Comparable
Objects which can be compared to each other.
Definition: Comparable.h:34
Sequence::fill
virtual void fill(T value)
Fill the Sequence with the given value.
Definition: Sequence.h:73
Sequence
Sequences are containers that provide indexed based storage of items.
Definition: Sequence.h:37
Sequence::clear
virtual void clear()
Removes all items from the Sequence.
Definition: Sequence.h:108
Sequence::insert
virtual int insert(const T &item)
Adds the given item to the Sequence, if possible.
Definition: Sequence.h:48
Sequence::operator[]
const T & operator[](int i) const
Returns the item at the given position in the Sequence.
Definition: Sequence.h:196
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Comparable.h
Sequence::equals
virtual bool equals(const Sequence< T > &s) const
Test if this Sequence is equal to an other Sequence.
Definition: Sequence.h:184
Container::size
virtual Size size() const =0
Returns the maximum size of this Container.
Sequence::compareTo
virtual int compareTo(const Sequence< T > &s) const
Compare this Sequence to another Sequence.
Definition: Sequence.h:153