MFEM v2.0
|
00001 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at 00002 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights 00003 // reserved. See file COPYRIGHT for details. 00004 // 00005 // This file is part of the MFEM library. For more information and source code 00006 // availability see http://mfem.googlecode.com. 00007 // 00008 // MFEM is free software; you can redistribute it and/or modify it under the 00009 // terms of the GNU Lesser General Public License (as published by the Free 00010 // Software Foundation) version 2.1 dated February 1999. 00011 00012 00013 // Abstract array data type 00014 00015 #include "array.hpp" 00016 #include <string.h> 00017 #include <stdlib.h> 00018 00019 BaseArray::BaseArray(int asize, int ainc, int elementsize) 00020 { 00021 if (asize > 0) 00022 { 00023 data = new char[asize * elementsize]; 00024 size = allocsize = asize; 00025 } 00026 else 00027 { 00028 data = 0; 00029 size = allocsize = 0; 00030 } 00031 inc = ainc; 00032 } 00033 00034 BaseArray::~BaseArray() 00035 { 00036 if (allocsize > 0) 00037 delete [] (char*)data; 00038 } 00039 00040 void BaseArray::GrowSize(int minsize, int elementsize) 00041 { 00042 void *p; 00043 int nsize = (inc > 0) ? abs(allocsize) + inc : 2 * abs(allocsize); 00044 if (nsize < minsize) nsize = minsize; 00045 00046 p = new char[nsize * elementsize]; 00047 if (size > 0) 00048 memcpy(p, data, size * elementsize); 00049 if (allocsize > 0) 00050 delete [] (char*)data; 00051 data = p; 00052 allocsize = nsize; 00053 } 00054 00055 template <class T> 00056 void Array<T>::Print(ostream &out, int width) 00057 { 00058 for (int i = 0; i < size; i++) 00059 { 00060 out << ((T*)data)[i]; 00061 if ( !((i+1) % width) || i+1 == size ) 00062 out << '\n'; 00063 else 00064 out << " "; 00065 } 00066 } 00067 00068 template <class T> 00069 void Array<T>::Save(ostream &out) 00070 { 00071 out << size << '\n'; 00072 for (int i = 0; i < size; i++) 00073 out << operator[](i) << '\n'; 00074 } 00075 00076 template <class T> 00077 T Array<T>::Max() const 00078 { 00079 #ifdef MFEM_DEBUG 00080 if (size <= 0) 00081 mfem_error("Array::Max : empty array!"); 00082 #endif 00083 00084 T max = operator[](0); 00085 for (int i = 1; i < size; i++) 00086 if (max < operator[](i)) 00087 max = operator[](i); 00088 00089 return max; 00090 } 00091 00092 template <class T> 00093 int Compare(const void *p, const void *q) 00094 { 00095 if (*((T*)p) < *((T*)q)) return -1; 00096 if (*((T*)q) < *((T*)p)) return +1; 00097 return 0; 00098 } 00099 00100 template <class T> 00101 void Array<T>::Sort() 00102 { 00103 // qsort((T*)data,0,size-1); 00104 qsort(data, size, sizeof(T), Compare<T>); // use qsort from stdlib.h 00105 } 00106 00107 template class Array<int>; 00108 template class Array<double>;