// @(#)root/cont:$Id$
// Author: Rene Brun   06/03/95

/*************************************************************************
 * 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.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TArrayS                                                              //
//                                                                      //
// Array of shorts (16 bits per element).                               //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TArrayS.h"
#include "TBuffer.h"


ClassImp(TArrayS)

//______________________________________________________________________________
TArrayS::TArrayS()
{
   // Default TArrayS ctor.

   fArray = 0;
}

//______________________________________________________________________________
TArrayS::TArrayS(Int_t n)
{
   // Create TArrayS object and set array size to n shorts.

   fArray = 0;
   if (n > 0) Set(n);
}

//______________________________________________________________________________
TArrayS::TArrayS(Int_t n, const Short_t *array)
{
   // Create TArrayS object and initialize it with values of array.

   fArray = 0;
   Set(n, array);
}

//______________________________________________________________________________
TArrayS::TArrayS(const TArrayS &array) : TArray(array)
{
   // Copy constructor.

   fArray = 0;
   Set(array.fN, array.fArray);
}

//______________________________________________________________________________
TArrayS &TArrayS::operator=(const TArrayS &rhs)
{
   // TArrayS assignment operator.

   if (this != &rhs)
      Set(rhs.fN, rhs.fArray);
   return *this;
}

//______________________________________________________________________________
TArrayS::~TArrayS()
{
   // Delete TArrayS object.

   delete [] fArray;
   fArray = 0;
}

//______________________________________________________________________________
void TArrayS::Adopt(Int_t n, Short_t *arr)
{
   // Adopt array arr into TArrayS, i.e. don't copy arr but use it directly
   // in TArrayS. User may not delete arr, TArrayS dtor will do it.

   if (fArray)
      delete [] fArray;

   fN     = n;
   fArray = arr;
}

//______________________________________________________________________________
void TArrayS::AddAt(Short_t c, Int_t i)
{
   // Add short c at position i. Check for out of bounds.

   if (!BoundsOk("TArrayS::AddAt", i)) return;
   fArray[i] = c;
}

//______________________________________________________________________________
void TArrayS::Set(Int_t n)
{
   // Set size of this array to n shorts.
   // A new array is created, the old contents copied to the new array,
   // then the old array is deleted.
   // This function should not be called if the array was declared via Adopt.

   if (n < 0) return;
   if (n != fN) {
      Short_t *temp = fArray;
      if (n != 0) {
         fArray = new Short_t[n];
         if (n < fN) memcpy(fArray,temp, n*sizeof(Short_t));
         else {
            memcpy(fArray,temp,fN*sizeof(Short_t));
            memset(&fArray[fN],0,(n-fN)*sizeof(Short_t));
         }
      } else {
         fArray = 0;
      }
      if (fN) delete [] temp;
      fN = n;
   }
}

//______________________________________________________________________________
void TArrayS::Set(Int_t n, const Short_t *array)
{
   // Set size of this array to n shorts and set the contents.
   // This function should not be called if the array was declared via Adopt.

   if (fArray && fN != n) {
      delete [] fArray;
      fArray = 0;
   }
   fN = n;
   if (fN == 0) return;
   if (array == 0) return;
   if (!fArray) fArray = new Short_t[fN];
   memmove(fArray, array, n*sizeof(Short_t));
}

//_______________________________________________________________________
void TArrayS::Streamer(TBuffer &b)
{
   // Stream a TArrayS object.

   if (b.IsReading()) {
      Int_t n;
      b >> n;
      Set(n);
      b.ReadFastArray(fArray,n);
   } else {
      b << fN;
      b.WriteFastArray(fArray, fN);
   }
}

 TArrayS.cxx:1
 TArrayS.cxx:2
 TArrayS.cxx:3
 TArrayS.cxx:4
 TArrayS.cxx:5
 TArrayS.cxx:6
 TArrayS.cxx:7
 TArrayS.cxx:8
 TArrayS.cxx:9
 TArrayS.cxx:10
 TArrayS.cxx:11
 TArrayS.cxx:12
 TArrayS.cxx:13
 TArrayS.cxx:14
 TArrayS.cxx:15
 TArrayS.cxx:16
 TArrayS.cxx:17
 TArrayS.cxx:18
 TArrayS.cxx:19
 TArrayS.cxx:20
 TArrayS.cxx:21
 TArrayS.cxx:22
 TArrayS.cxx:23
 TArrayS.cxx:24
 TArrayS.cxx:25
 TArrayS.cxx:26
 TArrayS.cxx:27
 TArrayS.cxx:28
 TArrayS.cxx:29
 TArrayS.cxx:30
 TArrayS.cxx:31
 TArrayS.cxx:32
 TArrayS.cxx:33
 TArrayS.cxx:34
 TArrayS.cxx:35
 TArrayS.cxx:36
 TArrayS.cxx:37
 TArrayS.cxx:38
 TArrayS.cxx:39
 TArrayS.cxx:40
 TArrayS.cxx:41
 TArrayS.cxx:42
 TArrayS.cxx:43
 TArrayS.cxx:44
 TArrayS.cxx:45
 TArrayS.cxx:46
 TArrayS.cxx:47
 TArrayS.cxx:48
 TArrayS.cxx:49
 TArrayS.cxx:50
 TArrayS.cxx:51
 TArrayS.cxx:52
 TArrayS.cxx:53
 TArrayS.cxx:54
 TArrayS.cxx:55
 TArrayS.cxx:56
 TArrayS.cxx:57
 TArrayS.cxx:58
 TArrayS.cxx:59
 TArrayS.cxx:60
 TArrayS.cxx:61
 TArrayS.cxx:62
 TArrayS.cxx:63
 TArrayS.cxx:64
 TArrayS.cxx:65
 TArrayS.cxx:66
 TArrayS.cxx:67
 TArrayS.cxx:68
 TArrayS.cxx:69
 TArrayS.cxx:70
 TArrayS.cxx:71
 TArrayS.cxx:72
 TArrayS.cxx:73
 TArrayS.cxx:74
 TArrayS.cxx:75
 TArrayS.cxx:76
 TArrayS.cxx:77
 TArrayS.cxx:78
 TArrayS.cxx:79
 TArrayS.cxx:80
 TArrayS.cxx:81
 TArrayS.cxx:82
 TArrayS.cxx:83
 TArrayS.cxx:84
 TArrayS.cxx:85
 TArrayS.cxx:86
 TArrayS.cxx:87
 TArrayS.cxx:88
 TArrayS.cxx:89
 TArrayS.cxx:90
 TArrayS.cxx:91
 TArrayS.cxx:92
 TArrayS.cxx:93
 TArrayS.cxx:94
 TArrayS.cxx:95
 TArrayS.cxx:96
 TArrayS.cxx:97
 TArrayS.cxx:98
 TArrayS.cxx:99
 TArrayS.cxx:100
 TArrayS.cxx:101
 TArrayS.cxx:102
 TArrayS.cxx:103
 TArrayS.cxx:104
 TArrayS.cxx:105
 TArrayS.cxx:106
 TArrayS.cxx:107
 TArrayS.cxx:108
 TArrayS.cxx:109
 TArrayS.cxx:110
 TArrayS.cxx:111
 TArrayS.cxx:112
 TArrayS.cxx:113
 TArrayS.cxx:114
 TArrayS.cxx:115
 TArrayS.cxx:116
 TArrayS.cxx:117
 TArrayS.cxx:118
 TArrayS.cxx:119
 TArrayS.cxx:120
 TArrayS.cxx:121
 TArrayS.cxx:122
 TArrayS.cxx:123
 TArrayS.cxx:124
 TArrayS.cxx:125
 TArrayS.cxx:126
 TArrayS.cxx:127
 TArrayS.cxx:128
 TArrayS.cxx:129
 TArrayS.cxx:130
 TArrayS.cxx:131
 TArrayS.cxx:132
 TArrayS.cxx:133
 TArrayS.cxx:134
 TArrayS.cxx:135
 TArrayS.cxx:136
 TArrayS.cxx:137
 TArrayS.cxx:138
 TArrayS.cxx:139
 TArrayS.cxx:140
 TArrayS.cxx:141
 TArrayS.cxx:142
 TArrayS.cxx:143
 TArrayS.cxx:144
 TArrayS.cxx:145
 TArrayS.cxx:146
 TArrayS.cxx:147
 TArrayS.cxx:148
 TArrayS.cxx:149
 TArrayS.cxx:150
 TArrayS.cxx:151
 TArrayS.cxx:152
 TArrayS.cxx:153
 TArrayS.cxx:154
 TArrayS.cxx:155
 TArrayS.cxx:156
 TArrayS.cxx:157
 TArrayS.cxx:158
 TArrayS.cxx:159
 TArrayS.cxx:160