datablock.h

00001 
00004 
00005 //
00006 // Copyright (c) 2001-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Mon Mar 12 18:04:10 2001
00011 // commit: $Id: datablock.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/mtx/datablock.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_PKGS_MTX_DATABLOCK_H_UTC20050626084022_DEFINED
00036 #define GROOVX_PKGS_MTX_DATABLOCK_H_UTC20050626084022_DEFINED
00037 
00038 #include <cstddef>
00039 
00040 
00041 //  #######################################################
00042 //  =======================================================
00044 
00045 struct mtx_policies
00046 {
00047   enum init_policy { ZEROS, NO_INIT };
00048   enum storage_policy { COPY, BORROW, REFER };
00049 };
00050 
00051 
00053 
00054 class data_block
00055 {
00056 private:
00057   int m_refcount;
00058 
00059   data_block(const data_block& other); // not implemented
00060   data_block& operator=(const data_block& other); // not implemented
00061 
00062   static data_block* get_empty_data_block();
00063 
00064 protected:
00065   double* const m_storage;        
00066   unsigned int const m_length;    
00067 
00069 
00071   data_block(double* data, unsigned int len);
00072 
00074   virtual ~data_block();
00075 
00077   void* operator new(size_t bytes);
00078 
00080   void operator delete(void* space);
00081 
00083   int refcount() const { return m_refcount; }
00084 
00085 public:
00087   static data_block* make_zeros(int length);
00088 
00090 
00091   static data_block* make_uninitialized(int length);
00092 
00094   static data_block* make_data_copy(const double* data, int data_length);
00095 
00097 
00099   static data_block* make_borrowed(double* data, int data_length);
00100 
00102 
00104   static data_block* make_referred(double* data, int data_length);
00105 
00106   static data_block* make(double* data,
00107                           int mrows, int ncols,
00108                           mtx_policies::storage_policy s);
00109 
00110   static data_block* make(int mrows, int ncols,
00111                           mtx_policies::init_policy p);
00112 
00114   static void make_unique(data_block*& rep);
00115 
00117   void incr_refcount() { ++m_refcount; }
00118 
00120   void decr_refcount() { if (--m_refcount <= 0) delete this; }
00121 
00123   virtual bool is_unique() const = 0;
00124 
00125 
00127   const double* data()    const { return m_storage; }
00128 
00130         double* data_nc()       { return m_storage; }
00131 
00133   unsigned int  length()  const { return m_length; }
00134 };
00135 
00136 
00137 //  #######################################################
00138 //  =======================================================
00140 
00141 class data_holder : public mtx_policies
00142 {
00143 public:
00145   data_holder(double* data, int mrows, int ncols, storage_policy s);
00146 
00148   data_holder(int mrows, int ncols, init_policy p);
00149 
00151   data_holder(data_block* d);
00152 
00154   data_holder(const data_holder& other);
00155 
00157   ~data_holder();
00158 
00160   void swap(data_holder& other);
00161 
00163   void make_unique() { data_block::make_unique(m_data); }
00164 
00166   const double* storage() const { return m_data->data(); }
00167 
00169   double* storage_nc() { make_unique(); return m_data->data_nc(); }
00170 
00172   int storage_length() const { return m_data->length(); }
00173 
00174 private:
00175   data_holder& operator=(const data_holder&); // not allowed
00176 
00177   data_block* m_data;
00178 };
00179 
00180 
00181 //  #######################################################
00182 //  =======================================================
00184 
00185 class data_ref_holder : public mtx_policies
00186 {
00187 public:
00189   data_ref_holder(data_holder* ref) : m_ref(ref) {}
00190 
00192   data_ref_holder(const data_ref_holder& other) : m_ref(other.m_ref) {}
00193 
00195   ~data_ref_holder() {}
00196 
00198   void swap(data_ref_holder& other);
00199 
00201   const double* storage() const { return m_ref->storage(); }
00202 
00204   double* storage_nc() { return m_ref->storage_nc(); }
00205 
00207   int storage_length() const { return m_ref->storage_length(); }
00208 
00209 private:
00210   data_ref_holder& operator=(const data_ref_holder&); // not allowed
00211 
00212   data_holder* m_ref;
00213 };
00214 
00215 
00216 static const char __attribute__((used)) vcid_groovx_pkgs_mtx_datablock_h_utc20050626084022[] = "$Id: datablock.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00217 #endif // !GROOVX_PKGS_MTX_DATABLOCK_H_UTC20050626084022_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:39 2008 by Doxygen version 1.5.5.