MFEM  v3.1
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)
81 {
82  out << size << '\n';
83  for (int i = 0; i < size; i++)
84  {
85  out << operator[](i) << '\n';
86  }
87 }
88 
89 template <class T>
90 T Array<T>::Max() const
91 {
92  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
93 
94  T max = operator[](0);
95  for (int i = 1; i < size; i++)
96  if (max < operator[](i))
97  {
98  max = operator[](i);
99  }
100 
101  return max;
102 }
103 
104 template <class T>
105 T Array<T>::Min() const
106 {
107  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
108 
109  T min = operator[](0);
110  for (int i = 1; i < size; i++)
111  if (operator[](i) < min)
112  {
113  min = operator[](i);
114  }
115 
116  return min;
117 }
118 
119 // Partial Sum
120 template <class T>
122 {
123  T sum = static_cast<T>(0);
124  for (int i = 0; i < size; i++)
125  {
126  sum+=operator[](i);
127  operator[](i) = sum;
128  }
129 }
130 
131 // Sum
132 template <class T>
134 {
135  T sum = static_cast<T>(0);
136  for (int i = 0; i < size; i++)
137  {
138  sum+=operator[](i);
139  }
140 
141  return sum;
142 }
143 
144 template <class T>
146 {
147  T val_prev = operator[](0), val;
148  for (int i = 1; i < size; i++)
149  {
150  val=operator[](i);
151  if (val < val_prev)
152  {
153  return 0;
154  }
155  val_prev = val;
156  }
157 
158  return 1;
159 }
160 
161 template class Array<int>;
162 template class Array<double>;
163 
164 }
~BaseArray()
Free the allocated memory.
Definition: array.cpp:35
void * data
Pointer to data.
Definition: array.hpp:31
T Sum()
Sum all entries.
Definition: array.cpp:133
T Min() const
Definition: array.cpp:105
void GrowSize(int minsize, int elementsize)
Definition: array.cpp:43
int size
Size of the array.
Definition: array.hpp:33
T Max() const
Definition: array.cpp:90
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:145
void PartialSum()
Partial Sum.
Definition: array.cpp:121
void Save(std::ostream &out)
Prints array to stream out.
Definition: array.cpp:80