aboutsummaryrefslogtreecommitdiff
path: root/PDContainer/include/HList.h
blob: b8a5768709802d897d14b0226dabe89364580f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// *********************(c)*2004*********************>
// -holzilib--holzilib--holzilib--holzilib--holzilib->
// ++++PD-External++by+Georg+Holzmann++grh@gmx.at++++>
//
// PDContainer: 
// this is a port of the containers from the C++ STL
// (Standard Template Library)
// for usage see the documentation and PD help files
// for license see readme.txt
//
// HList.h

#ifndef _h_list_h__
#define _h_list_h__


#include "include/SimpleBase.h"
#include <list>

using std::list;


//---------------------------------------------------
/* this is the class of the list
 */
class HList : 
public SimpleBase< list<Element>, list<Element>::iterator >
{
 protected:
  /* this integer holds the current position of the
   * iterator, because accessing the iterator directly
   * from PD is very buggy...
   * (maybe I'll change this !)
   */
  int i_pos_;

  /* the internal iterator, you can navigate
   * through the container with it
   */
  list<Element>::iterator iter_;

 private:

  /* Copy Construction is not allowed
   */
  HList(const HList &src)
    { }

  /* assignement operator is not allowed
   */
  const HList& operator = (const HList&)
    { return *this; }

 public:

  /* Standard Constructor
   * no namespace
   */
  HList()
    { 
      dataname_ = "h_list";
      i_pos_=0;
    }

  /* Constructor
   * with a namespace
   */
  HList(string h_namespace)
    {
      dataname_ = "h_list";
      setNamespace(h_namespace);
      i_pos_=0;
    }

  /* Destructor
   */
  virtual ~HList() { }

  /* inserts an element at the end of 
   * the container
   */
  virtual void pushBack(Element value)
    {  data_[h_namespace_].push_back(value);  }

  /* removes the element from the end of 
   * the container
   */
  virtual void popBack()
    {  data_[h_namespace_].pop_back();  }

  /* inserts an element at the end of 
   * the container
   */
  virtual void pushFront(Element value)
    {  data_[h_namespace_].push_front(value);  }

  /* removes the element from the end of 
   * the container
   */
  virtual void popFront()
    {  data_[h_namespace_].pop_front();  }

  /* returns the last element
   */
  virtual Element &back() const 
    {  return data_[h_namespace_].back(); }

  /* returns the first element
   */
  virtual Element &front() const 
    {  return data_[h_namespace_].front(); }

  /* inserts an element at the current
   * iterator position
   */
  virtual void insert(Element value)
    {
      makeIterator();
      data_[h_namespace_].insert(iter_, value);
      i_pos_++;
    }
  
  /* overrides the element at the current
   * iterator position
   */
  virtual void modify(Element value)
  {
    makeIterator();
    *iter_=value;
  }
    
    
  /* gives back the element at the current
   * iterator position
   */
  virtual Element get()
    {
      makeIterator();
 
      // key was not found if iterator is pointing to the end
      if(iter_ == data_[h_namespace_].end())
	throw "h_list, get: Element not found !";

      return *iter_;
    }

  /* removes all elements with that value
   */
  virtual void remove(Element value)
    {  data_[h_namespace_].remove(value);  }

  /* removes an element at the current
   * iterator position
   */
  virtual void del()
    {
      makeIterator();

      if(data_[h_namespace_].size() == 0)
	return;

      if(iter_ ==  data_[h_namespace_].end())
	{
	  post("h_list, delete: not possible, go back by 1 (iterator points to the element after the end) !!!");
	  return;
	}

      data_[h_namespace_].erase(iter_);
    }

  /* get the size of the sequence
   */
  virtual int getSize() const
    {  return data_[h_namespace_].size();  }

  /* set current iterator position
   */
  virtual void setIterPos(int position)
  {  i_pos_ = position;  }

  /* get the current iterator position
   */
  virtual int getIterPos()
    {
      makeIterator(); 
      return i_pos_;
    }

  /* sets the iterator position the the begin
   * of the list
   */
  virtual void begin()
    {  i_pos_ = 0;  }

  /* sets the iterator position the the end
   * of the list
   */
  virtual void end()
    {  i_pos_ = data_[h_namespace_].size();  }

  /* increases the iterator position by one
   */
  virtual void next()
    {  i_pos_++; }

  /* decreases the iterator position by one
   */
  virtual void last()
    {  i_pos_--;  }

  /* removes all but the first element in every 
   * consecutive group of equal elements
   */
  virtual void unique()
    {  data_[h_namespace_].unique();  }

  /* reverses the order of elements in the list
   */
  virtual void reverse()
    {  data_[h_namespace_].reverse();  }

  /* sorts the list according to operator<
   */
  virtual void sort()
    {  data_[h_namespace_].sort();  }
  
  /* reads from file
   */
  virtual bool readFromFile(string filename);

 private:
  /* generates the current iterator position
   */
  void makeIterator()
    {
      if(i_pos_<0)
	i_pos_=0;

      if((unsigned)i_pos_ > data_[h_namespace_].size())
	i_pos_=data_[h_namespace_].size();

      // this is a hack to make the iterator, I'll change this !
      iter_=data_[h_namespace_].begin();
      for(int i = 0; i<i_pos_; i++) iter_++;
    }
};



#endif //_h_list_h__