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
37template <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 */
Objects which can be compared to each other.
Definition Comparable.h:35
Containers provide access to stored items.
Definition Container.h:36
virtual Size count() const =0
Returns the number of items inside the Container.
virtual Size size() const =0
Returns the maximum size of this Container.
Sequences are containers that provide indexed based storage of items.
Definition Sequence.h:38
virtual int insert(const T &item)
Adds the given item to the Sequence, if possible.
Definition Sequence.h:48
virtual bool removeAt(Size position)
Removes the item at the given position.
Definition Sequence.h:100
virtual const T * get(Size position) const =0
Returns the item at the given position.
virtual void clear()
Removes all items from the Sequence.
Definition Sequence.h:108
virtual int compareTo(const Sequence< T > &s) const
Compare this Sequence to another Sequence.
Definition Sequence.h:153
virtual void fill(T value)
Fill the Sequence with the given value.
Definition Sequence.h:73
virtual bool equals(const Sequence< T > &s) const
Test if this Sequence is equal to an other Sequence.
Definition Sequence.h:184
virtual const T & at(Size position) const =0
Returns a reference to the item at the given position.
virtual bool contains(const T value) const
Check if the given item is stored in this Sequence.
Definition Sequence.h:139
const T & operator[](int i) const
Returns the item at the given position in the Sequence.
Definition Sequence.h:196
virtual int remove(T value)
Remove all items with the given value.
Definition Sequence.h:88
virtual bool insert(Size position, const T &item)
Inserts the given item at the given position.
Definition Sequence.h:63
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128