Array2D.h
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #ifndef ICL_CORE_ARRAY_2D_H_INCLUDED
00024 #define ICL_CORE_ARRAY_2D_H_INCLUDED
00025 
00026 #include <vector>
00027 #include <memory>
00028 #include <algorithm>
00029 
00030 namespace icl_core {
00031 
00037 template <typename T, typename TAllocator = std::allocator<T> >
00038 class Array2D
00039 {
00040 public:
00041   typedef std::vector<T, TAllocator> container_type;
00042   typedef typename container_type::value_type value_type;
00043   typedef typename container_type::allocator_type allocator_type;
00044   typedef typename container_type::size_type size_type;
00045   typedef typename container_type::difference_type difference_type;
00046   typedef typename container_type::reference reference;
00047   typedef typename container_type::const_reference const_reference;
00048   typedef typename container_type::pointer pointer;
00049   typedef typename container_type::const_pointer const_pointer;
00050   typedef typename container_type::iterator iterator;
00051   typedef typename container_type::const_iterator const_iterator;
00052   typedef typename container_type::reverse_iterator reverse_iterator;
00053   typedef typename container_type::const_reverse_iterator const_reverse_iterator;
00054 
00055   explicit Array2D(const allocator_type& alloc = allocator_type())
00056     : m_container(alloc),
00057       m_width(0),
00058       m_height(0)
00059   { }
00060 
00061   Array2D(size_t width, size_t height,
00062           const value_type& value = value_type(),
00063           const allocator_type& alloc = allocator_type())
00064     : m_container(width*height, value, alloc),
00065       m_width(width),
00066       m_height(height)
00067   { }
00068 
00069   Array2D(const Array2D& other)
00070     : m_container(other.m_container),
00071       m_width(other.m_width),
00072       m_height(other.m_height)
00073   { }
00074 
00075   Array2D& operator = (const Array2D& other)
00076   {
00077     m_container = other.m_container;
00078     m_width = other.m_width;
00079     m_height = other.m_height;
00080     return *this;
00081   }
00082 
00083   allocator_type get_allocator() const { return m_container.get_allocator(); }
00084 
00085   void resize(size_t width, size_t height)
00086   {
00087     m_container.resize(width*height);
00088     m_width = width;
00089     m_height = height;
00090   }
00091 
00092   void resize(size_t width, size_t height, const value_type& value)
00093   {
00094     m_container.resize(width*height, value);
00095     m_width = width;
00096     m_height = height;
00097   }
00098 
00099   void assign(size_t width, size_t height)
00100   {
00101     m_container.assign(width*height, value_type());
00102     m_width = width;
00103     m_height = height;
00104   }
00105 
00106   void assign(size_t width, size_t height, const value_type& value)
00107   {
00108     m_container.assign(width*height, value);
00109     m_width = width;
00110     m_height = height;
00111   }
00112 
00113   void reset()
00114   {
00115     m_container.assign(m_width*m_height, value_type());
00116   }
00117 
00118   void set(const value_type& value)
00119   {
00120     m_container.assign(m_width*m_height, value);
00121   }
00122 
00124   const T& at(size_t i) const { return m_container[i]; }
00126   T& at(size_t i) { return m_container[i]; }
00127 
00129   const T& operator [] (size_t i) const { return m_container[i]; }
00131   T& operator [] (size_t i) { return m_container[i]; }
00132 
00134   const T& at(size_t column, size_t row) const { return m_container[row*m_width + column]; }
00136   T& at(size_t column, size_t row) { return m_container[row*m_width + column]; }
00137 
00139   const T& operator () (size_t column, size_t row) const { return m_container[row*m_width + column]; }
00141   T& operator () (size_t column, size_t row) { return m_container[row*m_width + column]; }
00142 
00144   inline size_t index1D(size_t column, size_t row) const { return row*m_width + column; }
00145 
00147   inline void indexTo2D(size_t i, size_t& column, size_t& row) const
00148   {
00149     column = i % m_width;
00150     row = i / m_width;
00151   }
00152 
00154   void swap(Array2D& other)
00155   {
00156     m_container.swap(other.m_container);
00157     std::swap(m_width, other.m_width);
00158     std::swap(m_height, other.m_height);
00159   }
00160 
00161   size_t width() const { return m_width; }
00162   size_t height() const { return m_height; }
00163   size_t size() const { return m_container.size(); }
00164   size_type max_size() const { return m_container.max_size(); }
00165   size_type capacity() const { return m_container.capacity(); }
00166   bool empty() const { return m_container.empty(); }
00167 
00168   iterator begin() { return m_container.begin(); }
00169   iterator end() { return m_container.end(); }
00170   const_iterator begin() const { return m_container.begin(); }
00171   const_iterator end() const { return m_container.end(); }
00172 
00173 protected:
00174   std::vector<T> m_container;
00175   size_t m_width;
00176   size_t m_height;
00177 };
00178 
00179 }
00180 
00181 #endif


fzi_icl_core
Author(s):
autogenerated on Thu Jun 6 2019 20:22:23