1 // Copyright (c) 2010-2021, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11
12
13 // Abstract array data type
14
15 #include "array.hpp"
16 #include "../general/forall.hpp"
17 #include <fstream>
18
19 namespace mfem
20 {
21
22 template <class T>
Print(std::ostream & out,int width) const23 void Array<T>::Print(std::ostream &out, int width) const
24 {
25 for (int i = 0; i < size; i++)
26 {
27 out << data[i];
28 if ( !((i+1) % width) || i+1 == size )
29 {
30 out << '\n';
31 }
32 else
33 {
34 out << " ";
35 }
36 }
37 }
38
39 template <class T>
Save(std::ostream & out,int fmt) const40 void Array<T>::Save(std::ostream &out, int fmt) const
41 {
42 if (fmt == 0)
43 {
44 out << size << '\n';
45 }
46 for (int i = 0; i < size; i++)
47 {
48 out << operator[](i) << '\n';
49 }
50 }
51
52 template <class T>
Load(std::istream & in,int fmt)53 void Array<T>::Load(std::istream &in, int fmt)
54 {
55 if (fmt == 0)
56 {
57 int new_size;
58 in >> new_size;
59 SetSize(new_size);
60 }
61 for (int i = 0; i < size; i++)
62 {
63 in >> operator[](i);
64 }
65 }
66
67 template <class T>
Max() const68 T Array<T>::Max() const
69 {
70 MFEM_ASSERT(size > 0, "Array is empty with size " << size);
71
72 T max = operator[](0);
73 for (int i = 1; i < size; i++)
74 {
75 if (max < operator[](i))
76 {
77 max = operator[](i);
78 }
79 }
80
81 return max;
82 }
83
84 template <class T>
Min() const85 T Array<T>::Min() const
86 {
87 MFEM_ASSERT(size > 0, "Array is empty with size " << size);
88
89 T min = operator[](0);
90 for (int i = 1; i < size; i++)
91 {
92 if (operator[](i) < min)
93 {
94 min = operator[](i);
95 }
96 }
97
98 return min;
99 }
100
101 // Partial Sum
102 template <class T>
PartialSum()103 void Array<T>::PartialSum()
104 {
105 T sum = static_cast<T>(0);
106 for (int i = 0; i < size; i++)
107 {
108 sum+=operator[](i);
109 operator[](i) = sum;
110 }
111 }
112
113 // Sum
114 template <class T>
Sum()115 T Array<T>::Sum()
116 {
117 T sum = static_cast<T>(0);
118 for (int i = 0; i < size; i++)
119 {
120 sum+=operator[](i);
121 }
122
123 return sum;
124 }
125
126 template <class T>
IsSorted()127 int Array<T>::IsSorted()
128 {
129 T val_prev = operator[](0), val;
130 for (int i = 1; i < size; i++)
131 {
132 val=operator[](i);
133 if (val < val_prev)
134 {
135 return 0;
136 }
137 val_prev = val;
138 }
139
140 return 1;
141 }
142
143
144 template <class T>
Load(const char * filename,int fmt)145 void Array2D<T>::Load(const char *filename, int fmt)
146 {
147 std::ifstream in;
148 in.open(filename, std::ifstream::in);
149 MFEM_VERIFY(in.is_open(), "File " << filename << " does not exist.");
150 Load(in, fmt);
151 in.close();
152 }
153
154 template <class T>
Print(std::ostream & out,int width_)155 void Array2D<T>::Print(std::ostream &out, int width_)
156 {
157 int height = this->NumRows();
158 int width = this->NumCols();
159
160 for (int i = 0; i < height; i++)
161 {
162 out << "[row " << i << "]\n";
163 for (int j = 0; j < width; j++)
164 {
165 out << (*this)(i,j);
166 if ( (j+1) == width_ || (j+1) % width_ == 0 )
167 {
168 out << '\n';
169 }
170 else
171 {
172 out << ' ';
173 }
174 }
175 }
176 }
177
178 template class Array<char>;
179 template class Array<int>;
180 template class Array<long long>;
181 template class Array<double>;
182 template class Array2D<int>;
183 template class Array2D<double>;
184
185 } // namespace mfem
186