ROOT logo
#include "hseqarr.h"

//*-- AUTHOR : Pechenov Vladimir
//*-- Modified : 23/06/2004 by V.Pechenov


//_HADES_CLASS_DESCRIPTION 
////////////////////////////////////////////////////////////////
// 
// HSeqArrBlock 
// Classes for creating of huge arraies of sequential type.
// New element is added to the end of array always.
// Access to the existing elements can be rundom but
// optimization done for sequential access.
// 
// HSeqArrBlock  - one block of array, keep 0x40000 bytes.
//
// HSeqArr can be created for keeping words of the next types:
//   Char_t     --> HSeqArr(HSeqArr::kChar) 
//   UChar_t    --> HSeqArr(HSeqArr::kUChar)
//   Short_t    --> HSeqArr(HSeqArr::kShort)
//   UShort_t   --> HSeqArr(HSeqArr::kUShort)
//   Int_t      --> HSeqArr(HSeqArr::kInt)
//   UInt_t     --> HSeqArr(HSeqArr::kUInt)
//   Long64_t   --> HSeqArr(HSeqArr::kLong64)
//   ULong64_t  --> HSeqArr(HSeqArr::kULong64)
//   Float_t    --> HSeqArr(HSeqArr::kFloat)
//   Double_t   --> HSeqArr(HSeqArr::kDouble)
//   Bool_t     --> HSeqArr(HSeqArr::kBool)
// or HSeqArr arr; arr.setArrType(HSeqArr::kChar); ...
// type of array can't be changed
//
// More useful functions:
// Bool_t addElement(void) - adding new element to the end of array
//                           and setting cursor to this element
// Bool_t setCursor(UInt_t ind) - setting cursor to the element number "ind"
//
// void setElement(Char_t e) - set element in cursor position to "e"
// ...
// void getElement(Char_t& e) - get element in cursor position
// 
// UInt_t getArraySize() - number of words in array
// UInt_t getMemSize(void) - size of allocated memory in bytes
//
////////////////////////////////////////////////////////////////

ClassImp(HSeqArrBlock)
ClassImp(HSeqArr)
ClassImp(HSeqArrIter)

HSeqArrBlock::HSeqArrBlock(void) {
  blockNumber = 0;
  nextBlock   = NULL;
}

HSeqArrBlock::HSeqArrBlock(HSeqArrBlock* p) {
  blockNumber=p->blockNumber+1; 
  nextBlock=NULL;
}
    
HSeqArrBlock::~HSeqArrBlock(void) {
  if(nextBlock) {
    delete nextBlock;
    nextBlock=NULL;
  }
}

Bool_t HSeqArrBlock::expand(void) {
  // Creating of new array block:
  if(nextBlock==NULL) {
    nextBlock=new HSeqArrBlock(this);
    if(nextBlock) return kTRUE;
    Error("expand","Can't allocate memory for the next block of array!");
  }
  return kFALSE;
}

void HSeqArr::init(ESeqArrType tp) {
  size       = 0;
  firstBlock = new HSeqArrBlock;
  lastBlock  = firstBlock;
  if(firstBlock==0) {
    Error("init","Can't allocate memory for the first block of array!");
    return;
  } 
  type=kNoType;
  blockLength=sizeof(HSeqArrBlock);
  nBlocks=1;
  rewind();
  setArrType(tp);
}

HSeqArr::~HSeqArr(void){
  if(firstBlock) {
    delete firstBlock;
    firstBlock=0;
  }
}

Bool_t HSeqArr::setArrType(ESeqArrType tp) {
  if(type!=kNoType) {
    Error("setArrType","Array type already seted!");
    return kFALSE;
  }
  type=tp;
  if(type==kNoType)       wordLength = sizeof(Char_t);
  else if(type==kChar)    wordLength = sizeof(Char_t);
  else if(type==kUChar)   wordLength = sizeof(UChar_t);
  else if(type==kShort)   wordLength = sizeof(Short_t);
  else if(type==kUShort)  wordLength = sizeof(UShort_t);
  else if(type==kInt)     wordLength = sizeof(Int_t);
  else if(type==kUInt)    wordLength = sizeof(UInt_t);
  else if(type==kLong64)  wordLength = sizeof(Long64_t);
  else if(type==kULong64) wordLength = sizeof(ULong64_t);
  else if(type==kFloat)   wordLength = sizeof(Float_t);
  else if(type==kDouble)  wordLength = sizeof(Double_t);
  else if(type==kBool)    wordLength = sizeof(Bool_t);
  return  kTRUE;
}

UInt_t HSeqArr::getMemSize(void) const {
  // return size of allocated memory
  return blockLength*nBlocks+sizeof(HSeqArr);
}

void HSeqArr::rewind(void) {
  block  = firstBlock;
  index  = 0;
  isFrst = kTRUE;
  cElem  = block->getArr();
}

Bool_t HSeqArr::next(ESeqArrType tp) {
  // Setting cursor to the next word
  if(!tpOk(tp)) return kFALSE;
  if(isFrst) isFrst=kFALSE;
  else {
    if(index+wordLength >= size) return kFALSE;
    index+=wordLength;
    if(HSeqArrBlock::blockIndex(index) > block->getBlockNumber()) {
      block = block->getNextBlock();
      cElem = block->getArr();
    } else cElem += wordLength;
  }
  return kTRUE;
}

Bool_t HSeqArr::nextAExp(ESeqArrType tp) {
  if(!tpOk(tp)) return kFALSE;
  if(isFrst) isFrst=kFALSE;
  else {
    if(HSeqArrBlock::blockIndex(index+wordLength) > block->getBlockNumber()) {
      if(block->getNextBlock()==0 && !addBlock()) return kFALSE;
      block = block->getNextBlock();
      cElem = block->getArr();
    } else cElem += wordLength;
    index+=wordLength;
  }
  if(index>=size) size+=wordLength;
  return kTRUE;
}

Bool_t HSeqArr::addBlock(void) {
  if(!(lastBlock->expand())) return kFALSE;
  lastBlock = lastBlock->getNextBlock();
  nBlocks++;
  return kTRUE;
}

Bool_t HSeqArr::setCurrBlock(UInt_t n) {
  // finding array block number n:
  if(n==block->getBlockNumber()) return kTRUE;
  if(n==nBlocks) {
    // adding next block of array:
    if(!addBlock()) return kFALSE;
  } else if(n<block->getBlockNumber()) rewind();
  else if(n>nBlocks) return kFALSE;
  while(n!=block->getBlockNumber()) block = block->getNextBlock();
  return kTRUE;
}

Bool_t HSeqArr::addElement(void) {
  // adding new element in array (to the end of array)
  if(!setCurrBlock(HSeqArrBlock::blockIndex(size))) return kFALSE;
  cElem = (block->getArr())+HSeqArrBlock::indexInBlock(size);
  index = size;
  size += wordLength;
  return kTRUE;
}

Bool_t HSeqArr::setCursor(Int_t indArr) {
  // setting of block,index for element number ind
  if(indArr<0) {
    isFrst=kTRUE;
    indArr=0;
  }
  UInt_t ind=indArr*wordLength;
  if(ind==index) return kTRUE;
  if(ind>=size) return kFALSE;
  setCurrBlock(HSeqArrBlock::blockIndex(ind));
  cElem = (block->getArr())+HSeqArrBlock::indexInBlock(ind);
  index = ind;
  return kTRUE;
}

Bool_t HSeqArr::err(ESeqArrType tp) {
  if(tp==kNoType) Error("err","Array type is not seted!");
  else Error("err","Mismatching of types!");
  return kFALSE;
}

HSeqArrIter* HSeqArr::makeIterator(void) {
  return new HSeqArrIter(this);
}

HSeqArrIter::HSeqArrIter(HSeqArr* arr) {
  array = arr;
  reset();
}

void HSeqArrIter::reset(void) {
  block  = array->getFirstBlock();
  index  = 0;
  isFrst = kTRUE;
  cElem  = block->getArr();
}

Bool_t HSeqArrIter::next(ESeqArrType tp) {
  // Setting cursor to the next word
  if(array->tpOk(tp)) return next(); 
  return kFALSE;
}

Bool_t HSeqArrIter::next(void) {
  // Setting cursor position to the next word
  if(isFrst) isFrst=kFALSE;
  else {
    if(!array->isBoundOk(index+array->getWordLength())) return kFALSE;
    index += array->getWordLength();
    if(HSeqArrBlock::blockIndex(index) > block->getBlockNumber()) {
      block = block->getNextBlock();
      cElem = block->getArr();
    } else cElem += array->getWordLength();
  }
  return kTRUE;
}

Bool_t HSeqArrIter::nextAndExpand(void) {
  // Setting cursor position to the next word and expand array if necessary
  if(isFrst) isFrst = kFALSE;
  else {
    if(HSeqArrBlock::blockIndex(index+array->getWordLength()) >
      block->getBlockNumber()) {
      if(block->getNextBlock()==0 && !array->addBlock()) return kFALSE;
      block = block->getNextBlock();
      cElem = block->getArr();
    } else cElem += array->getWordLength();
    index += array->getWordLength();
  }
  if(index>=array->getArrSize()) array->increaseArrSize();
  return kTRUE;
}

Bool_t HSeqArrIter::nextAExp(ESeqArrType tp) {
  if(array->tpOk(tp)) return nextAndExpand();
  return kFALSE;
}

Bool_t HSeqArrIter::setCursor(Int_t indArr) {
  // setting of block,index for element number ind
  if(indArr<0) {
    isFrst=kTRUE;
    indArr=0;
  }
  UInt_t ind = indArr * array->getWordLength();
  if(ind==index) return kTRUE;
  if(ind>=array->getArrSize()) return kFALSE;
  UInt_t n=HSeqArrBlock::blockIndex(ind);
  if(n!=block->getBlockNumber()) {
    if(n < block->getBlockNumber()) block = array->getFirstBlock();
    while(n != block->getBlockNumber()) block = block->getNextBlock();
  }
  index = ind;
  cElem = (block->getArr())+HSeqArrBlock::indexInBlock(index);
  return kTRUE;
}
 hseqarr.cc:1
 hseqarr.cc:2
 hseqarr.cc:3
 hseqarr.cc:4
 hseqarr.cc:5
 hseqarr.cc:6
 hseqarr.cc:7
 hseqarr.cc:8
 hseqarr.cc:9
 hseqarr.cc:10
 hseqarr.cc:11
 hseqarr.cc:12
 hseqarr.cc:13
 hseqarr.cc:14
 hseqarr.cc:15
 hseqarr.cc:16
 hseqarr.cc:17
 hseqarr.cc:18
 hseqarr.cc:19
 hseqarr.cc:20
 hseqarr.cc:21
 hseqarr.cc:22
 hseqarr.cc:23
 hseqarr.cc:24
 hseqarr.cc:25
 hseqarr.cc:26
 hseqarr.cc:27
 hseqarr.cc:28
 hseqarr.cc:29
 hseqarr.cc:30
 hseqarr.cc:31
 hseqarr.cc:32
 hseqarr.cc:33
 hseqarr.cc:34
 hseqarr.cc:35
 hseqarr.cc:36
 hseqarr.cc:37
 hseqarr.cc:38
 hseqarr.cc:39
 hseqarr.cc:40
 hseqarr.cc:41
 hseqarr.cc:42
 hseqarr.cc:43
 hseqarr.cc:44
 hseqarr.cc:45
 hseqarr.cc:46
 hseqarr.cc:47
 hseqarr.cc:48
 hseqarr.cc:49
 hseqarr.cc:50
 hseqarr.cc:51
 hseqarr.cc:52
 hseqarr.cc:53
 hseqarr.cc:54
 hseqarr.cc:55
 hseqarr.cc:56
 hseqarr.cc:57
 hseqarr.cc:58
 hseqarr.cc:59
 hseqarr.cc:60
 hseqarr.cc:61
 hseqarr.cc:62
 hseqarr.cc:63
 hseqarr.cc:64
 hseqarr.cc:65
 hseqarr.cc:66
 hseqarr.cc:67
 hseqarr.cc:68
 hseqarr.cc:69
 hseqarr.cc:70
 hseqarr.cc:71
 hseqarr.cc:72
 hseqarr.cc:73
 hseqarr.cc:74
 hseqarr.cc:75
 hseqarr.cc:76
 hseqarr.cc:77
 hseqarr.cc:78
 hseqarr.cc:79
 hseqarr.cc:80
 hseqarr.cc:81
 hseqarr.cc:82
 hseqarr.cc:83
 hseqarr.cc:84
 hseqarr.cc:85
 hseqarr.cc:86
 hseqarr.cc:87
 hseqarr.cc:88
 hseqarr.cc:89
 hseqarr.cc:90
 hseqarr.cc:91
 hseqarr.cc:92
 hseqarr.cc:93
 hseqarr.cc:94
 hseqarr.cc:95
 hseqarr.cc:96
 hseqarr.cc:97
 hseqarr.cc:98
 hseqarr.cc:99
 hseqarr.cc:100
 hseqarr.cc:101
 hseqarr.cc:102
 hseqarr.cc:103
 hseqarr.cc:104
 hseqarr.cc:105
 hseqarr.cc:106
 hseqarr.cc:107
 hseqarr.cc:108
 hseqarr.cc:109
 hseqarr.cc:110
 hseqarr.cc:111
 hseqarr.cc:112
 hseqarr.cc:113
 hseqarr.cc:114
 hseqarr.cc:115
 hseqarr.cc:116
 hseqarr.cc:117
 hseqarr.cc:118
 hseqarr.cc:119
 hseqarr.cc:120
 hseqarr.cc:121
 hseqarr.cc:122
 hseqarr.cc:123
 hseqarr.cc:124
 hseqarr.cc:125
 hseqarr.cc:126
 hseqarr.cc:127
 hseqarr.cc:128
 hseqarr.cc:129
 hseqarr.cc:130
 hseqarr.cc:131
 hseqarr.cc:132
 hseqarr.cc:133
 hseqarr.cc:134
 hseqarr.cc:135
 hseqarr.cc:136
 hseqarr.cc:137
 hseqarr.cc:138
 hseqarr.cc:139
 hseqarr.cc:140
 hseqarr.cc:141
 hseqarr.cc:142
 hseqarr.cc:143
 hseqarr.cc:144
 hseqarr.cc:145
 hseqarr.cc:146
 hseqarr.cc:147
 hseqarr.cc:148
 hseqarr.cc:149
 hseqarr.cc:150
 hseqarr.cc:151
 hseqarr.cc:152
 hseqarr.cc:153
 hseqarr.cc:154
 hseqarr.cc:155
 hseqarr.cc:156
 hseqarr.cc:157
 hseqarr.cc:158
 hseqarr.cc:159
 hseqarr.cc:160
 hseqarr.cc:161
 hseqarr.cc:162
 hseqarr.cc:163
 hseqarr.cc:164
 hseqarr.cc:165
 hseqarr.cc:166
 hseqarr.cc:167
 hseqarr.cc:168
 hseqarr.cc:169
 hseqarr.cc:170
 hseqarr.cc:171
 hseqarr.cc:172
 hseqarr.cc:173
 hseqarr.cc:174
 hseqarr.cc:175
 hseqarr.cc:176
 hseqarr.cc:177
 hseqarr.cc:178
 hseqarr.cc:179
 hseqarr.cc:180
 hseqarr.cc:181
 hseqarr.cc:182
 hseqarr.cc:183
 hseqarr.cc:184
 hseqarr.cc:185
 hseqarr.cc:186
 hseqarr.cc:187
 hseqarr.cc:188
 hseqarr.cc:189
 hseqarr.cc:190
 hseqarr.cc:191
 hseqarr.cc:192
 hseqarr.cc:193
 hseqarr.cc:194
 hseqarr.cc:195
 hseqarr.cc:196
 hseqarr.cc:197
 hseqarr.cc:198
 hseqarr.cc:199
 hseqarr.cc:200
 hseqarr.cc:201
 hseqarr.cc:202
 hseqarr.cc:203
 hseqarr.cc:204
 hseqarr.cc:205
 hseqarr.cc:206
 hseqarr.cc:207
 hseqarr.cc:208
 hseqarr.cc:209
 hseqarr.cc:210
 hseqarr.cc:211
 hseqarr.cc:212
 hseqarr.cc:213
 hseqarr.cc:214
 hseqarr.cc:215
 hseqarr.cc:216
 hseqarr.cc:217
 hseqarr.cc:218
 hseqarr.cc:219
 hseqarr.cc:220
 hseqarr.cc:221
 hseqarr.cc:222
 hseqarr.cc:223
 hseqarr.cc:224
 hseqarr.cc:225
 hseqarr.cc:226
 hseqarr.cc:227
 hseqarr.cc:228
 hseqarr.cc:229
 hseqarr.cc:230
 hseqarr.cc:231
 hseqarr.cc:232
 hseqarr.cc:233
 hseqarr.cc:234
 hseqarr.cc:235
 hseqarr.cc:236
 hseqarr.cc:237
 hseqarr.cc:238
 hseqarr.cc:239
 hseqarr.cc:240
 hseqarr.cc:241
 hseqarr.cc:242
 hseqarr.cc:243
 hseqarr.cc:244
 hseqarr.cc:245
 hseqarr.cc:246
 hseqarr.cc:247
 hseqarr.cc:248
 hseqarr.cc:249
 hseqarr.cc:250
 hseqarr.cc:251
 hseqarr.cc:252
 hseqarr.cc:253
 hseqarr.cc:254
 hseqarr.cc:255
 hseqarr.cc:256
 hseqarr.cc:257
 hseqarr.cc:258
 hseqarr.cc:259
 hseqarr.cc:260
 hseqarr.cc:261
 hseqarr.cc:262
 hseqarr.cc:263
 hseqarr.cc:264
 hseqarr.cc:265
 hseqarr.cc:266
 hseqarr.cc:267
 hseqarr.cc:268
 hseqarr.cc:269
 hseqarr.cc:270
 hseqarr.cc:271
 hseqarr.cc:272
 hseqarr.cc:273
 hseqarr.cc:274
 hseqarr.cc:275
 hseqarr.cc:276
 hseqarr.cc:277
 hseqarr.cc:278
 hseqarr.cc:279
 hseqarr.cc:280
 hseqarr.cc:281
 hseqarr.cc:282
 hseqarr.cc:283
 hseqarr.cc:284
 hseqarr.cc:285
 hseqarr.cc:286