GUIDOLib  1.7.7
Guido Engine Internal Documentation
kf_ilist.h
1 #ifndef KF_IPointerList_H
2 #define KF_IPointerList_H
3 
4 /*
5  GUIDO Library
6  Copyright (C) 2002 Holger Hoos, Juergen Kilian, Kai Renz
7  Copyright (C) 2002-2017 Grame
8 
9  This Source Code Form is subject to the terms of the Mozilla Public
10  License, v. 2.0. If a copy of the MPL was not distributed with this
11  file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 
13  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
14  research@grame.fr
15 
16 */
17 
18 
19 #include "kf_list.h"
20 
21 template <class TYPE>
22 class KF_IPointerList : public KF_List<TYPE *>
23 {
24  public:
25  KF_IPointerList(int p_ownselements = 0) { ownselements = p_ownselements; }
26  KF_IPointerList(const KF_IPointerList<TYPE> &lst, int p_ownselements = 0);
27  virtual ~KF_IPointerList() { RemoveAll(); }
28 
29  virtual GuidoPos GetElementPos(const TYPE * data) const;
30 
31  virtual void RemoveAll();
32  virtual void RemoveElementAt(GuidoPos pos);
33  virtual int RemoveElement(TYPE *data);
34 
35  virtual void DumpListAtTail(KF_IPointerList<TYPE> * list );
36  virtual KF_IPointerList<TYPE> * getCopy();
37  virtual void Cut(GuidoPos pos,KF_IPointerList<TYPE> **pnew);
38 
39  virtual void setOwnership(int p_ownselements) { ownselements = p_ownselements; }
40  virtual int getOwnership() const { return ownselements; }
41 
42  virtual void sort( int comp(const TYPE *, const TYPE *) );
43 
44  virtual void AddSortedHead(TYPE *, int comp(const TYPE *, const TYPE *));
45  virtual void AddSortedTail(TYPE *, int comp(const TYPE *, const TYPE *));
46 
47  protected:
48  int ownselements; // 1: elements will be deleted by list
49 
50  private:
51  void DeleteData(GuidoPos pos);
52  void DeleteAllData(); // delete data
53 
56 };
57 
58 template <class TYPE>
60  : KF_List<TYPE *>()
61 {
62  ownselements = p_ownselements;
63  GuidoPos pos = lst.GetHeadPosition();
64  TYPE * mytype;
65  while (pos)
66  {
67  mytype = lst.GetNext(pos);
69  }
70 }
71 
72 template <class TYPE>
74 {
75  if (ownselements) {
76  GuidoPos pos = this->GetHeadPosition();
77  while (pos) {
78  DeleteData (pos);
79  this->GetNext(pos);
80  }
81  }
82 }
83 
84 template <class TYPE>
85 void KF_IPointerList<TYPE>::DeleteData(GuidoPos pos)
86 {
87  if (ownselements) {
88  delete this->GetAt(pos);
89  this->SetAt (pos, 0);
90  }
91 }
92 
93 template <class TYPE>
95 {
96  DeleteAllData();
98 }
99 
100 template <class TYPE>
101 GuidoPos KF_IPointerList<TYPE>::GetElementPos(const TYPE *data) const
102 {
103  GuidoPos pos = this->GetHeadPosition();
104  while (pos)
105  {
106  if ( data == this->GetAt(pos) )
107  return pos;
108  this->GetNext(pos);
109  }
110  return pos;
111 }
112 
113 template <class TYPE>
115 {
116  GuidoPos pos = GetElementPos(data);
117  if (pos)
118  {
119  RemoveElementAt(pos);
120  return 1;
121  }
122  return 0;
123 }
124 
125 template <class TYPE>
127 {
128  if (ownselements)
129  DeleteData(pos);
131 }
132 
133 
134 template <class TYPE>
135 void KF_IPointerList<TYPE>::sort(int comp(const TYPE *, const TYPE *))
136 {
137  // so we bubble agagin ...
138  int needssort = 1;
139  while (needssort)
140  {
141  needssort = 0;
142  GuidoPos pos = this->GetHeadPosition();
143  while (pos)
144  {
145  GuidoPos pos1 = pos;
146  TYPE *t1 = this->GetNext(pos);
147  if (pos)
148  {
149  TYPE *t2 = this->GetAt(pos);
150  if ( comp(t1,t2) == 1)
151  {
152  KF_List<TYPE *>::SetAt(pos1,t2);
153  KF_List<TYPE *>::SetAt(pos,t1);
154  needssort = 1;
155  }
156  }
157  }
158  }
159 }
160 
161 // - Stole the events of input list (was: AddListAtTail)
162 template<class TYPE>
164 {
165  if (this->fTail == 0)
166  {
167  this->fHead = list->fHead;
168  this->fTail = list->fTail;
169  this->fCount = list->fCount;
170  }
171  else if (list->fHead)
172  {
173  this->fTail->fNext = list->fHead;
174  list->fHead->fPrev = this->fTail;
175  this->fTail = list->fTail;
176  this->fCount += list->fCount;
177  }
178 
179  list->fHead = 0;
180  list->fTail = 0;
181  list->fCount = 0;
182 }
183 
184 template <class TYPE>
186 {
187  // first, we create the new list:
188  *pnew = new KF_IPointerList<TYPE>(ownselements);
189  // this remembers the head-position for the new-list
191 
192  if (node)
193  {
194  node = node->fNext;
195  // then, we set the tail-position of the current list
196  this->SetTailPosition(pos);
197  // and the head-position (and tail-pos) of the new-list
198  (*pnew)->SetHeadPosition((GuidoPos) node);
199  }
200  else
201  {
202  // cut at the very beginning
203  (*pnew)->SetHeadPosition((GuidoPos) this->fHead);
204  this->fHead = 0;
205  this->fTail = 0;
206  this->fCount = 0;
207  }
208 }
209 
210 // this routine copies the list ....
211 template <class TYPE>
213 {
214  // if (ownselements) return 0;
215  KF_IPointerList *myl = new KF_IPointerList(0);
216 
217  GuidoPos pos = this->GetHeadPosition();
218  while (pos)
219  {
220  myl->AddTail(this->GetNext(pos));
221  }
222  return myl;
223 }
224 
225 // These routines assume that the list is sorted
226 // already. a new element is placed at the correct position
227 // beginning at either the head or the tail ....
228 template <class TYPE>
229 void KF_IPointerList<TYPE>::AddSortedHead(TYPE * data, int comp(const TYPE *, const TYPE *))
230 {
231  // iterate through the list from the beginning
232  // and place the rod where it should go ...
233  GuidoPos pos = this->GetHeadPosition();
234  if (!pos)
235  {
236  // there is no data in the list ....
238  return;
239  }
240  while (pos)
241  {
242  TYPE * tmp = this->GetAt(pos);
243  int res = comp(tmp,data);
244  // >= 0 means 0 or 1
245  // 0: tmp is equal to data
246  // 1: tmp is bigger then data
247  if ( res >= 1)
248  {
249  // then we can place the element at the current position
251  return;
252  }
253  this->GetNext(pos);
254  }
255  // then we have traversed the whole list without finding a suitable position ....
257 }
258 
259 template <class TYPE>
260 void KF_IPointerList<TYPE>::AddSortedTail(TYPE * data,int comp(const TYPE *, const TYPE *))
261 {
262  // iterate through the list from the end and place the rod where it should go ...
263  GuidoPos pos = this->GetTailPosition();
264  if (!pos)
265  {
266  // then there is no data in the list ....
268  return;
269  }
270  while (pos)
271  {
272  TYPE * tmp = this->GetAt(pos);
273  int res = comp(tmp,data);
274  // <= means -1 or 0,
275  // -1: tmp is smaller than data
276  // 0 : tmp is equal to data
277  if ( res < 0)
278  {
279  // then we can place the element at the current position
281  return;
282  }
283  this->GetPrev(pos);
284  }
285  // then we have traversed the whole list without finding a suitable position ....
287 }
288 
289 #endif
KF_List::AddTail
GuidoPos AddTail(TYPE data)
Definition: kf_list.h:78
KF_List::AddHead
GuidoPos AddHead(TYPE data)
Definition: kf_list.h:124
KF_IPointerList::RemoveAll
virtual void RemoveAll()
Definition: kf_ilist.h:94
KF_IPointerList::GetElementPos
virtual GuidoPos GetElementPos(const TYPE *data) const
Definition: kf_ilist.h:101
KF_IPointerList::DumpListAtTail
virtual void DumpListAtTail(KF_IPointerList< TYPE > *list)
Definition: kf_ilist.h:163
KF_IPointerList::RemoveElement
virtual int RemoveElement(TYPE *data)
Definition: kf_ilist.h:114
KF_IPointerList::Cut
virtual void Cut(GuidoPos pos, KF_IPointerList< TYPE > **pnew)
Definition: kf_ilist.h:185
KF_IPointerList::ownselements
int ownselements
Definition: kf_ilist.h:48
KF_IPointerList::~KF_IPointerList
virtual ~KF_IPointerList()
Definition: kf_ilist.h:27
KF_List< TYPE * >::sort
virtual void sort()
Definition: kf_list.h:166
KF_List< TYPE * >::GetNext
TYPE * GetNext(GuidoPos &pos) const
Definition: kf_list.h:105
KF_List::SetAt
void SetAt(GuidoPos pos, TYPE data)
Definition: kf_list.h:120
KF_List< TYPE * >::fCount
int fCount
Definition: kf_list.h:197
KF_IPointerList::AddSortedTail
virtual void AddSortedTail(TYPE *, int comp(const TYPE *, const TYPE *))
Definition: kf_ilist.h:260
KF_ListNode::fPrev
KF_ListNode * fPrev
Definition: kf_list.h:29
KF_IPointerList
Definition: ARMusicalVoiceState.h:33
KF_ListNode::fNext
KF_ListNode * fNext
Definition: kf_list.h:28
KF_IPointerList::getOwnership
virtual int getOwnership() const
Definition: kf_ilist.h:40
KF_IPointerList::getCopy
virtual KF_IPointerList< TYPE > * getCopy()
Definition: kf_ilist.h:212
KF_List::AddElementAfter
GuidoPos AddElementAfter(GuidoPos pos, TYPE data)
Definition: kf_list.h:222
KF_List< TYPE * >::fTail
mynode * fTail
Definition: kf_list.h:196
KF_IPointerList::KF_IPointerList
KF_IPointerList(int p_ownselements=0)
Definition: kf_ilist.h:25
KF_IPointerList::AddSortedHead
virtual void AddSortedHead(TYPE *, int comp(const TYPE *, const TYPE *))
Definition: kf_ilist.h:229
KF_List::AddElementAt
GuidoPos AddElementAt(GuidoPos pos, TYPE data)
Definition: kf_list.h:203
KF_IPointerList::setOwnership
virtual void setOwnership(int p_ownselements)
Definition: kf_ilist.h:39
KF_List< TYPE * >::fHead
mynode * fHead
Definition: kf_list.h:195
KF_List::RemoveAll
virtual void RemoveAll()
Definition: kf_list.h:238
KF_List< TYPE * >::GetHeadPosition
GuidoPos GetHeadPosition(void) const
Definition: kf_list.h:102
KF_List::RemoveElementAt
virtual void RemoveElementAt(GuidoPos pos)
Definition: kf_list.h:261
KF_ListNode
Definition: kf_list.h:21
KF_List
Definition: GRBreakMatrix.h:24
KF_IPointerList::RemoveElementAt
virtual void RemoveElementAt(GuidoPos pos)
Definition: kf_ilist.h:126

Guido Project Copyright © 2019 Grame-CNCM