/[escript]/trunk/escript/src/DataVector.cpp
ViewVC logotype

Contents of /trunk/escript/src/DataVector.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 123 - (show annotations)
Fri Jul 8 04:08:13 2005 UTC (14 years, 2 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataVector.cpp
File size: 4614 byte(s)
Merge of development branch back to main trunk on 2005-07-08

1 /*
2 ******************************************************************************
3 * *
4 * COPYRIGHT ACcESS 2004 - All Rights Reserved *
5 * *
6 * This software is the property of ACcESS. No part of this code *
7 * may be copied in any form or by any means without the expressed written *
8 * consent of ACcESS. Copying, use or modification of this software *
9 * by any unauthorised person is illegal unless that person has a software *
10 * license agreement with ACcESS. *
11 * *
12 ******************************************************************************
13 */
14
15 #include <iostream>
16 #include <fstream>
17 #include <cassert>
18
19 #include "escript/Data/DataVector.h"
20 #include "escript/Data/Taipan.h"
21 #include "escript/Data/DataException.h"
22
23 using namespace std;
24 using namespace escript;
25
26 namespace escript {
27
28 Taipan arrayManager;
29
30 DataVector::DataVector() :
31 m_array_data(0),
32 m_size(0),
33 m_dim(0),
34 m_N(0)
35 {
36 }
37
38 DataVector::DataVector(const DataVector& other) :
39 m_array_data(0),
40 m_size(other.m_size),
41 m_dim(other.m_dim),
42 m_N(other.m_N)
43 {
44 m_array_data = arrayManager.new_array(m_dim,m_N);
45 int i;
46 #pragma omp parallel for private(i) schedule(static)
47 for (i=0; i<m_size; i++) {
48 m_array_data[i] = other.m_array_data[i];
49 }
50 }
51
52 DataVector::DataVector(const DataVector::size_type size,
53 const DataVector::value_type val,
54 const DataVector::size_type blockSize) :
55 m_array_data(0),
56 m_size(size),
57 m_dim(blockSize)
58 {
59 resize(size, val, blockSize);
60 }
61
62 DataVector::~DataVector()
63 {
64 // dispose of data array
65 arrayManager.delete_array(m_array_data);
66
67 // clear data members
68 m_size = -1;
69 m_dim = -1;
70 m_N = -1;
71 m_array_data = 0;
72 }
73
74 void
75 DataVector::resize(const DataVector::size_type newSize,
76 const DataVector::value_type newValue,
77 const DataVector::size_type newBlockSize)
78 {
79 assert(m_size >= 0);
80
81 if ( (newSize % newBlockSize) != 0) {
82 throw DataException("DataVector: invalid blockSize specified");
83 }
84
85 arrayManager.delete_array(m_array_data);
86
87 m_size = newSize;
88 m_dim = newBlockSize;
89 m_N = newSize / newBlockSize;
90 m_array_data = arrayManager.new_array(m_dim,m_N);
91
92 int i;
93 #pragma omp parallel for private(i) schedule(static)
94 for (i=0; i<m_size; i++) {
95 m_array_data[i] = newValue;
96 }
97 }
98
99 DataVector&
100 DataVector::operator=(const DataVector& other)
101 {
102 assert(m_size >= 0);
103
104 arrayManager.delete_array(m_array_data);
105
106 m_size = other.m_size;
107 m_dim = other.m_dim;
108 m_N = other.m_N;
109
110 m_array_data = arrayManager.new_array(m_dim,m_N);
111 int i;
112 #pragma omp parallel for private(i) schedule(static)
113 for (i=0; i<m_size; i++) {
114 m_array_data[i] = other.m_array_data[i];
115 }
116
117 return *this;
118 }
119
120 bool
121 DataVector::operator==(const DataVector& other) const
122 {
123 assert(m_size >= 0);
124
125 if (m_size!=other.m_size) {
126 return false;
127 }
128 if (m_dim!=other.m_dim) {
129 return false;
130 }
131 if (m_N!=other.m_N) {
132 return false;
133 }
134 for (int i=0; i<m_size; i++) {
135 if (m_array_data[i] != other.m_array_data[i]) {
136 return false;
137 }
138 }
139 return true;
140 }
141
142 bool
143 DataVector::operator!=(const DataVector& other) const
144 {
145 return !(*this==other);
146 }
147
148 int
149 DataVector::archiveData(ofstream& archiveFile,
150 const size_type noValues) const
151 {
152 //
153 // Check number of values expected to be written matches number in this object
154 if (noValues != size()) {
155 return 2;
156 }
157
158 //
159 // Write all values in this object out to archiveFile
160 for (int i=0; i<size(); i++) {
161 archiveFile.write(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double));
162 }
163
164 //
165 // Check no errors were encountered before returning
166 if (!archiveFile.good()) {
167 return 1;
168 }
169
170 return 0;
171 }
172
173 int
174 DataVector::extractData(ifstream& archiveFile,
175 const size_type noValues)
176 {
177 //
178 // Check number of values expected to be read matches number in this object
179 if (noValues != size()) {
180 return 2;
181 }
182
183 //
184 // Read all values in archiveFile back to this object
185 for (int i=0; i<size(); i++) {
186 archiveFile.read(reinterpret_cast<char *>(&m_array_data[i]),sizeof(double));
187 }
188
189 //
190 // Check no errors were encountered before returning
191 if (!archiveFile.good()) {
192 return 1;
193 }
194
195 return 0;
196 }
197
198 } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26