rn.hh

Go to the documentation of this file.
00001 /*************************************************************************
00002 
00003 Copyright Rice University, 2004.
00004 All rights reserved.
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a
00007 copy of this software and associated documentation files (the "Software"),
00008 to deal in the Software without restriction, including without limitation
00009 the rights to use, copy, modify, merge, publish, distribute, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, provided that the above copyright notice(s) and this
00012 permission notice appear in all copies of the Software and that both the
00013 above copyright notice(s) and this permission notice appear in supporting
00014 documentation.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
00019 RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
00020 NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
00021 DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00022 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00023 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
00024 THIS SOFTWARE.
00025 
00026 Except as contained in this notice, the name of a copyright holder shall
00027 not be used in advertising or otherwise to promote the sale, use or other
00028 dealings in this Software without prior written authorization of the
00029 copyright holder.
00030 
00031 **************************************************************************/
00032 
00033 #ifndef __RVLRN
00034 #define __RVLRN
00035 
00036 #include "local.hh"
00037 
00038 namespace RVL {
00039 
00044   template<class Scalar>
00045   class RnArray: public LocalDataContainer<Scalar> {
00046   private:
00047 
00048     size_t n;
00049     int own;
00050     size_t start;
00051     mutable Scalar * a;
00052 
00053     RnArray() {}
00054 
00055   public:
00056 
00058     RnArray(const RnArray<Scalar> & x)
00059       : n(x.n), own(1), start(0), a(new Scalar[n]) {
00060       for (size_t i=0;i<n;i++) {
00061     a[i]=x.a[i];
00062       }
00063     }
00064 
00066     RnArray(size_t _n)
00067       : n(_n),
00068     own(1), start(0), a(NULL) {
00069       if (n<=0) {
00070     RVLException e;
00071     e<<"Error: RnArray constructor\n";
00072     e<<"must have positive dimension = "<<n<<"\n";
00073     throw e;
00074       }
00075     }
00076 
00078     RnArray(LocalDataContainer<Scalar> & rn, size_t len, size_t _start=0)
00079       : start(_start), n(len), own(0) {
00080       a = &(rn.getData()[start]);
00081       if (start+n>rn.getSize()) {
00082     RVLException e;
00083     e<<"Error: RnArray \"view\" constructor\n";
00084     e<<"segment runs off end of input LocalDC\n";
00085     e<<"length of input LocalDC = "<<rn.getSize()<<"\n";
00086     e<<"index of segment start  = "<<start<<"\n";
00087     e<<"length of segment       = "<<n<<"\n";
00088     throw e;
00089       }
00090     }
00091 
00092     ~RnArray() { if (own && a) delete [] a; }
00093 
00094     size_t getSize() const { return n; }
00095 
00096     Scalar * getData() { 
00097       if (!a) a = new Scalar[n];
00098       return a; 
00099     }
00100 
00101     Scalar const * getData() const {
00102       if (!a) a = new Scalar[n];
00103       return a;
00104     }
00105 
00106     void write(RVLException & str) const {
00107       str<<"RnArray Local Data Container object\n";
00108       str<<"length = "<<n<<"\n";
00109       if (a) {
00110     str<<"samples: \n";
00111     for (size_t i=0;i<n;i++) {
00112       str<<"data["<<i<<"] = "<<a[i]<<"\n";
00113     }
00114       }
00115       else {
00116     str<<"data samples not initialized\n";
00117       }
00118     }
00119 
00120     ostream & write(ostream & str) const {
00121       str<<"RnArray Local Data Container object\n";
00122       str<<"length = "<<n<<"\n";
00123       str<<"samples: \n";
00124       if (a) {
00125     for (size_t i=0;i<n;i++) {
00126       str<<"data["<<i<<"] = "<<a[i]<<"\n";
00127     }
00128       }
00129       else {
00130     str<<"data samples not initialized\n";
00131       }
00132       return str;
00133     }
00134   };
00135 
00137   template<class Scalar> 
00138   class RnDataContainerFactory: public LocalDataContainerFactory<Scalar> {
00139 
00140   private:
00141 
00142     size_t n;
00143 
00144   protected:
00145 
00146     void redim(size_t _n) { n = _n; }
00147 
00148   public:
00149 
00150     RnDataContainerFactory(size_t _n=0): n(_n) {}
00151     RnDataContainerFactory(const RnDataContainerFactory<Scalar> & f): n(f.n) {}
00152     ~RnDataContainerFactory() {}
00153 
00154     virtual LocalDataContainer<Scalar> * buildLocal() const { 
00155       try {
00156     return new RnArray<Scalar>(n);
00157       }
00158       catch (RVLException & e) {
00159     e<<"\ncalled from RnDataContainerFactory::buildLocal()\n";
00160     throw e;
00161       }
00162     }      
00163 
00169     virtual bool compare( DataContainerFactory const & dcf) const {
00170       try {
00171     if (&dcf == this) return 1;
00172     RnDataContainerFactory<Scalar> const & a = 
00173       dynamic_cast< RnDataContainerFactory<Scalar> const &>(dcf);
00174     if (n == a.n) return 1;
00175     return 0;
00176       }
00177       catch (bad_cast) {
00178     return 0;
00179       }
00180     }
00181 
00186     virtual bool isCompatible(DataContainer const & dc) const  {
00187       try {
00188     RnArray<Scalar> const & a = 
00189       dynamic_cast<RnArray<Scalar> const &>(dc);
00190     if (n == a.getSize()) return 1;
00191     return 0;
00192       }
00193       catch (bad_cast) {
00194     return 0;
00195       }
00196     }
00197 
00198     size_t getSize() const { return n; }
00199 
00200     virtual void write(RVLException & e) const {
00201       e<<"RnDataContainerFactory, dimn = "<<n<<"\n";
00202     }
00203     virtual ostream & write(ostream & str) const {
00204       str<<"RnDataContainerFactory, dimn = "<<n<<"\n";
00205       return str;
00206     }
00207 
00208   };
00209 
00210 }
00211 
00212 #endif
00213 
00214 
00215 
00216 
00217 
00218 
00219 

Generated on 5 Jan 2017 for LocalRVL by  doxygen 1.4.7