StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StSstPointList.cc
1 //$Id: StSstPointList.cc,v 1.2 2016/05/30 21:44:30 bouchet Exp $
2 //
3 //$Log: StSstPointList.cc,v $
4 //Revision 1.2 2016/05/30 21:44:30 bouchet
5 //coverity : RESOURCE_LEAK fixed in exchangePoints method
6 //
7 //Revision 1.1 2015/06/23 16:26:20 jeromel
8 //First version created from the SSD code and reshaped
9 //
10 //Revision 1.1 2015/04/19 17:30:32 bouchet
11 //initial commit ; SST codes
12 //
13 
14 //fork from the SSD code, move along - see history therein
15 
16 #include "StSstPointList.hh"
17 
18 StSstPointList::StSstPointList()
19 {
20  mListLength = 0;
21  mFirstPoint = 0;
22  mLastPoint = 0;
23 }
24 
25 StSstPointList::~StSstPointList()
26 {
27  if (mListLength)
28  {
29  StSstPoint *ptr = mLastPoint;
30  StSstPoint *toDele;
31  while (mListLength)
32  {
33  toDele = ptr;
34  ptr = prev(ptr);
35  delete toDele;
36  mListLength--;
37  }
38  }
39 }
40 
41 StSstPoint* StSstPointList::next(StSstPoint *ptr)
42 { return ptr->getNextPoint(); }
43 
44 StSstPoint* StSstPointList::prev(StSstPoint *ptr)
45 { return ptr->getPrevPoint(); }
46 
47 StSstPoint* StSstPointList::first()
48 { return mFirstPoint; }
49 
50 StSstPoint* StSstPointList::last()
51 { return mLastPoint; }
52 
53 Int_t StSstPointList::getSize()
54 { return mListLength; }
55 
56 Int_t StSstPointList::addNewPoint(StSstPoint *ptr)
57 {
58  if (!ptr) return 0;
59  if (mListLength == 0)
60  {
61  ptr->setPrevPoint(0);
62  ptr->setNextPoint(0);
63  mFirstPoint = ptr;
64  mLastPoint = ptr;
65  }
66  else
67  {
68  ptr->setPrevPoint(mLastPoint);
69  ptr->setNextPoint(0);
70  mLastPoint->setNextPoint(ptr);
71  mLastPoint = ptr;
72  }
73  mListLength++;
74  return 1;
75 }
76 
77 Int_t StSstPointList::removePoint(StSstPoint *ptr)
78 {
79  if (!this->getSize()) return 0;
80  StSstPoint *ptBefore = ptr->getPrevPoint();
81  StSstPoint *ptAfter = ptr->getNextPoint();
82 
83  if (ptBefore == 0)
84  {
85  if (ptAfter== 0)
86  {
87  // taille = 1
88  this->mFirstPoint = 0;
89  this->mLastPoint = 0;
90  this->mListLength = 0;
91  delete ptr;
92  return 1;
93  }
94  else
95  {
96  this->mFirstPoint = ptAfter;
97  ptAfter->setPrevPoint(0);
98  this->mListLength--;
99  delete ptr;
100  return 1;
101  }
102  }
103  else
104  {
105  if (ptAfter== 0)
106  {
107  this->mLastPoint = ptBefore;
108  ptBefore->setNextPoint(0);
109  this->mListLength--;
110  delete ptr;
111  return 1;
112  }
113  else
114  {
115  ptBefore->setNextPoint(ptAfter);
116  ptAfter->setPrevPoint(ptBefore);
117  this->mListLength--;
118  delete ptr;
119  return 1;
120  }
121  }
122 
123 }
124 
125 void StSstPointList::exchangeTwoPoints(StSstPoint *ptr1,StSstPoint *ptr2)
126 {
127  StSstPoint *ptrTemp = ptr1;
128  ptr1 = ptr2;
129  ptr2 = ptrTemp;
130  delete ptrTemp;
131 }
132 
133 StSstPointList* StSstPointList::addPointList(StSstPointList *list)
134 {
135  Int_t size2 = list->getSize();
136 
137  if (!size2) return this;
138  StSstPoint *pt1 ;
139  StSstPoint *pt2 = list->first();
140 
141  for (Int_t i = 0; i < size2; i++)
142  {
143  pt1 = pt2->giveCopy();
144  this->addNewPoint(pt1);
145  pt2 = list->next(pt2);
146  }
147  return this;
148 }
149 
150 StSstPointList* StSstPointList::substractPointList(StSstPointList *list)
151 {
152 
153  Int_t localSizeToDelete = list->getSize();
154  Int_t localSizeToKeep = this->getSize();
155 
156  if((!localSizeToDelete)||(!localSizeToKeep)) return this;
157  StSstPoint *currDele = list->first();
158 
159  for (Int_t iDele = 0; iDele < localSizeToDelete; iDele++)
160  {
161  StSstPoint *currKeep = this->first();
162  Int_t iKeep = 0;
163  for (iKeep =0 ; ((iKeep < this->getSize())&&((currDele->getNId())!=(currKeep->getNId()))); iKeep++)
164  {
165  currKeep = list->next(currKeep);
166  }
167  //if (currDele->getNPoint()==currKeep->getNPoint())
168  //StSstPoint->NId is used for the simulation, not NPoint
169  //this method is used only for simulation
170  if (currDele->getNId()==currKeep->getNId())
171  {
172  this->removePoint(currKeep);
173  }
174  currDele = list->next(currDele);
175  }
176  return this;
177 }
178 
179 StSstPointList* StSstPointList::removeMultipleCount()
180 {
181  Int_t localSize = this->getSize();
182  if (localSize < 2) return this;
183  StSstPoint *ptBigLoop = this->first();
184 
185  while ((ptBigLoop != this->last())&&(ptBigLoop != 0))
186  {
187  StSstPoint *ptSmallLoop = this->next(ptBigLoop);
188 
189  while (ptSmallLoop!=0)
190  {
191  //if (ptSmallLoop->getNPoint() == ptBigLoop->getNPoint())
192  //StSstPoint->NId is used for the simulation, not NPoint
193  //this method is used only for simulation
194  if (ptSmallLoop->getNId() == ptBigLoop->getNId())
195  {
196  StSstPoint *temp = ptSmallLoop;
197  ptSmallLoop = this->next(ptSmallLoop);
198  this->removePoint(temp);
199  }
200  else
201  {
202  ptSmallLoop = this->next(ptSmallLoop);
203  }
204  }
205  ptBigLoop = this->next(ptBigLoop);
206  }
207  return this;
208 }
209 
210 StSstPointList* StSstPointList::sortPoint()
211 {
212  Int_t localSize=this->getSize();
213  if (localSize<2) return this;
214 
215  StSstPoint *ptCurr = this->first();
216  ptCurr = this->next(ptCurr);
217  for ( ; ptCurr!=0 ; )
218 
219  {
220  StSstPoint *ptB1 = ptCurr;
221  StSstPoint *ptB2;
222  Int_t isCont = 1;
223  while ((ptB1 != this->first())&&(isCont))
224  {
225  ptB2 = this->prev(ptB1);
226  if (ptB2->getNPoint() > ptB1->getNPoint())
227  {
228  this->exchangeTwoPoints(ptB1,ptB2);
229  ptB1 = ptB2;
230  }
231  else
232  {
233  isCont = 0;
234  }
235  }
236  ptCurr = this->next(ptCurr);
237 
238  }
239  return this;
240 }
241 
242 Int_t StSstPointList::renumHits(Int_t last)
243 {
244  Int_t localSize = this->getSize();
245  if (!localSize) return last;
246  StSstPoint *ptr = this->first();
247  for (Int_t iPt = 0; iPt < localSize; iPt++)
248  {
249  ptr->setNPoint(last + iPt + 1);
250  ptr = this->next(ptr);
251  }
252  return (last + localSize);
253 }
254 
255 StSstPointList::StSstPointList(const StSstPointList & originalPointList)
256 {
257  mListLength = originalPointList.mListLength;
258  mFirstPoint = originalPointList.mFirstPoint;
259  mLastPoint = originalPointList.mLastPoint;
260 }
261 
262 StSstPointList& StSstPointList::operator=(const StSstPointList originalPointList)
263 {
264  mListLength = originalPointList.mListLength;
265  mFirstPoint = originalPointList.mFirstPoint;
266  mLastPoint = originalPointList.mLastPoint;
267 
268  return *this;
269 }
270 
271