replicode
list.h
Go to the documentation of this file.
1 /*
2 * HUMANOBS - mBrane
3 *
4 * Eric Nivel
5 * Center for Analysis and Design of Intelligent Agents
6 * Reykjavik University, Menntavegur 1, 101 Reykjavik, Iceland
7 * http://cadia.ru.is
8 * Copyright(c)2012
9 *
10 * This software was developed by the above copyright holder as part of
11 * the HUMANOBS EU research project, in collaboration with the
12 * following parties:
13 *
14 * Autonomous Systems Laboratory
15 * Technical University of Madrid, Spain
16 * http://www.aslab.org/
17 *
18 * Communicative Machines
19 * Edinburgh, United Kingdom
20 * http://www.cmlabs.com/
21 *
22 * Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
23 * University of Lugano and SUPSI, Switzerland
24 * http://www.idsia.ch/
25 *
26 * Institute of Cognitive Sciences and Technologies
27 * Consiglio Nazionale delle Ricerche, Italy
28 * http://www.istc.cnr.it/
29 *
30 * Dipartimento di Ingegneria Informatica
31 * University of Palermo, Italy
32 * http://roboticslab.dinfo.unipa.it/index.php/Main/HomePage
33 *
34 *
35 * --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, is permitted provided that the following conditions
39 * are met:
40 *
41 * - Redistributions of source code must retain the above copyright
42 * and collaboration notice, this list of conditions and the
43 * following disclaimer.
44 *
45 * - Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following
47 * disclaimer in the documentation and/or other materials provided
48 * with the distribution.
49 *
50 * - Neither the name of its copyright holders nor the names of its
51 * contributors may be used to endorse or promote products
52 * derived from this software without specific prior written permission.
53 *
54 * - CADIA Clause: The license granted in and to the software under this
55 * agreement is a limited-use license. The software may not be used in
56 * furtherance of:
57 * (i) intentionally causing bodily injury or severe emotional distress
58 * to any person;
59 * (ii) invading the personal privacy or violating the human rights of
60 * any person; or
61 * (iii) committing or preparing for any act of war.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
67 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
69 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
73 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 */
75 
76 #ifndef mBrane_sdk_list_h
77 #define mBrane_sdk_list_h
78 
79 #include "array.h"
80 
81 namespace mBrane
82 {
83 namespace sdk
84 {
85 
86 template<typename T> class ListElement
87 {
88 public:
89  T data;
92 };
93 
94 template<typename T, uint16_t Size> class List
95 {
96 public:
97  class Iterator
98  {
99  friend class List;
100  private:
103  Iterator(List *l, ListElement<T> *object): list(l), object(object) {}
104  public:
105  Iterator(): list(NULL), object(NULL) {}
106  Iterator(const Iterator &i): list(i.list), object(i.object) {}
109  {
110  list = i.list;
111  object = i.object;
112  return *this;
113  }
115  {
116  object = object->next;
117  return *this;
118  }
120  {
121  object = object->prev;
122  return *this;
123  }
124  bool operator ==(const Iterator &i) const
125  {
126  return object == i.object;
127  }
128  bool operator !=(const Iterator &i) const
129  {
130  return object != i.object;
131  }
132  operator ListElement<T> *()
133  {
134  return object;
135  }
136  T &operator *() const
137  {
138  return object->data;
139  }
140  Iterator insertAfter(T &t) const
141  {
142  list->insertAfter(object, t);
143  return Iterator(list, object->next);
144  }
145  Iterator insertBefore(T &t) const
146  {
147  list->insertBefore(object, t);
148  return Iterator(list, object->prev);
149  }
150  void remove()
151  {
152  list->remove(object);
153  object = NULL;
154  }
156  {
157  object = list->removeReturnNext(object);
158  }
160  {
161  object = list->removeReturnPrevious(object);
162  }
163  };
164 protected:
165  class _List:
166  public Array<ListElement<T>, Size>
167  {
168  public:
169  _List();
170  ~_List();
171  void clear();
173  };
174 
175  _List data;
176 
183  void remove(ListElement<T> *o);
186  Iterator insertAfter(ListElement<T> *o, T &t); // returns the index of the new element
187  Iterator insertBefore(ListElement<T> *o, T &t);
188 public:
189  List();
190  ~List();
191  uint32_t elementCount() const;
192  void clear();
193  Iterator addElementHead(T &t); // returns the index of the new element
194  Iterator addElementTail(T &t);
195  Iterator addElement(T &t); // inserts in order with respect to the B function (i.e. new element "before" current element)
196  void removeElement(T *t);
197  Iterator begin()
198  {
199  Iterator i(this, first);
200  return i;
201  }
202  Iterator end()
203  {
204  Iterator i(this, last);
205  return i;
206  }
207 };
208 }
209 }
210 
211 
212 #include "list.tpl.cpp"
213 
214 
215 #endif
bool operator==(const Iterator &i) const
Definition: list.h:124
Iterator insertBefore(ListElement< T > *o, T &t)
Definition: list.tpl.cpp:242
Iterator(List *l, ListElement< T > *object)
Definition: list.h:103
~_List()
Definition: list.tpl.cpp:87
Iterator & operator--()
Definition: list.h:119
void clear()
Definition: list.tpl.cpp:91
void remove(ListElement< T > *o)
Definition: list.tpl.cpp:174
~Iterator()
Definition: list.h:107
Iterator addElementHead(T &t)
Definition: list.tpl.cpp:268
Iterator end()
Definition: list.h:202
T data
Definition: list.h:89
Definition: list.h:86
Iterator addElementTail(T &t)
Definition: list.tpl.cpp:273
Iterator(const Iterator &i)
Definition: list.h:106
Definition: array.h:84
~List()
Definition: list.tpl.cpp:130
uint32_t _elementCount
Definition: list.h:179
void removeJumpNext()
Definition: list.h:155
uint32_t elementCount() const
Definition: list.tpl.cpp:134
Definition: list.h:94
ListElement< T > * first
Definition: list.h:177
void removeJumpPrevious()
Definition: list.h:159
bool operator!=(const Iterator &i) const
Definition: list.h:128
List * list
Definition: list.h:101
Iterator insertAfter(ListElement< T > *o, T &t)
Definition: list.tpl.cpp:216
Iterator addElement(T &t)
Definition: list.tpl.cpp:278
Iterator & operator++()
Definition: list.h:114
Definition: list.h:97
Iterator()
Definition: list.h:105
_List()
Definition: list.tpl.cpp:83
ListElement< T > * last
Definition: list.h:178
T & operator*() const
Definition: list.h:136
List()
Definition: list.tpl.cpp:125
ListElement< T > * getFreeSlot()
Definition: list.tpl.cpp:148
Iterator & operator=(const Iterator &i)
Definition: list.h:108
Iterator begin()
Definition: list.h:197
uint32_t removeReturnPrevious(ListElement< T > *o)
Definition: list.tpl.cpp:210
void removeElement(T *t)
Definition: list.tpl.cpp:199
Iterator insertAfter(T &t) const
Definition: list.h:140
_List data
Definition: list.h:175
Definition: array.h:162
ListElement< T > * lastFree
Definition: list.h:181
uint32_t removeReturnNext(ListElement< T > *o)
Definition: list.tpl.cpp:204
ListElement< T > * expand()
Definition: list.tpl.cpp:116
ListElement * next
Definition: list.h:90
Iterator insertBefore(T &t) const
Definition: list.h:145
void clear()
Definition: list.tpl.cpp:139
ListElement< T > * firstFree
Definition: list.h:180
ListElement< T > * object
Definition: list.h:102
Definition: list.h:165
ListElement * prev
Definition: list.h:91