Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Array2D.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLUtils/src/ICLUtils/Array2D.h                        **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.GPL  **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/SmartArray.h>
00034 #include <ICLUtils/Size.h>
00035 #include <ICLUtils/Point.h>
00036 #include <algorithm>
00037 
00038 namespace icl{
00039   namespace utils{
00040     
00042 
00059     template<class T>
00060     class Array2D{
00061       
00063       Size m_size;
00064       
00066       SmartArray<T> m_data;
00067   
00068       public:
00069   
00071       typedef T* iterator;
00072   
00074       typedef const T* const_iterator;
00075       
00077       inline Array2D(){}
00078       
00080       inline Array2D(int w,int h): 
00081       m_size(w,h),m_data(new T[w*h]){}
00082       
00084       inline Array2D(const Size &s): 
00085       m_size(s),m_data(new T[s.getDim()]){}
00086       
00088       template<class Init>
00089       inline Array2D(int w, int h, Init init): 
00090       m_size(w,h),m_data(new T[w*h]){
00091         fill(init);
00092       }
00093       
00095       template<class Init>
00096       inline Array2D(const Size &s, Init init): 
00097       m_size(s),m_data(new T[s.getDim()]){
00098         fill(init);
00099       }
00100       
00102       inline Array2D(int w, int h, T *data, bool deepCopy=false):
00103       m_size(w,h),m_data(deepCopy ? new T[w*h] : data, deepCopy){
00104         if(deepCopy) assign(data,data+w*h);
00105       }
00106       
00108       inline Array2D(const Size &s, T *data, bool deepCopy=false):
00109       m_size(s),m_data(deepCopy ? new T[s.getDim()] : data, deepCopy){
00110         if(deepCopy) assign(data,data+s.getDim());
00111       }
00112       
00114       inline Array2D(int w, int h,const T *data):
00115       m_size(w,h),m_data(new T[w*h]){
00116         assign(data,data+w*h);
00117       }
00118       
00120       inline Array2D(const Size &s,const T *data):
00121       m_size(s),m_data(new T[s.getDim()]){
00122         assign(data,data+s.getDim());
00123       }
00124       
00126       template<class Iterator>
00127       inline Array2D(int w, int h,Iterator begin, Iterator end):
00128       m_size(w,h),m_data(new T[w*h]){
00129         assign(begin,end);
00130       }
00131       
00133       template<class Iterator>
00134       inline Array2D(const Size &s,Iterator begin, Iterator end):
00135       m_size(s),m_data(new T[s.getDim()]){
00136         assign(begin,end);
00137       }
00138       
00139       
00141       template<class Value>
00142       inline void fill(Value val){
00143         std::fill(begin(),end(),val);
00144       }
00145       
00147       template<class Iterator>
00148       inline void assign(Iterator begin, Iterator end){
00149         std::copy(begin,end,this->begin());
00150       }
00151   
00153       inline int getWidth() const { return m_size.width; }
00154   
00156       inline int getHeight() const { return m_size.height; }
00157   
00159       inline int getDim() const { return m_size.getDim(); }
00160   
00162       inline const Size &getSize() const { return m_size; }
00163   
00164   
00166       inline T &operator[](int idx) { return m_data.get()[idx]; }
00167   
00169       inline const T &operator[](int idx) const{ return m_data.get()[idx]; }
00170   
00172       inline T &operator()(int x,int y) { return m_data.get()[x+m_size.width*y]; }
00173   
00175       inline const T &operator()(int x, int y) const{ return m_data.get()[x+m_size.width*y]; }
00176   
00177       
00179       inline iterator begin() { return m_data.get(); }
00180   
00182       inline const_iterator begin() const { return m_data.get(); }
00183   
00185       inline iterator end() { return m_data.get()+getDim(); }
00186   
00188       inline const_iterator end() const{ return m_data.get()+getDim(); }
00189       
00191       inline Array2D<T> deepCopy() const{
00192         return Array2D<T>(getSize(),begin(),end());
00193       }
00194   
00196 
00200       inline void detach(){
00201           if(m_data.use_count() > 1){
00202             SmartArray<T> det(new T[getDim()]);
00203             std::copy(begin(),end(),det.get());
00204             m_data = det;
00205           }
00206       }
00207       
00209       inline T* data() { return begin(); }
00210   
00212       inline const T* data() const { return begin(); }
00213   
00215 
00217       inline const T &minElem(Point *pos=0) const { 
00218         int idx = (int)(std::min_element(begin(),end()) - begin());
00219         if(pos) *pos = Point(idx%getWidth(),idx/getWidth());
00220         return data()[idx];
00221       }
00222   
00224 
00226       inline const T &maxElem(Point *pos=0) const { 
00227         int idx = (int)(std::max_element(begin(),end()) - begin());
00228         if(pos) *pos = Point(idx%getWidth(),idx/getWidth());
00229         return data()[idx];
00230       }
00231     };
00232     
00233   
00234   
00235   } // namespace utils
00236 }
00237 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines