Scopira 20080306

islice.h

00001 
00002 /*
00003  *  Copyright (c) 2002    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED_SCOPIRA_BASEKIT_ISLICE_H__
00015 #define __INCLUDED_SCOPIRA_BASEKIT_ISLICE_H__
00016 
00017 #include <scopira/tool/array.h>
00018 
00019 namespace scopira
00020 {
00021   namespace basekit
00022   {
00023     template <class T> class islice_vec_iterator_g;
00024     template <class T> class const_islice_vec_iterator_g;
00025     template <class T> class islice_vec_g;
00026   }
00027 }
00028 
00033 template <class T> class scopira::basekit::islice_vec_iterator_g
00034 {
00035   public:
00037     typedef T data_type;
00038   private:
00039     T * const * dm_ptr;
00040   public:
00042     islice_vec_iterator_g(T * const * ptr) : dm_ptr(ptr) { }
00044     const T & operator *(void) const { return **dm_ptr; }
00046     bool operator ==(const islice_vec_iterator_g<T> &rhs) const { return dm_ptr == rhs.dm_ptr; }
00048     bool operator !=(const islice_vec_iterator_g<T> &rhs) const { return dm_ptr != rhs.dm_ptr; }
00049 
00051     islice_vec_iterator_g<T>& operator++(void) { dm_ptr++; return *this; }
00052 };
00053 
00058 template <class T> class scopira::basekit::const_islice_vec_iterator_g
00059 {
00060   public:
00062     typedef T data_type;
00063   private:
00064     const T * const * dm_ptr;
00065   public:
00067     const_islice_vec_iterator_g(const T * const * ptr) : dm_ptr(ptr) { }
00069     const T & operator *(void) const { return **dm_ptr; }
00071     bool operator ==(const const_islice_vec_iterator_g<T> &rhs) const { return dm_ptr == rhs.dm_ptr; }
00073     bool operator !=(const const_islice_vec_iterator_g<T> &rhs) const { return dm_ptr != rhs.dm_ptr; }
00074 
00076     const_islice_vec_iterator_g<T>& operator++(void) { dm_ptr++; return *this; }
00077 };
00078 
00084 template <class T> class scopira::basekit::islice_vec_g : private scopira::tool::basic_array<T*>
00085 {
00086   private:
00087     typedef scopira::tool::basic_array<T*> parent_type;
00088   public:
00090     typedef T data_type;
00092     typedef islice_vec_iterator_g<T> iterator;
00094     typedef const_islice_vec_iterator_g<T> const_iterator;
00095 
00096   public:
00097     // default default ctor - ok
00098 
00100     explicit islice_vec_g(size_t len) : parent_type(len) { }
00101 
00103     size_t size(void) const { return parent_type::size(); }
00105     bool empty(void) const { return parent_type::empty(); }
00107     void resize(size_t len) { parent_type::resize(len); }
00109     void clear(void) { parent_type::clear(); }
00110 
00112     iterator begin(void) { return islice_vec_iterator_g<T>(dm_ary); }
00114     iterator end(void) { return islice_vec_iterator_g<T>(dm_ary+dm_len); }
00116     const_iterator begin(void) const { return const_islice_vec_iterator_g<T>(dm_ary); }
00118     const_iterator end(void) const { return const_islice_vec_iterator_g<T>(dm_ary+dm_len); }
00119 
00121     template <class L>
00122       void copy(const L & rhs);
00123 
00125     void set_ptr(size_t idx, T * ref) { parent_type::set(idx, ref); }
00126 
00128     T &operator[](size_t idx) { return *parent_type::get(idx); }
00130     const T &operator[](size_t idx) const { return *parent_type::get(idx); }
00132     T & get(size_t idx) { return *parent_type::get(idx); }
00134     const T & get(size_t idx) const { return *parent_type::get(idx); }
00136     void set(size_t idx, const T &val) { *parent_type::get(idx) = val; }
00137 };
00138 template <class T> template <class L>
00139 void scopira::basekit::islice_vec_g<T>::copy(const L &rhs)
00140 {
00141   typename L::const_iterator ii;
00142   size_t i;
00143   resize( rhs.size() );
00144   for (i=0, ii=rhs.begin(); ii != rhs.end(); ++i, ++ii)
00145     set(i, *ii);
00146 }
00147 
00148 #endif
00149