GUIDOLib  1.7.7
Guido Engine Internal Documentation
kf_ivect.h
1 /*
2  GUIDO Library
3  Copyright (C) 2002 Holger Hoos, Juergen Kilian, Kai Renz
4  Copyright (C) 2002-2017 Grame
5 
6  This Source Code Form is subject to the terms of the Mozilla Public
7  License, v. 2.0. If a copy of the MPL was not distributed with this
8  file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
11  research@grame.fr
12 
13 */
14 #ifndef __KF_IVECT_H
15 #define __KF_IVECT_H
16 
17 #include "kf_vect.h"
18 
24 template <class TYPE>
25 class KF_IVector : public KF_Vector<TYPE *>
26 {
27  public:
28  KF_IVector(int p_ownselements = 0)
29  : KF_Vector<TYPE *>(NULL)
30  {
31  ownselements = p_ownselements;
32  }
33 
34  KF_IVector(int p_ownselements,TYPE ** newdata,int newsize,
35  int newindexoffset, int newminimum,int newmaximum,
36  int newcount)
37  : KF_Vector<TYPE *>(NULL,newdata,newsize,newindexoffset,newminimum,newmaximum,
38  newcount)
39  {
40  ownselements = p_ownselements;
41  }
42 
43  virtual ~KF_IVector();
44 
45  virtual void Delete(int index)
46  {
47  if (ownselements)
48  {
49  if (this->data[index - this->indexoffset ] != NULL)
50  {
51  assert( index >= this->minimum );
52  assert( index <= this->maximum );
53  delete this->data[ index - this->indexoffset ];
54 
56  // this does the adjustment of minimum
57  // and maximum (hopefully)
58 
59  }
60  }
61  else
63  }
64 
65  virtual void Cut(int index,KF_IVector<TYPE> **ppvect);
66  virtual void CutBegin(int index,KF_IVector<TYPE> **ppvect);
67 
68  virtual void setOwnership(int p_ownselements)
69  { ownselements = p_ownselements; }
70 
71  virtual int getOwnership() const
72  { return ownselements; }
73 
75 
76  protected:
77  int ownselements; // 1: delete the data elements of the array
78 };
79 
80 template <class TYPE>
82 {
83  if (ownselements)
84  {
85  int i;
86  for (i=0;i<this->memsize;i++)
87  {
88  if (this->data[i] != NULL)
89  {
90  assert( i+this->indexoffset >= this->minimum);
91  assert( i+this->indexoffset <= this->maximum);
92  delete this->data[i];
93  }
94  }
95  }
96  // when calling the standard destruc´tor of KF_Vector, the
97  // vector structures will also be deleted
98 }
99 
100 // this operation cuts a vector in two
101 // the old vector reaches from
102 // minimum to index, the new vector
103 // (which is constructed within the function)
104 // ranges from index+1 till maximum.
105 template <class TYPE>
107 {
108  *pnew = NULL;
109  if (index < this->minimum) return;
110  if (index > this->maximum) return;
111 
112  int mymemindex = index - this->indexoffset;
113 
114  int newmin = INT_MAX;
115  int newmax = INT_MIN;
116 
117  int newsize = this->maximum - index;
118  if (newsize > 0)
119  {
120  // we make sure, that there are -10 left and +10 on the
121  // right hand side ....
122  int newindexoffset = 10;
123 
124  TYPE **newdata = (TYPE **) malloc((newsize + 2*newindexoffset ) * sizeof( TYPE *) );
125 
126  int i;
127  int newcount = 0;
128  for (i=0;i<newindexoffset;i++)
129  {
130  newdata[i] = this->noelement;
131  }
132  for (i=newindexoffset;i<newsize+newindexoffset;i++)
133  {
134  newdata[i] = (TYPE *) this->data[mymemindex + 1 + i - newindexoffset] ;
135  if (newdata[i] != this->noelement)
136  {
137  int tmpindex = mymemindex + this->indexoffset + i - newindexoffset;
138  if (tmpindex < newmin)
139  newmin = tmpindex;
140  if (tmpindex > newmax)
141  newmax = tmpindex;
142 
143  newcount++;
144  this->data[mymemindex + 1 + i - newindexoffset] = this->noelement;
145  }
146  }
147  for (i=newsize+newindexoffset;i<newsize+2*newindexoffset;i++)
148  {
149  newdata[i] = this->noelement;
150  }
151 
152  if (newmin > newmax)
153  {
154  newmin = 0;
155  newmax = -1;
156  assert(newcount == 0);
157  }
158 
159 
160  // create the new vector.
161  *pnew = new KF_IVector<TYPE>(ownselements,newdata,newsize+2*newindexoffset,
162  newmin - newindexoffset,newmin, newmax, newcount); // index+1,maximum,newcount);
163 
164 
165  // we have to change the maximum and minimum and also the elements ....
166  this->count = this->count - newcount;
167  if (this->count == 0)
168  {
169  this->minimum = 0;
170  this->maximum = -1;
171  }
172  else
173  {
174  int newmaximum = index;
175  while (newmaximum >= this->minimum &&
176  this->data[newmaximum - this->indexoffset] == this->noelement)
177  newmaximum--;
178  this->maximum = newmaximum;
179  }
180  }
181  else
182  {
183  // the new vector has no size ....
184  *pnew = new KF_IVector<TYPE>(ownselements);
185  }
186 }
187 
188 // this operation cuts a vector in two
189 // the old vector reaches from
190 // index+1 to maximum and the new vector (pnew) goes
191 // from minimum to index.
192 template <class TYPE>
194 {
195  *pnew = 0;
196  if (index < this->minimum) return;
197  if (index > this->maximum) return;
198 
199  int mymemindex = this->minimum - this->indexoffset;
200 
201  int newmin = INT_MAX;
202  int newmax = INT_MIN;
203  int newsize = index - this->minimum + 1;
204  if (newsize > 0)
205  {
206  // we make sure, that there are -10 left and +10 on the
207  // right hand side ....
208  int newindexoffset = 10;
209 
210  TYPE **newdata = (TYPE **) malloc((newsize + 2*newindexoffset ) * sizeof( TYPE *) );
211 
212  int i;
213  int newcount = 0;
214  for (i=0;i<newindexoffset;i++)
215  {
216  newdata[i] = this->noelement;
217  }
218  for (i=newindexoffset;i<newsize+newindexoffset;i++)
219  {
220  newdata[i] = (TYPE *) this->data[mymemindex + i - newindexoffset] ;
221  if (newdata[i] != this->noelement)
222  {
223  int tmpindex = mymemindex + this->indexoffset + i - newindexoffset;
224  if (tmpindex < newmin)
225  newmin = tmpindex;
226  if (tmpindex > newmax)
227  newmax = tmpindex;
228  newcount++;
229  this->data[mymemindex + i - newindexoffset] = this->noelement;
230  }
231  }
232  for (i=newsize+newindexoffset;i<newsize+2*newindexoffset;i++)
233  {
234  newdata[i] = this->noelement;
235  }
236 
237  if (newmin > newmax)
238  {
239  newmin = 0;
240  newmax = -1;
241  assert(newcount == 0);
242  }
243  // create the new vector.
244  *pnew = new KF_IVector<TYPE>(ownselements,newdata,newsize+2*newindexoffset,
245  newmin - newindexoffset,newmin,newmax,newcount); // minimum,index,newcount);
246 
247 
248  // we have to change the maximum and minimum and also the elements ....
249  // find the new minimum ....
250 
251  this->count = this->count - newcount;
252  if (this->count == 0)
253  {
254  this->minimum = 0;
255  this->maximum = -1;
256 
257  }
258  else
259  {
260  int newminimum = index + 1;
261  while (newminimum <= this->maximum &&
262  this->data[newminimum - this->indexoffset] == this->noelement)
263  newminimum++;
264  this->minimum = newminimum;
265  }
266 
267  }
268  else
269  {
270  // the new vector has no size ....
271  *pnew = new KF_IVector<TYPE>(ownselements);
272  }
273 
274 }
275 
276 #endif /* __KF_IVECT_H */
KF_IVector::getOwnership
virtual int getOwnership() const
Definition: kf_ivect.h:71
KF_IVector::setOwnership
virtual void setOwnership(int p_ownselements)
Definition: kf_ivect.h:68
KF_IVector::Cut
virtual void Cut(int index, KF_IVector< TYPE > **ppvect)
Definition: kf_ivect.h:106
KF_IVector::~KF_IVector
virtual ~KF_IVector()
Definition: kf_ivect.h:81
KF_IVector::KF_IVector
KF_IVector(int p_ownselements, TYPE **newdata, int newsize, int newindexoffset, int newminimum, int newmaximum, int newcount)
Definition: kf_ivect.h:34
KF_IVector::ownselements
int ownselements
Definition: kf_ivect.h:77
KF_Vector< TYPE * >::indexoffset
int indexoffset
Definition: kf_vect.h:86
KF_Vector::Delete
virtual void Delete(int index)
Deletes an Element at position index.
Definition: kf_vect.h:212
KF_IVector::Delete
virtual void Delete(int index)
Deletes an Element at position index.
Definition: kf_ivect.h:45
KF_IVector
Definition: GRPossibleBreakState.h:20
KF_IVector::KF_IVector
KF_IVector(int p_ownselements=0)
Definition: kf_ivect.h:28
KF_Vector< TYPE * >::data
TYPE * * data
Definition: kf_vect.h:88
KF_Vector< TYPE * >::minimum
int minimum
Definition: kf_vect.h:83
KF_IVector::CutBegin
virtual void CutBegin(int index, KF_IVector< TYPE > **ppvect)
Definition: kf_ivect.h:193
KF_Vector< TYPE * >::maximum
int maximum
Definition: kf_vect.h:82
KF_Vector
Definition: kf_vect.h:35

Guido Project Copyright © 2019 Grame-CNCM