メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

DiagArray2.h

解説を見る。
00001 // Template array classes
00002 /*
00003 
00004 Copyright (C) 1996, 1997 John W. Eaton
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 2, or (at your option) any
00011 later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, write to the Free
00020 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 
00022 */
00023 
00024 #if !defined (octave_DiagArray2_h)
00025 #define octave_DiagArray2_h 1
00026 
00027 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION)
00028 #pragma interface
00029 #endif
00030 
00031 #include <cassert>
00032 #include <cstdlib>
00033 
00034 #include "Array.h"
00035 #include "lo-error.h"
00036 
00037 class idx_vector;
00038 
00039 // A two-dimensional array with diagonal elements only.
00040 //
00041 // Idea and example code for Proxy class and functions from:
00042 //
00043 // From: kanze@us-es.sel.de (James Kanze)
00044 // Subject: Re: How to overload [] to do READ/WRITE differently ?
00045 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de>
00046 // Sender: news@us-es.sel.de
00047 // Date: 29 Nov 1993 14:14:07 GMT
00048 // --
00049 // James Kanze                             email: kanze@us-es.sel.de
00050 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
00051 
00052 template <class T>
00053 class
00054 DiagArray2 : public Array<T>
00055 {
00056 private:
00057 
00058   T get (int i) { return Array<T>::xelem (i); }
00059 
00060   void set (const T& val, int i) { Array<T>::xelem (i) = val; }
00061 
00062   class Proxy
00063   {
00064   public:
00065 
00066     Proxy (DiagArray2<T> *ref, int r, int c)
00067       : i (r), j (c), object (ref) { } 
00068 
00069     const Proxy& operator = (const T& val) const
00070       {
00071         if (i == j)
00072           {
00073             if (object)
00074               object->set (val, i);
00075           }
00076         else
00077           (*current_liboctave_error_handler)
00078             ("invalid assignment to off-diagonal in diagonal array");
00079 
00080         return *this;
00081       }
00082 
00083     operator T () const
00084       {
00085         if (object && i == j)
00086           return object->get (i);
00087         else
00088           {
00089             static T foo (0);
00090             return foo;
00091           }
00092       }
00093 
00094   private:
00095 
00096     // XXX FIXME XXX -- this is declared private to keep the user from
00097     // taking the address of a Proxy.  Maybe it should be implemented
00098     // by means of a companion function in the DiagArray2 class.
00099 
00100     T *operator& () const { assert (0); return (T *) 0; }
00101 
00102     int i;
00103     int j;
00104 
00105     DiagArray2<T> *object;
00106 
00107   };
00108 
00109 friend class Proxy;
00110 
00111 protected:
00112 
00113   DiagArray2 (T *d, int r, int c) : Array<T> (d, r < c ? r : c)
00114     { Array<T>::dimensions = dim_vector (r, c); }
00115 
00116 public:
00117 
00118   DiagArray2 (void) : Array<T> (dim_vector (0, 0)) { }
00119 
00120   DiagArray2 (int r, int c) : Array<T> (r < c ? r : c)
00121     { this->dimensions = dim_vector (r, c); }
00122 
00123   DiagArray2 (int r, int c, const T& val) : Array<T> (r < c ? r : c)
00124     {
00125       this->dimensions = dim_vector (r, c);
00126 
00127       fill (val);
00128     }
00129 
00130   DiagArray2 (const Array<T>& a) : Array<T> (a)
00131     { this->dimensions = dim_vector (a.length (), a.length ()); }
00132 
00133   DiagArray2 (const DiagArray2<T>& a) : Array<T> (a)
00134     { this->dimensions = a.dims (); }
00135 
00136   ~DiagArray2 (void) { }
00137 
00138   DiagArray2<T>& operator = (const DiagArray2<T>& a)
00139     {
00140       if (this != &a)
00141         Array<T>::operator = (a);
00142 
00143       return *this;
00144     }
00145 
00146   Proxy elem (int r, int c)
00147     {
00148       return Proxy (this, r, c);
00149     }
00150 
00151   Proxy checkelem (int r, int c)
00152     {
00153       if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ())
00154         {
00155           (*current_liboctave_error_handler) ("range error in DiagArray2");
00156           return Proxy (0, r, c);
00157         }
00158       else
00159         return Proxy (this, r, c);
00160     }
00161 
00162   Proxy operator () (int r, int c)
00163     {
00164       if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ())
00165         {
00166           (*current_liboctave_error_handler) ("range error in DiagArray2");
00167           return Proxy (0, r, c);
00168         }
00169       else
00170         return Proxy (this, r, c);
00171   }
00172 
00173   T elem (int r, int c) const;
00174   T checkelem (int r, int c) const;
00175   T operator () (int r, int c) const;
00176 
00177   // No checking.
00178 
00179   T& xelem (int r, int c);
00180   T xelem (int r, int c) const;
00181 
00182   void resize (int n, int m);
00183   void resize (int n, int m, const T& val);
00184 
00185   void maybe_delete_elements (idx_vector& i, idx_vector& j);
00186 };
00187 
00188 #endif
00189 
00190 /*
00191 ;;; Local Variables: ***
00192 ;;; mode: C++ ***
00193 ;;; End: ***
00194 */

Wed Dec 29 11:51:13 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送