Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TArrayS.cxx
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Rene Brun 06/03/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class TArrayS
13\ingroup Containers
14Array of shorts (16 bits per element).
15*/
16
17#include "TArrayS.h"
18#include "TBuffer.h"
19
20
21
22////////////////////////////////////////////////////////////////////////////////
23/// Default TArrayS ctor.
24
26{
27 fArray = nullptr;
28}
29
30////////////////////////////////////////////////////////////////////////////////
31/// Create TArrayS object and set array size to n shorts.
32
34{
35 fArray = nullptr;
36 if (n > 0) Set(n);
37}
38
39////////////////////////////////////////////////////////////////////////////////
40/// Create TArrayS object and initialize it with values of array.
41
43{
44 fArray = nullptr;
45 Set(n, array);
46}
47
48////////////////////////////////////////////////////////////////////////////////
49/// Copy constructor.
50
51TArrayS::TArrayS(const TArrayS &array) : TArray(array)
52{
53 fArray = nullptr;
54 Set(array.fN, array.fArray);
55}
56
57////////////////////////////////////////////////////////////////////////////////
58/// TArrayS assignment operator.
59
61{
62 if (this != &rhs)
63 Set(rhs.fN, rhs.fArray);
64 return *this;
65}
66
67////////////////////////////////////////////////////////////////////////////////
68/// Delete TArrayS object.
69
71{
72 delete [] fArray;
73 fArray = nullptr;
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// Adopt array arr into TArrayS, i.e. don't copy arr but use it directly
78/// in TArrayS. User may not delete arr, TArrayS dtor will do it.
79
81{
82 if (fArray)
83 delete [] fArray;
84
85 fN = n;
86 fArray = arr;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Add short c at position i. Check for out of bounds.
91
93{
94 if (!BoundsOk("TArrayS::AddAt", i)) return;
95 fArray[i] = c;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Set size of this array to n shorts.
100/// A new array is created, the old contents copied to the new array,
101/// then the old array is deleted.
102/// This function should not be called if the array was declared via Adopt.
103
105{
106 if (n < 0) return;
107 if (n != fN) {
108 Short_t *temp = fArray;
109 if (n != 0) {
110 fArray = new Short_t[n];
111 if (n < fN) {
112 memcpy(fArray, temp, n*sizeof(Short_t));
113 } else if (temp) {
114 memcpy(fArray, temp, fN*sizeof(Short_t));
115 memset(&fArray[fN], 0, (n-fN)*sizeof(Short_t));
116 } else {
117 memset(fArray, 0, n*sizeof(Short_t));
118 }
119 } else {
120 fArray = nullptr;
121 }
122 if (fN) delete [] temp;
123 fN = n;
124 }
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Set size of this array to n shorts and set the contents.
129/// This function should not be called if the array was declared via Adopt.
130
131void TArrayS::Set(Int_t n, const Short_t *array)
132{
133 if (fArray && fN != n) {
134 delete [] fArray;
135 fArray = nullptr;
136 }
137 fN = n;
138 if ((fN == 0) || !array)
139 return;
140 if (!fArray) fArray = new Short_t[fN];
141 memmove(fArray, array, n*sizeof(Short_t));
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Stream a TArrayS object.
146
148{
149 if (b.IsReading()) {
150 Int_t n;
151 b >> n;
152 Set(n);
153 b.ReadFastArray(fArray,n);
154 } else {
155 b << fN;
156 b.WriteFastArray(fArray, fN);
157 }
158}
159
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Array of shorts (16 bits per element).
Definition TArrayS.h:27
void Adopt(Int_t n, Short_t *array)
Adopt array arr into TArrayS, i.e.
Definition TArrayS.cxx:80
TArrayS & operator=(const TArrayS &rhs)
TArrayS assignment operator.
Definition TArrayS.cxx:60
void AddAt(Short_t c, Int_t i)
Add short c at position i. Check for out of bounds.
Definition TArrayS.cxx:92
void Set(Int_t n) override
Set size of this array to n shorts.
Definition TArrayS.cxx:104
TArrayS()
Default TArrayS ctor.
Definition TArrayS.cxx:25
void Streamer(TBuffer &) override
Stream a TArrayS object.
Definition TArrayS.cxx:147
virtual ~TArrayS()
Delete TArrayS object.
Definition TArrayS.cxx:70
Short_t * fArray
Definition TArrayS.h:30
Abstract array base class.
Definition TArray.h:31
Int_t fN
Definition TArray.h:38
Bool_t BoundsOk(const char *where, Int_t at) const
Definition TArray.h:77
Buffer base class used for serializing objects.
Definition TBuffer.h:43
const Int_t n
Definition legend1.C:16