Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TArrayD.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 TArrayD
13\ingroup Containers
14Array of doubles (64 bits per element).
15*/
16
17#include "TArrayD.h"
18#include "TBuffer.h"
19
20
22
23////////////////////////////////////////////////////////////////////////////////
24/// Default TArrayD ctor.
25
27{
28 fArray = nullptr;
29}
30
31////////////////////////////////////////////////////////////////////////////////
32/// Create TArrayD object and set array size to n doubles.
33
35{
36 fArray = nullptr;
37 if (n > 0) Set(n);
38}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Create TArrayD object and initialize it with values of array.
42
44{
45 fArray = nullptr;
46 Set(n, array);
47}
48
49////////////////////////////////////////////////////////////////////////////////
50/// Copy constructor.
51
52TArrayD::TArrayD(const TArrayD &array) : TArray(array)
53{
54 fArray = nullptr;
55 Set(array.fN, array.fArray);
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// TArrayD assignment operator.
60
62{
63 if (this != &rhs)
64 Set(rhs.fN, rhs.fArray);
65 return *this;
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Delete TArrayD object.
70
72{
73 delete [] fArray;
74 fArray = nullptr;
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Adopt array arr into TArrayD, i.e. don't copy arr but use it directly
79/// in TArrayD. User may not delete arr, TArrayD dtor will do it.
80
82{
83 if (fArray)
84 delete [] fArray;
85
86 fN = n;
87 fArray = arr;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Set the double c value at position i in the array. Check for out of bounds.
92/// Warning: the name of the method is misleading, it actually overwrites the position i.
93
95{
96 if (!BoundsOk("TArrayD::AddAt", i)) return;
97 fArray[i] = c;
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Set size of this array to n doubles.
102/// A new array is created, the old contents copied to the new array,
103/// then the old array is deleted.
104/// This function should not be called if the array was declared via Adopt.
105
107{
108 if (n < 0) return;
109 if (n != fN) {
110 Double_t *temp = fArray;
111 if (n != 0) {
112 fArray = new Double_t[n];
113 if (n < fN) {
114 memcpy(fArray, temp, n*sizeof(Double_t));
115 } else if (temp) {
116 memcpy(fArray, temp, fN*sizeof(Double_t));
117 memset(&fArray[fN], 0, (n-fN)*sizeof(Double_t));
118 } else {
119 memset(fArray, 0, n*sizeof(Double_t));
120 }
121 } else {
122 fArray = nullptr;
123 }
124 if (fN) delete [] temp;
125 fN = n;
126 }
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Set size of this array to n doubles and set the contents
131/// This function should not be called if the array was declared via Adopt.
132
133void TArrayD::Set(Int_t n, const Double_t *array)
134{
135 if (fArray && fN != n) {
136 delete [] fArray;
137 fArray = nullptr;
138 }
139 fN = n;
140 if ((fN == 0) || !array)
141 return;
142 if (!fArray) fArray = new Double_t[fN];
143 memmove(fArray, array, n*sizeof(Double_t));
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Stream a TArrayD object.
148
150{
151 if (b.IsReading()) {
152 Int_t n;
153 b >> n;
154 Set(n);
155 b.ReadFastArray(fArray,n);
156 } else {
157 b << fN;
158 b.WriteFastArray(fArray, fN);
159 }
160}
161
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define ClassImp(name)
Definition Rtypes.h:377
Array of doubles (64 bits per element).
Definition TArrayD.h:27
Double_t * fArray
Definition TArrayD.h:30
void Streamer(TBuffer &) override
Stream a TArrayD object.
Definition TArrayD.cxx:149
void Adopt(Int_t n, Double_t *array)
Adopt array arr into TArrayD, i.e.
Definition TArrayD.cxx:81
void Set(Int_t n) override
Set size of this array to n doubles.
Definition TArrayD.cxx:106
virtual ~TArrayD()
Delete TArrayD object.
Definition TArrayD.cxx:71
TArrayD & operator=(const TArrayD &rhs)
TArrayD assignment operator.
Definition TArrayD.cxx:61
void AddAt(Double_t c, Int_t i)
Set the double c value at position i in the array.
Definition TArrayD.cxx:94
TArrayD()
Default TArrayD ctor.
Definition TArrayD.cxx:26
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