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.LGPL **
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/CompatMacros.h>
00034 #include <ICLUtils/SmartArray.h>
00035 #include <ICLUtils/Size.h>
00036 #include <ICLUtils/Point.h>
00037 #include <algorithm>
00038 
00039 namespace icl{
00040   namespace utils{
00041     
00043 
00060     template<class T>
00061     class Array2D{
00062       
00064       Size m_size;
00065       
00067       SmartArray<T> m_data;
00068   
00069       public:
00070   
00072       typedef T* iterator;
00073   
00075       typedef const T* const_iterator;
00076       
00078       inline Array2D(){}
00079       
00081       inline Array2D(int w,int h): 
00082       m_size(w,h),m_data(new T[w*h]){}
00083       
00085       inline Array2D(const Size &s): 
00086       m_size(s),m_data(new T[s.getDim()]){}
00087       
00089       template<class Init>
00090       inline Array2D(int w, int h, Init init): 
00091       m_size(w,h),m_data(new T[w*h]){
00092         fill(init);
00093       }
00094       
00096       template<class Init>
00097       inline Array2D(const Size &s, Init init): 
00098       m_size(s),m_data(new T[s.getDim()]){
00099         fill(init);
00100       }
00101       
00103       inline Array2D(int w, int h, T *data, bool deepCopy=false):
00104       m_size(w,h),m_data(deepCopy ? new T[w*h] : data, deepCopy){
00105         if(deepCopy) assign(data,data+w*h);
00106       }
00107       
00109       inline Array2D(const Size &s, T *data, bool deepCopy=false):
00110       m_size(s),m_data(deepCopy ? new T[s.getDim()] : data, deepCopy){
00111         if(deepCopy) assign(data,data+s.getDim());
00112       }
00113       
00115       inline Array2D(int w, int h,const T *data):
00116       m_size(w,h),m_data(new T[w*h]){
00117         assign(data,data+w*h);
00118       }
00119       
00121       inline Array2D(const Size &s,const T *data):
00122       m_size(s),m_data(new T[s.getDim()]){
00123         assign(data,data+s.getDim());
00124       }
00125       
00127       template<class Iterator>
00128       inline Array2D(int w, int h,Iterator begin, Iterator end):
00129       m_size(w,h),m_data(new T[w*h]){
00130         assign(begin,end);
00131       }
00132       
00134       template<class Iterator>
00135       inline Array2D(const Size &s,Iterator begin, Iterator end):
00136       m_size(s),m_data(new T[s.getDim()]){
00137         assign(begin,end);
00138       }
00139       
00140       
00142       template<class Value>
00143       inline void fill(Value val){
00144         std::fill(begin(),end(),val);
00145       }
00146       
00148       template<class Iterator>
00149       inline void assign(Iterator begin, Iterator end){
00150         std::copy(begin,end,this->begin());
00151       }
00152   
00154       inline int getWidth() const { return m_size.width; }
00155   
00157       inline int getHeight() const { return m_size.height; }
00158   
00160       inline int getDim() const { return m_size.getDim(); }
00161   
00163       inline const Size &getSize() const { return m_size; }
00164   
00165   
00167       inline T &operator[](int idx) { return m_data.get()[idx]; }
00168   
00170       inline const T &operator[](int idx) const{ return m_data.get()[idx]; }
00171   
00173       inline T &operator()(int x,int y) { return m_data.get()[x+m_size.width*y]; }
00174   
00176       inline const T &operator()(int x, int y) const{ return m_data.get()[x+m_size.width*y]; }
00177   
00178       
00180       inline iterator begin() { return m_data.get(); }
00181   
00183       inline const_iterator begin() const { return m_data.get(); }
00184   
00186       inline iterator end() { return m_data.get()+getDim(); }
00187   
00189       inline const_iterator end() const{ return m_data.get()+getDim(); }
00190       
00192       inline Array2D<T> deepCopy() const{
00193         return Array2D<T>(getSize(),begin(),end());
00194       }
00195   
00197 
00201       inline void detach(){
00202           if(m_data.use_count() > 1){
00203             SmartArray<T> det(new T[getDim()]);
00204             std::copy(begin(),end(),det.get());
00205             m_data = det;
00206           }
00207       }
00208       
00210       inline T* data() { return begin(); }
00211   
00213       inline const T* data() const { return begin(); }
00214   
00216 
00218       inline const T &minElem(Point *pos=0) const { 
00219         int idx = (int)(std::min_element(begin(),end()) - begin());
00220         if(pos) *pos = Point(idx%getWidth(),idx/getWidth());
00221         return data()[idx];
00222       }
00223   
00225 
00227       inline const T &maxElem(Point *pos=0) const { 
00228         int idx = (int)(std::max_element(begin(),end()) - begin());
00229         if(pos) *pos = Point(idx%getWidth(),idx/getWidth());
00230         return data()[idx];
00231       }
00232     };
00233     
00234   
00235   
00236   } // namespace utils
00237 }
00238 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines