1d_array.h
Go to the documentation of this file.00001 // -*-c++-*- 00002 //----------------------------< /-/ CLARAty /-/ >------------------------------ 00003 /** 00004 * @file 1d_array.h 00005 * 00006 * Defines a one-dimensional array class and its functionality. This 00007 * class inherits functionality from 2d_array and supports sub-arrays. 00008 * 1D_Array is a container class similar to the STL vector with sub 00009 * array support. It also inherits from 2D_Array to support the 00010 * matrix and vector classes that derive from 2D_Array and 1D_Array 00011 * respectively 1D_Array class allocates continguous memory for the 00012 * arrays and carries out vector indexing and some basic operations 00013 * such as == comparsions. Most functionality is inherited from its 00014 * parent. This class requires and guarantees that the 00015 * elements are contiguous (but not necessarily the memory allocated 00016 * inside each element). 00017 * 00018 * <br>@b Designer(s): Issa A.D. Nesnas 00019 * <br>@b Author(s): Issa A.D. Nesnas, Clay Kunz 00020 * <br>@b Date: March 22, 2001 00021 * 00022 * <b>Software License:</b><br> 00023 * <code> http://claraty.jpl.nasa.gov/license/open_src/ or 00024 * file: license/open_src.txt </code> 00025 * 00026 * © 2006, Jet Propulsion Laboratory, California Institute of Technology<br> 00027 * 00028 * $Revision: 1.5 $ 00029 */ 00030 //----------------------------------------------------------------------------- 00031 00032 #ifndef N_1D_ARRAY_H 00033 #define N_1D_ARRAY_H 00034 00035 #include "claraty/share.h" 00036 #include "claraty/2d_array.h" 00037 00038 namespace claraty { 00039 00040 /** 00041 * @ingroup math_data_structure 00042 */ 00043 00044 // Forward declarations 00045 template <class T> class N_1D_Array; 00046 00047 typedef N_1D_Array<double> N_1D_Array_d; 00048 00049 00050 //------------------------------------------------------------------------------ 00051 //-----------------------------< N_1D_Array Class >----------------------------- 00052 //------------------------------------------------------------------------------ 00053 00054 // All 1D arrays are made of N columns and one row (similar to Matlab) 00055 // The N_1D_Array is a row vector 00056 /** 00057 * 00058 * @ingroup arrays 00059 */ 00060 template <class T> 00061 class N_1D_Array : public N_2D_Array<T> { 00062 public: 00063 N_1D_Array() : N_2D_Array<T>() {} 00064 N_1D_Array(int size) : N_2D_Array<T>(1, size) {} 00065 N_1D_Array(const N_2D_Array<T>& array) 00066 : N_2D_Array<T>(1, array.get_size(), array.begin()) {} 00067 template <class In> 00068 N_1D_Array(int size, In start) : N_2D_Array<T>(1, size, start) {} 00069 00070 // Indexing for read and write operations 00071 // 00072 T& operator()(int c) { return this->element(c); } 00073 T& operator[](int c) { return this->element(c); } 00074 const T& operator()(int c) const { return this->element(c); } 00075 const T& operator[](int c) const { return this->element(c); } 00076 00077 // This resizes the array, destroying its contents and re-allocating the 00078 // underlying space. It cannot be used on subarrays. 00079 // This has to be inlined here, or else a bug in the sparcSol2.6-CC compiler 00080 // surfaces, and there are link errors. 00081 void resize(int newSize) { 00082 if(N_2D_Array<T>::_num_of_cols != newSize) { 00083 if (N_2D_Array<T>::_isSubarray) { 00084 std::cerr << "N_1D_Array::resize cannot be used on a subarray!" 00085 << std::endl; 00086 // throw exception 00087 } else if (N_2D_Array<T>::_ref_count->get_value() != 1) { 00088 std::cerr << "N_1D_Array::resize cannot be used on an array being " 00089 "shared by subarrays" << std::endl; 00090 // throw exception 00091 } else { 00092 delete [] N_2D_Array<T>::_mem_base; 00093 delete [] N_2D_Array<T>::_index; 00094 N_2D_Array<T>::_init(1, newSize); 00095 } 00096 } 00097 } 00098 00099 }; 00100 //--------------------------------------------------------------------------- 00101 //---------------------< N_1D_Array Member Functions >----------------------- 00102 //--------------------------------------------------------------------------- 00103 00104 template <class T> 00105 std::ostream& operator<<(std::ostream& os, const N_1D_Array<T>& v) 00106 { 00107 os << "size: (" << v.get_size() << " )" << std::endl; 00108 os << "[ "; 00109 for (int i=0; i < cl_min(10, v.get_size()); ++i) 00110 os << " " << ((sizeof(T)==1) ? (int) v(i) : v(i)); 00111 os << " ]" << std::endl; 00112 return os; 00113 } 00114 //------------------------------------------------------------------------- 00115 00116 } // namespace claraty 00117 00118 #endif // N_1D_ARRAY_H