MFEM  v3.4
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
array.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 
13 // Abstract array data type
14 
15 #include "array.hpp"
16 #include <fstream>
17 
18 namespace mfem
19 {
20 
21 BaseArray::BaseArray(int asize, int ainc, int elementsize)
22 {
23  if (asize > 0)
24  {
25  data = new char[asize * elementsize];
26  size = allocsize = asize;
27  }
28  else
29  {
30  data = 0;
31  size = allocsize = 0;
32  }
33  inc = ainc;
34 }
35 
37 {
38  if (allocsize > 0)
39  {
40  delete [] (char*)data;
41  }
42 }
43 
44 void BaseArray::GrowSize(int minsize, int elementsize)
45 {
46  void *p;
47  int nsize = (inc > 0) ? abs(allocsize) + inc : 2 * abs(allocsize);
48  if (nsize < minsize) { nsize = minsize; }
49 
50  p = new char[nsize * elementsize];
51  if (size > 0)
52  {
53  memcpy(p, data, size * elementsize);
54  }
55  if (allocsize > 0)
56  {
57  delete [] (char*)data;
58  }
59  data = p;
60  allocsize = nsize;
61 }
62 
63 template <class T>
64 void Array<T>::Print(std::ostream &out, int width) const
65 {
66  for (int i = 0; i < size; i++)
67  {
68  out << ((T*)data)[i];
69  if ( !((i+1) % width) || i+1 == size )
70  {
71  out << '\n';
72  }
73  else
74  {
75  out << " ";
76  }
77  }
78 }
79 
80 template <class T>
81 void Array<T>::Save(std::ostream &out, int fmt) const
82 {
83  if (fmt == 0)
84  {
85  out << size << '\n';
86  }
87  for (int i = 0; i < size; i++)
88  {
89  out << operator[](i) << '\n';
90  }
91 }
92 
93 template <class T>
94 void Array<T>::Load(std::istream &in, int fmt)
95 {
96  if (fmt == 0)
97  {
98  int new_size;
99  in >> new_size;
100  SetSize(new_size);
101  }
102  for (int i = 0; i < size; i++)
103  {
104  in >> operator[](i);
105  }
106 }
107 
108 template <class T>
109 T Array<T>::Max() const
110 {
111  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
112 
113  T max = operator[](0);
114  for (int i = 1; i < size; i++)
115  if (max < operator[](i))
116  {
117  max = operator[](i);
118  }
119 
120  return max;
121 }
122 
123 template <class T>
124 T Array<T>::Min() const
125 {
126  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
127 
128  T min = operator[](0);
129  for (int i = 1; i < size; i++)
130  if (operator[](i) < min)
131  {
132  min = operator[](i);
133  }
134 
135  return min;
136 }
137 
138 // Partial Sum
139 template <class T>
141 {
142  T sum = static_cast<T>(0);
143  for (int i = 0; i < size; i++)
144  {
145  sum+=operator[](i);
146  operator[](i) = sum;
147  }
148 }
149 
150 // Sum
151 template <class T>
153 {
154  T sum = static_cast<T>(0);
155  for (int i = 0; i < size; i++)
156  {
157  sum+=operator[](i);
158  }
159 
160  return sum;
161 }
162 
163 template <class T>
165 {
166  T val_prev = operator[](0), val;
167  for (int i = 1; i < size; i++)
168  {
169  val=operator[](i);
170  if (val < val_prev)
171  {
172  return 0;
173  }
174  val_prev = val;
175  }
176 
177  return 1;
178 }
179 
180 
181 template <class T>
182 void Array2D<T>::Load(const char *filename, int fmt)
183 {
184  std::ifstream in;
185  in.open(filename, std::ifstream::in);
186  MFEM_VERIFY(in.is_open(), "File " << filename << " does not exist.");
187  Load(in, fmt);
188  in.close();
189 }
190 
191 template <class T>
192 void Array2D<T>::Print(std::ostream &out, int width_)
193 {
194  int height = this->NumRows();
195  int width = this->NumCols();
196 
197  for (int i = 0; i < height; i++)
198  {
199  out << "[row " << i << "]\n";
200  for (int j = 0; j < width; j++)
201  {
202  out << (*this)(i,j);
203  if ( (j+1) == width_ || (j+1) % width_ == 0 )
204  {
205  out << '\n';
206  }
207  else
208  {
209  out << ' ';
210  }
211  }
212  }
213 }
214 
215 template class Array<int>;
216 template class Array<double>;
217 template class Array2D<int>;
218 template class Array2D<double>;
219 }
void Load(std::istream &in, int fmt=0)
Read an Array from the stream in using format fmt. The format fmt can be:
Definition: array.cpp:94
~BaseArray()
Free the allocated memory.
Definition: array.cpp:36
void * data
Pointer to data.
Definition: array.hpp:32
void Load(std::istream &in, int fmt=0)
Read an Array2D from the stream in using format fmt. The format fmt can be:
Definition: array.hpp:350
void Save(std::ostream &out, int fmt=0) const
Save the Array to the stream out using the format fmt. The format fmt can be:
Definition: array.cpp:81
T Sum()
Sum all entries.
Definition: array.cpp:152
T Min() const
Find the minimal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:124
void GrowSize(int minsize, int elementsize)
Definition: array.cpp:44
int size
Size of the array.
Definition: array.hpp:34
T Max() const
Find the maximal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:109
int allocsize
Size of the allocated memory.
Definition: array.hpp:36
void Print(std::ostream &out=mfem::out, int width=4)
Prints array to stream with width elements per row.
Definition: array.cpp:192
int IsSorted()
return true if the array is sorted.
Definition: array.cpp:164
void PartialSum()
Partial Sum.
Definition: array.cpp:140
void Print(std::ostream &out=mfem::out, int width=4) const
Prints array to stream with width elements per row.
Definition: array.cpp:64
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:64