00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION)
00025 #pragma implementation
00026 #endif
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032 #include <cassert>
00033
00034 #include <iostream>
00035
00036 #include "DiagArray2.h"
00037
00038 #include "lo-error.h"
00039
00040
00041
00042 template <class T>
00043 T
00044 DiagArray2<T>::elem (int r, int c) const
00045 {
00046 return (r == c) ? Array<T>::xelem (r) : T (0);
00047 }
00048
00049 template <class T>
00050 T
00051 DiagArray2<T>::checkelem (int r, int c) const
00052 {
00053 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ())
00054 {
00055 (*current_liboctave_error_handler) ("range error in DiagArray2");
00056 return T ();
00057 }
00058 return (r == c) ? Array<T>::xelem (r) : T (0);
00059 }
00060
00061 template <class T>
00062 T
00063 DiagArray2<T>::operator () (int r, int c) const
00064 {
00065 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ())
00066 {
00067 (*current_liboctave_error_handler) ("range error in DiagArray2");
00068 return T ();
00069 }
00070 return (r == c) ? Array<T>::xelem (r) : T (0);
00071 }
00072
00073 template <class T>
00074 T&
00075 DiagArray2<T>::xelem (int r, int c)
00076 {
00077 static T foo (0);
00078 return (r == c) ? Array<T>::xelem (r) : foo;
00079 }
00080
00081 template <class T>
00082 T
00083 DiagArray2<T>::xelem (int r, int c) const
00084 {
00085 return (r == c) ? Array<T>::xelem (r) : T (0);
00086 }
00087
00088 template <class T>
00089 void
00090 DiagArray2<T>::resize (int r, int c)
00091 {
00092 if (r < 0 || c < 0)
00093 {
00094 (*current_liboctave_error_handler) ("can't resize to negative dimensions");
00095 return;
00096 }
00097
00098 if (r == this->dim1 () && c == this->dim2 ())
00099 return;
00100
00101 typename Array<T>::ArrayRep *old_rep = Array<T>::rep;
00102 const T *old_data = this->data ();
00103 int old_len = this->length ();
00104
00105 int new_len = r < c ? r : c;
00106
00107 Array<T>::rep = new typename Array<T>::ArrayRep (new_len);
00108
00109 this->dimensions = dim_vector (r, c);
00110
00111 if (old_data && old_len > 0)
00112 {
00113 int min_len = old_len < new_len ? old_len : new_len;
00114
00115 for (int i = 0; i < min_len; i++)
00116 xelem (i, i) = old_data[i];
00117 }
00118
00119 if (--old_rep->count <= 0)
00120 delete old_rep;
00121 }
00122
00123 template <class T>
00124 void
00125 DiagArray2<T>::resize (int r, int c, const T& val)
00126 {
00127 if (r < 0 || c < 0)
00128 {
00129 (*current_liboctave_error_handler) ("can't resize to negative dimensions");
00130 return;
00131 }
00132
00133 if (r == this->dim1 () && c == this->dim2 ())
00134 return;
00135
00136 typename Array<T>::ArrayRep *old_rep = Array<T>::rep;
00137 const T *old_data = this->data ();
00138 int old_len = this->length ();
00139
00140 int new_len = r < c ? r : c;
00141
00142 Array<T>::rep = new typename Array<T>::ArrayRep (new_len);
00143
00144 this->dimensions = dim_vector (r, c);
00145
00146 int min_len = old_len < new_len ? old_len : new_len;
00147
00148 if (old_data && old_len > 0)
00149 {
00150 for (int i = 0; i < min_len; i++)
00151 xelem (i, i) = old_data[i];
00152 }
00153
00154 for (int i = min_len; i < new_len; i++)
00155 xelem (i, i) = val;
00156
00157 if (--old_rep->count <= 0)
00158 delete old_rep;
00159 }
00160
00161
00162
00163
00164
00165