MFEM  v3.3
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 
17 namespace mfem
18 {
19 
20 BaseArray::BaseArray(int asize, int ainc, int elementsize)
21 {
22  if (asize > 0)
23  {
24  data = new char[asize * elementsize];
25  size = allocsize = asize;
26  }
27  else
28  {
29  data = 0;
30  size = allocsize = 0;
31  }
32  inc = ainc;
33 }
34 
36 {
37  if (allocsize > 0)
38  {
39  delete [] (char*)data;
40  }
41 }
42 
43 void BaseArray::GrowSize(int minsize, int elementsize)
44 {
45  void *p;
46  int nsize = (inc > 0) ? abs(allocsize) + inc : 2 * abs(allocsize);
47  if (nsize < minsize) { nsize = minsize; }
48 
49  p = new char[nsize * elementsize];
50  if (size > 0)
51  {
52  memcpy(p, data, size * elementsize);
53  }
54  if (allocsize > 0)
55  {
56  delete [] (char*)data;
57  }
58  data = p;
59  allocsize = nsize;
60 }
61 
62 template <class T>
63 void Array<T>::Print(std::ostream &out, int width)
64 {
65  for (int i = 0; i < size; i++)
66  {
67  out << ((T*)data)[i];
68  if ( !((i+1) % width) || i+1 == size )
69  {
70  out << '\n';
71  }
72  else
73  {
74  out << " ";
75  }
76  }
77 }
78 
79 template <class T>
80 void Array<T>::Save(std::ostream &out, int fmt) const
81 {
82  if (fmt == 0)
83  {
84  out << size << '\n';
85  }
86  for (int i = 0; i < size; i++)
87  {
88  out << operator[](i) << '\n';
89  }
90 }
91 
92 template <class T>
93 void Array<T>::Load(std::istream &in, int fmt)
94 {
95  if (fmt == 0)
96  {
97  int new_size;
98  in >> new_size;
99  SetSize(new_size);
100  }
101  for (int i = 0; i < size; i++)
102  {
103  in >> operator[](i);
104  }
105 }
106 
107 template <class T>
108 T Array<T>::Max() const
109 {
110  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
111 
112  T max = operator[](0);
113  for (int i = 1; i < size; i++)
114  if (max < operator[](i))
115  {
116  max = operator[](i);
117  }
118 
119  return max;
120 }
121 
122 template <class T>
123 T Array<T>::Min() const
124 {
125  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
126 
127  T min = operator[](0);
128  for (int i = 1; i < size; i++)
129  if (operator[](i) < min)
130  {
131  min = operator[](i);
132  }
133 
134  return min;
135 }
136 
137 // Partial Sum
138 template <class T>
140 {
141  T sum = static_cast<T>(0);
142  for (int i = 0; i < size; i++)
143  {
144  sum+=operator[](i);
145  operator[](i) = sum;
146  }
147 }
148 
149 // Sum
150 template <class T>
152 {
153  T sum = static_cast<T>(0);
154  for (int i = 0; i < size; i++)
155  {
156  sum+=operator[](i);
157  }
158 
159  return sum;
160 }
161 
162 template <class T>
164 {
165  T val_prev = operator[](0), val;
166  for (int i = 1; i < size; i++)
167  {
168  val=operator[](i);
169  if (val < val_prev)
170  {
171  return 0;
172  }
173  val_prev = val;
174  }
175 
176  return 1;
177 }
178 
179 template class Array<int>;
180 template class Array<double>;
181 
182 }
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:93
~BaseArray()
Free the allocated memory.
Definition: array.cpp:35
void * data
Pointer to data.
Definition: array.hpp:31
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:80
T Sum()
Sum all entries.
Definition: array.cpp:151
T Min() const
Find the minimal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:123
void GrowSize(int minsize, int elementsize)
Definition: array.cpp:43
int size
Size of the array.
Definition: array.hpp:33
T Max() const
Find the maximal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:108
int allocsize
Size of the allocated memory.
Definition: array.hpp:35
void Print(std::ostream &out=std::cout, int width=4)
Prints array to stream with width elements per row.
Definition: array.cpp:63
int IsSorted()
return true if the array is sorted.
Definition: array.cpp:163
void PartialSum()
Partial Sum.
Definition: array.cpp:139