// @(#)root/tree:$Id$
// Author: Rene Brun   12/01/96

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// A TLeaf for an Integer data type.                                    //
//////////////////////////////////////////////////////////////////////////

#include "TLeafI.h"
#include "TBranch.h"
#include "TClonesArray.h"
#include "Riostream.h"

ClassImp(TLeafI)

//______________________________________________________________________________
TLeafI::TLeafI(): TLeaf()
{
//*-*-*-*-*-*Default constructor for LeafI*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ============================

   fLenType = 4;
   fMinimum = 0;
   fMaximum = 0;
   fValue   = 0;
   fPointer = 0;
}

//______________________________________________________________________________
TLeafI::TLeafI(TBranch *parent, const char *name, const char *type)
   :TLeaf(parent, name,type)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Create a LeafI*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ==============
//*-*

   fLenType = 4;
   fMinimum = 0;
   fMaximum = 0;
   fValue   = 0;
   fPointer = 0;
}

//______________________________________________________________________________
TLeafI::~TLeafI()
{
//*-*-*-*-*-*Default destructor for a LeafI*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ===============================

   if (ResetAddress(0,kTRUE)) delete [] fValue;
}


//______________________________________________________________________________
void TLeafI::Export(TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*Export element from local leaf buffer to ClonesArray*-*-*-*-*
//*-*        ======================================================

   Int_t *value = fValue;
   for (Int_t i=0;i<n;i++) {
      char *first = (char*)list->UncheckedAt(i);
      Int_t *ii = (Int_t*)&first[fOffset];
      for (Int_t j=0;j<fLen;j++) {
         ii[j] = value[j];
      }
      value += fLen;
   }
}

//______________________________________________________________________________
void TLeafI::FillBasket(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*-*-*Pack leaf elements in Basket output buffer*-*-*-*-*-*-*
//*-*                  =========================================

   Int_t i;
   Int_t len = GetLen();
   if (fPointer) fValue = *fPointer;
   if (IsRange()) {
      if (fValue[0] > fMaximum) fMaximum = fValue[0];
   }
   if (IsUnsigned()) {
      for (i=0;i<len;i++) b << (UInt_t)fValue[i];
   } else {
      b.WriteFastArray(fValue,len);
   }
}

//______________________________________________________________________________
const char *TLeafI::GetTypeName() const
{
//*-*-*-*-*-*-*-*Returns name of leaf type*-*-*-*-*-*-*-*-*-*-*-*
//*-*            =========================

   if (fIsUnsigned) return "UInt_t";
   return "Int_t";
}


//______________________________________________________________________________
Double_t TLeafI::GetValue(Int_t i) const
{
// Returns current value of leaf
// if leaf is a simple type, i must be set to 0
// if leaf is an array, i is the array element number to be returned

   if (fIsUnsigned) return (UInt_t)fValue[i];
   return fValue[i];
}



//______________________________________________________________________________
void TLeafI::Import(TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*Import element from ClonesArray into local leaf buffer*-*-*-*-*
//*-*        ======================================================

   const Int_t kIntUndefined = -9999;
   Int_t j = 0;
   char *clone;
   for (Int_t i=0;i<n;i++) {
      clone = (char*)list->UncheckedAt(i);
      if (clone) memcpy(&fValue[j],clone + fOffset, 4*fLen);
      else       memcpy(&fValue[j],&kIntUndefined,  4*fLen);
      j += fLen;
   }
}

//______________________________________________________________________________
void TLeafI::PrintValue(Int_t l) const
{
// Prints leaf value

   if (fIsUnsigned) {
      UInt_t *uvalue = (UInt_t*)GetValuePointer();
      printf("%u",uvalue[l]);
   } else {
      Int_t *value = (Int_t*)GetValuePointer();
      printf("%d",value[l]);
   }
}

//______________________________________________________________________________
void TLeafI::ReadBasket(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*-*-*Read leaf elements from Basket input buffer*-*-*-*-*-*
//*-*                  ===========================================

   if (!fLeafCount && fNdata == 1) {
      b.ReadInt(fValue[0]);
   } else {
      if (fLeafCount) {
         Long64_t entry = fBranch->GetReadEntry();
         if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
            fLeafCount->GetBranch()->GetEntry(entry);
         }
         Int_t len = Int_t(fLeafCount->GetValue());
         if (len > fLeafCount->GetMaximum()) {
            printf("ERROR leaf:%s, len=%d and max=%d\n",GetName(),len,fLeafCount->GetMaximum());
            len = fLeafCount->GetMaximum();
         }
         fNdata = len*fLen;
         b.ReadFastArray(fValue,len*fLen);
      } else {
         b.ReadFastArray(fValue,fLen);
      }
   }
}

//______________________________________________________________________________
void TLeafI::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*-*-*-*-*-*Read leaf elements from Basket input buffer*-*-*-*-*-*
//  and export buffer to TClonesArray objects

   if (n*fLen == 1) {
      b >> fValue[0];
   } else {
      b.ReadFastArray(fValue,n*fLen);
   }
   Int_t *value = fValue;
   for (Int_t i=0;i<n;i++) {
      char *first = (char*)list->UncheckedAt(i);
      Int_t *ii = (Int_t*)&first[fOffset];
      for (Int_t j=0;j<fLen;j++) {
         ii[j] = value[j];
      }
      value += fLen;
   }
}

//______________________________________________________________________________
void TLeafI::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
{
// read an integer from std::istream s and store it into the branch buffer
   if (fIsUnsigned) {
      UInt_t *uvalue = (UInt_t*)GetValuePointer();
      for (Int_t i=0;i<fLen;i++) s >> uvalue[i];
   } else {
      Int_t *value = (Int_t*)GetValuePointer();
      for (Int_t i=0;i<fLen;i++) s >> value[i];
   }
}

//______________________________________________________________________________
void TLeafI::SetAddress(void *add)
{
//*-*-*-*-*-*-*-*-*-*-*Set leaf buffer data address*-*-*-*-*-*
//*-*                  ============================

   if (ResetAddress(add) && (add!= fValue)) {
      delete [] fValue;
   }
   if (add) {
      if (TestBit(kIndirectAddress)) {
         fPointer = (Int_t**) add;
         Int_t ncountmax = fLen;
         if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
         if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
             ncountmax > fNdata || *fPointer == 0) {
            if (*fPointer) delete [] *fPointer;
            if (ncountmax > fNdata) fNdata = ncountmax;
            *fPointer = new Int_t[fNdata];
         }
         fValue = *fPointer;
      } else {
         fValue = (Int_t*)add;
      }
   } else {
      fValue = new Int_t[fNdata];
      fValue[0] = 0;
   }
}

 TLeafI.cxx:1
 TLeafI.cxx:2
 TLeafI.cxx:3
 TLeafI.cxx:4
 TLeafI.cxx:5
 TLeafI.cxx:6
 TLeafI.cxx:7
 TLeafI.cxx:8
 TLeafI.cxx:9
 TLeafI.cxx:10
 TLeafI.cxx:11
 TLeafI.cxx:12
 TLeafI.cxx:13
 TLeafI.cxx:14
 TLeafI.cxx:15
 TLeafI.cxx:16
 TLeafI.cxx:17
 TLeafI.cxx:18
 TLeafI.cxx:19
 TLeafI.cxx:20
 TLeafI.cxx:21
 TLeafI.cxx:22
 TLeafI.cxx:23
 TLeafI.cxx:24
 TLeafI.cxx:25
 TLeafI.cxx:26
 TLeafI.cxx:27
 TLeafI.cxx:28
 TLeafI.cxx:29
 TLeafI.cxx:30
 TLeafI.cxx:31
 TLeafI.cxx:32
 TLeafI.cxx:33
 TLeafI.cxx:34
 TLeafI.cxx:35
 TLeafI.cxx:36
 TLeafI.cxx:37
 TLeafI.cxx:38
 TLeafI.cxx:39
 TLeafI.cxx:40
 TLeafI.cxx:41
 TLeafI.cxx:42
 TLeafI.cxx:43
 TLeafI.cxx:44
 TLeafI.cxx:45
 TLeafI.cxx:46
 TLeafI.cxx:47
 TLeafI.cxx:48
 TLeafI.cxx:49
 TLeafI.cxx:50
 TLeafI.cxx:51
 TLeafI.cxx:52
 TLeafI.cxx:53
 TLeafI.cxx:54
 TLeafI.cxx:55
 TLeafI.cxx:56
 TLeafI.cxx:57
 TLeafI.cxx:58
 TLeafI.cxx:59
 TLeafI.cxx:60
 TLeafI.cxx:61
 TLeafI.cxx:62
 TLeafI.cxx:63
 TLeafI.cxx:64
 TLeafI.cxx:65
 TLeafI.cxx:66
 TLeafI.cxx:67
 TLeafI.cxx:68
 TLeafI.cxx:69
 TLeafI.cxx:70
 TLeafI.cxx:71
 TLeafI.cxx:72
 TLeafI.cxx:73
 TLeafI.cxx:74
 TLeafI.cxx:75
 TLeafI.cxx:76
 TLeafI.cxx:77
 TLeafI.cxx:78
 TLeafI.cxx:79
 TLeafI.cxx:80
 TLeafI.cxx:81
 TLeafI.cxx:82
 TLeafI.cxx:83
 TLeafI.cxx:84
 TLeafI.cxx:85
 TLeafI.cxx:86
 TLeafI.cxx:87
 TLeafI.cxx:88
 TLeafI.cxx:89
 TLeafI.cxx:90
 TLeafI.cxx:91
 TLeafI.cxx:92
 TLeafI.cxx:93
 TLeafI.cxx:94
 TLeafI.cxx:95
 TLeafI.cxx:96
 TLeafI.cxx:97
 TLeafI.cxx:98
 TLeafI.cxx:99
 TLeafI.cxx:100
 TLeafI.cxx:101
 TLeafI.cxx:102
 TLeafI.cxx:103
 TLeafI.cxx:104
 TLeafI.cxx:105
 TLeafI.cxx:106
 TLeafI.cxx:107
 TLeafI.cxx:108
 TLeafI.cxx:109
 TLeafI.cxx:110
 TLeafI.cxx:111
 TLeafI.cxx:112
 TLeafI.cxx:113
 TLeafI.cxx:114
 TLeafI.cxx:115
 TLeafI.cxx:116
 TLeafI.cxx:117
 TLeafI.cxx:118
 TLeafI.cxx:119
 TLeafI.cxx:120
 TLeafI.cxx:121
 TLeafI.cxx:122
 TLeafI.cxx:123
 TLeafI.cxx:124
 TLeafI.cxx:125
 TLeafI.cxx:126
 TLeafI.cxx:127
 TLeafI.cxx:128
 TLeafI.cxx:129
 TLeafI.cxx:130
 TLeafI.cxx:131
 TLeafI.cxx:132
 TLeafI.cxx:133
 TLeafI.cxx:134
 TLeafI.cxx:135
 TLeafI.cxx:136
 TLeafI.cxx:137
 TLeafI.cxx:138
 TLeafI.cxx:139
 TLeafI.cxx:140
 TLeafI.cxx:141
 TLeafI.cxx:142
 TLeafI.cxx:143
 TLeafI.cxx:144
 TLeafI.cxx:145
 TLeafI.cxx:146
 TLeafI.cxx:147
 TLeafI.cxx:148
 TLeafI.cxx:149
 TLeafI.cxx:150
 TLeafI.cxx:151
 TLeafI.cxx:152
 TLeafI.cxx:153
 TLeafI.cxx:154
 TLeafI.cxx:155
 TLeafI.cxx:156
 TLeafI.cxx:157
 TLeafI.cxx:158
 TLeafI.cxx:159
 TLeafI.cxx:160
 TLeafI.cxx:161
 TLeafI.cxx:162
 TLeafI.cxx:163
 TLeafI.cxx:164
 TLeafI.cxx:165
 TLeafI.cxx:166
 TLeafI.cxx:167
 TLeafI.cxx:168
 TLeafI.cxx:169
 TLeafI.cxx:170
 TLeafI.cxx:171
 TLeafI.cxx:172
 TLeafI.cxx:173
 TLeafI.cxx:174
 TLeafI.cxx:175
 TLeafI.cxx:176
 TLeafI.cxx:177
 TLeafI.cxx:178
 TLeafI.cxx:179
 TLeafI.cxx:180
 TLeafI.cxx:181
 TLeafI.cxx:182
 TLeafI.cxx:183
 TLeafI.cxx:184
 TLeafI.cxx:185
 TLeafI.cxx:186
 TLeafI.cxx:187
 TLeafI.cxx:188
 TLeafI.cxx:189
 TLeafI.cxx:190
 TLeafI.cxx:191
 TLeafI.cxx:192
 TLeafI.cxx:193
 TLeafI.cxx:194
 TLeafI.cxx:195
 TLeafI.cxx:196
 TLeafI.cxx:197
 TLeafI.cxx:198
 TLeafI.cxx:199
 TLeafI.cxx:200
 TLeafI.cxx:201
 TLeafI.cxx:202
 TLeafI.cxx:203
 TLeafI.cxx:204
 TLeafI.cxx:205
 TLeafI.cxx:206
 TLeafI.cxx:207
 TLeafI.cxx:208
 TLeafI.cxx:209
 TLeafI.cxx:210
 TLeafI.cxx:211
 TLeafI.cxx:212
 TLeafI.cxx:213
 TLeafI.cxx:214
 TLeafI.cxx:215
 TLeafI.cxx:216
 TLeafI.cxx:217
 TLeafI.cxx:218
 TLeafI.cxx:219
 TLeafI.cxx:220
 TLeafI.cxx:221
 TLeafI.cxx:222
 TLeafI.cxx:223
 TLeafI.cxx:224
 TLeafI.cxx:225
 TLeafI.cxx:226
 TLeafI.cxx:227
 TLeafI.cxx:228
 TLeafI.cxx:229
 TLeafI.cxx:230
 TLeafI.cxx:231
 TLeafI.cxx:232
 TLeafI.cxx:233
 TLeafI.cxx:234
 TLeafI.cxx:235
 TLeafI.cxx:236
 TLeafI.cxx:237
 TLeafI.cxx:238
 TLeafI.cxx:239
 TLeafI.cxx:240
 TLeafI.cxx:241
 TLeafI.cxx:242
 TLeafI.cxx:243
 TLeafI.cxx:244