MFEM  v3.0
 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.googlecode.com.
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  delete [] (char*)data;
39 }
40 
41 void BaseArray::GrowSize(int minsize, int elementsize)
42 {
43  void *p;
44  int nsize = (inc > 0) ? abs(allocsize) + inc : 2 * abs(allocsize);
45  if (nsize < minsize) nsize = minsize;
46 
47  p = new char[nsize * elementsize];
48  if (size > 0)
49  memcpy(p, data, size * elementsize);
50  if (allocsize > 0)
51  delete [] (char*)data;
52  data = p;
53  allocsize = nsize;
54 }
55 
56 template <class T>
57 void Array<T>::Print(std::ostream &out, int width)
58 {
59  for (int i = 0; i < size; i++)
60  {
61  out << ((T*)data)[i];
62  if ( !((i+1) % width) || i+1 == size )
63  out << '\n';
64  else
65  out << " ";
66  }
67 }
68 
69 template <class T>
70 void Array<T>::Save(std::ostream &out)
71 {
72  out << size << '\n';
73  for (int i = 0; i < size; i++)
74  out << operator[](i) << '\n';
75 }
76 
77 template <class T>
78 T Array<T>::Max() const
79 {
80  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
81 
82  T max = operator[](0);
83  for (int i = 1; i < size; i++)
84  if (max < operator[](i))
85  max = operator[](i);
86 
87  return max;
88 }
89 
90 template <class T>
91 T Array<T>::Min() const
92 {
93  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
94 
95  T min = operator[](0);
96  for (int i = 1; i < size; i++)
97  if (operator[](i) < min)
98  min = operator[](i);
99 
100  return min;
101 }
102 
103 template <class T>
104 int Compare(const void *p, const void *q)
105 {
106  if (*((T*)p) < *((T*)q)) return -1;
107  if (*((T*)q) < *((T*)p)) return +1;
108  return 0;
109 }
110 
111 template <class T>
113 {
114  // qsort((T*)data,0,size-1);
115  qsort(data, size, sizeof(T), Compare<T>); // use qsort from stdlib.h
116 }
117 
118 // Partial Sum
119 template <class T>
121 {
122  T sum = static_cast<T>(0);
123  for (int i = 0; i < size; i++)
124  {
125  sum+=operator[](i);
126  operator[](i) = sum;
127  }
128 }
129 
130 // Sum
131 template <class T>
133 {
134  T sum = static_cast<T>(0);
135  for (int i = 0; i < size; i++)
136  sum+=operator[](i);
137 
138  return sum;
139 }
140 
141 template <class T>
143 {
144  T val_prev = operator[](0), val;
145  for (int i = 1; i < size; i++)
146  {
147  val=operator[](i);
148  if (val < val_prev)
149  return 0;
150  val_prev = val;
151  }
152 
153  return 1;
154 }
155 
156 template class Array<int>;
157 template class Array<double>;
158 
159 }
~BaseArray()
Free the allocated memory.
Definition: array.cpp:35
void * data
Pointer to data.
Definition: array.hpp:30
T Sum()
Sum all entries.
Definition: array.cpp:132
T Min() const
Definition: array.cpp:91
void GrowSize(int minsize, int elementsize)
Definition: array.cpp:41
int size
Size of the array.
Definition: array.hpp:32
T Max() const
Definition: array.cpp:78
void Print(std::ostream &out, int width)
Prints array to stream with width elements per row.
Definition: array.cpp:57
void Sort()
Sorts the array.
Definition: array.cpp:112
int allocsize
Size of the allocated memory.
Definition: array.hpp:34
int IsSorted()
return true if the array is sorted.
Definition: array.cpp:142
void PartialSum()
Partial Sum.
Definition: array.cpp:120
void Save(std::ostream &out)
Prints array to stream out.
Definition: array.cpp:70
int Compare(const void *p, const void *q)
Definition: array.cpp:104