]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/STEERBase/AliRefArray.cxx
Coverity
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliRefArray.cxx
CommitLineData
491194c8 1/**************************************************************************
2 * Copyright(c) 2007-2009, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16#include "AliRefArray.h"
4f967557 17#include <string.h>
491194c8 18
19ClassImp(AliRefArray)
20
21//____________________________________________________________________
22AliRefArray::AliRefArray() : fNElems(0),fRefSize(0),fElems(0),fRefInd(0),fRefBuff(0)
23{
24 // default constructor
25}
26
27//____________________________________________________________________
28AliRefArray::AliRefArray(UInt_t nelem,UInt_t depth) :
29 TObject(),fNElems(nelem),fRefSize(depth),fElems(0),fRefInd(0),fRefBuff(0)
30{
31 fNElems = nelem;
32 // create array with nelem initial referres
33 if (fNElems<1) fNElems = 1;
34 fElems = new Int_t[fNElems];
35 if (fRefSize>0) {
36 fRefInd = new UInt_t[fRefSize];
37 fRefBuff = new UInt_t[fRefSize];
38 }
39 Reset();
40 //
41}
42
43//____________________________________________________________________
44AliRefArray::AliRefArray(const AliRefArray& src) :
4c234df8 45 TObject(src),
46 fNElems(src.fNElems),
47 fRefSize(src.fRefSize),
48 fElems(new Int_t[fNElems]),
49 fRefInd(new UInt_t[fRefSize]),
50 fRefBuff(new UInt_t[fRefSize])
491194c8 51{
4c234df8 52 //
53 // create a copy
54 //
55 memcpy(fElems,src.fElems,fNElems*sizeof(Int_t));
56 memcpy(fRefInd,src.fRefInd,fRefSize*sizeof(UInt_t));
57 memcpy(fRefBuff,src.fRefBuff,fRefSize*sizeof(UInt_t));
491194c8 58}
59
60//____________________________________________________________________
61AliRefArray& AliRefArray::operator=(const AliRefArray& src)
62{
63 // create a copy with useful info (skip unused slots)
64 if(&src != this) {
65 TObject::operator=(src);
66 fNElems = src.fNElems;
67 fRefSize=0;
68 if (fElems) delete[] fElems;
69 if (fRefInd) delete[] fRefInd;
70 if (fRefBuff) delete[] fRefBuff;
71 fElems = 0;
72 fRefInd = 0;
73 fRefBuff = 0;
74 if (src.fRefInd) {
5b2a2954 75 fRefSize = src.fRefInd[0];
491194c8 76 fRefInd = new UInt_t[fRefSize];
77 fRefBuff = new UInt_t[fRefSize];
78 memcpy(fRefInd, src.fRefInd, fRefSize*sizeof(UInt_t));
79 memcpy(fRefBuff,src.fRefBuff,fRefSize*sizeof(UInt_t));
80 }
81 if (fNElems) {
82 fElems = new Int_t[fNElems];
83 memcpy(fElems,src.fElems,fNElems*sizeof(Int_t));
84 }
85 }
86 return *this;
87 //
88}
89
90//____________________________________________________________________
91AliRefArray::~AliRefArray()
92{
93 // destructor
4c234df8 94 delete[] fElems;
95 delete[] fRefBuff;
96 delete[] fRefInd;
491194c8 97}
98
99//____________________________________________________________________
100void AliRefArray::Expand(UInt_t size)
101{
102 // expand the size
103 if (size<fNElems) {
104 if (size>0) {printf("The size can be only increased\n");return;}
105 else size = (fNElems<<2) + 1;
106 }
107 else if (size==fNElems) return;
108 Int_t *tmpArr = new Int_t[size];
109 memcpy(tmpArr,fElems,fNElems*sizeof(Int_t));
110 memset(tmpArr+fNElems,0,(size-fNElems)*sizeof(UInt_t));
111 delete[] fElems;
112 fElems = tmpArr;
113 fNElems = size;
114}
115
116//____________________________________________________________________
117void AliRefArray::Reset()
118{
119 // reset references
120 if (fNElems) memset(fElems,0,fNElems*sizeof(Int_t));
121 if (fRefSize) {
122 memset(fRefInd,0,fRefSize*sizeof(UInt_t));
123 memset(fRefBuff,0,fRefSize*sizeof(UInt_t));
124 fRefInd[0] = 1;
125 }
126}
127
128//____________________________________________________________________
129void AliRefArray::ExpandReferences(Int_t addSize)
130{
131 // add extra slots
132 if (addSize<3) addSize = 3;
133 UInt_t oldSize = fRefSize;
134 fRefSize += addSize;
135 UInt_t* buff = new UInt_t[fRefSize];
136 UInt_t* ind = new UInt_t[fRefSize];
70b2ed88 137 if (fRefBuff) memcpy(buff, fRefBuff, oldSize*sizeof(UInt_t)); // copy current content
138 if (fRefInd) memcpy(ind, fRefInd, oldSize*sizeof(UInt_t));
5b2a2954 139 memset(buff+oldSize,0,addSize*sizeof(UInt_t));
140 memset(ind +oldSize,0,addSize*sizeof(UInt_t));
491194c8 141 delete[] fRefBuff; fRefBuff = buff;
142 delete[] fRefInd; fRefInd = ind;
143 if (!oldSize) fRefInd[0] = 1;
144}
145
146//____________________________________________________________________
147void AliRefArray::Print(Option_t*) const
148{
149 // reset references
150 for (UInt_t i=0;i<fNElems;i++) {
151 printf("Entry%4d: ",i);
152 Int_t ref;
153 if (!(ref=fElems[i])) {printf("None\n"); continue;}
154 if (fElems[i]<0) {printf("%d\n",-(1+ref)); continue;}
155 do { printf("%d ",fRefBuff[ref]-1); } while((ref=fRefInd[ref])); printf("\n");
156 }
157}
158
159//____________________________________________________________________
160void AliRefArray::AddReferences(UInt_t from, UInt_t *refs, UInt_t nref)
161{
162 // add nodes to the references of "from"
163 if (nref==1) {AddReference(from, refs[0]); return;}
164 if (!nref) return;
165 //
166 if (from>=fNElems) Expand(from+1);
167 UInt_t chk = nref + (fElems[from]<0); // if <0, need to transfer to indices the only existing reference
bc158c21 168 if (!fRefInd) ExpandReferences(chk+1);
169 else if ( fRefInd[0]+chk >= fRefSize ) ExpandReferences(chk);
491194c8 170 UInt_t &freeSlot = fRefInd[0];
171 // if there is already single ref, transfer it to indices
172 Int_t ref = fElems[from];
173 if (ref<0) { fRefInd[freeSlot]=0; fRefBuff[freeSlot] = -ref; ref = fElems[from] = freeSlot++; }
174 //
175 while(fRefInd[ref]) ref=fRefInd[ref]; // find last index of last entry for cluster from
176 if (fElems[from]) fRefInd[ref] = freeSlot; // not a first entry, register it in the indices
177 else fElems[from] = freeSlot; // first entry, register it in the refs
178 for (UInt_t ir=0;ir<nref;ir++) {
179 if (!ir && !fElems[from]) fElems[from] = freeSlot;
180 else ref = fRefInd[ref] = freeSlot;
181 fRefBuff[freeSlot++] = refs[ir]+1;
182 }
183}