Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers container.h Source File

container.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2014 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_CONTAINER__
00032 #define __ETL_CONTAINER__
00033 
00034 #include <stddef.h>
00035 #include <iterator>
00036 
00037 #include "platform.h "
00038 
00039 ///\defgroup container container
00040 ///\ingroup utilities
00041 
00042 namespace etl
00043 {
00044     //*****************************************************************************
00045     /// Get the 'begin' iterator.
00046   ///\ingroup container
00047     //*****************************************************************************
00048     template<typename TContainer>
00049   ETL_CONSTEXPR typename TContainer::iterator begin(TContainer& container)
00050     {
00051         return container.begin();
00052     }
00053 
00054     //*****************************************************************************
00055     /// Get the 'begin' const_iterator for a container.
00056   ///\ingroup container
00057     //*****************************************************************************
00058     template<typename TContainer>
00059   ETL_CONSTEXPR typename TContainer::const_iterator begin(const TContainer& container)
00060     {
00061         return container.begin();
00062     }
00063 
00064   //*****************************************************************************
00065   /// Get the 'begin' const_iterator for a container.
00066   ///\ingroup container
00067   //*****************************************************************************
00068   template<typename TContainer>
00069   ETL_CONSTEXPR typename TContainer::const_iterator cbegin(const TContainer& container)
00070   {
00071     return container.cbegin();
00072   }
00073 
00074   //*****************************************************************************
00075   /// Get the 'begin' reverse_iterator for a container.
00076   ///\ingroup container
00077   //*****************************************************************************
00078   template<typename TContainer>
00079   ETL_CONSTEXPR typename TContainer::reverse_iterator rbegin(const TContainer& container)
00080   {
00081     return container.rbegin();
00082   }
00083 
00084   //*****************************************************************************
00085   /// Get the 'begin' reverse_iterator for a container.
00086   ///\ingroup container
00087   //*****************************************************************************
00088   template<typename TContainer>
00089   ETL_CONSTEXPR typename TContainer::reverse_iterator crbegin(const TContainer& container)
00090   {
00091     return container.crbegin();
00092   }
00093 
00094     //*****************************************************************************
00095     /// Get the 'end' iterator for a container.
00096   ///\ingroup container
00097     //*****************************************************************************
00098     template<typename TContainer>
00099   ETL_CONSTEXPR typename TContainer::iterator end(TContainer& container)
00100     {
00101         return container.end();
00102     }
00103 
00104     //*****************************************************************************
00105     /// Get the 'end' const_iterator for a container.
00106   ///\ingroup container
00107     //*****************************************************************************
00108     template<typename TContainer>
00109   ETL_CONSTEXPR typename TContainer::const_iterator end(const TContainer& container)
00110     {
00111         return container.end();
00112     }
00113 
00114   //*****************************************************************************
00115   /// Get the 'end' const_iterator for a container.
00116   ///\ingroup container
00117   //*****************************************************************************
00118   template<typename TContainer>
00119   ETL_CONSTEXPR typename TContainer::const_iterator cend(const TContainer& container)
00120   {
00121     return container.cend();
00122   }
00123 
00124   //*****************************************************************************
00125   /// Get the 'end' reverse_iterator for a container.
00126   ///\ingroup container
00127   //*****************************************************************************
00128   template<typename TContainer>
00129   ETL_CONSTEXPR typename TContainer::const_iterator rend(const TContainer& container)
00130   {
00131     return container.rend();
00132   }
00133 
00134   //*****************************************************************************
00135   /// Get the 'end' reverse_iterator for a container.
00136   ///\ingroup container
00137   //*****************************************************************************
00138   template<typename TContainer>
00139   ETL_CONSTEXPR typename TContainer::reverse_iterator crend(const TContainer& container)
00140   {
00141     return container.crend();
00142   }
00143 
00144     //*****************************************************************************
00145     /// Get the 'begin' pointer for an array.
00146   ///\ingroup container
00147     //*****************************************************************************
00148     template<typename TValue, const size_t ARRAY_SIZE>
00149   ETL_CONSTEXPR TValue* begin(TValue(&data)[ARRAY_SIZE])
00150     {
00151         return &data[0];
00152     }
00153 
00154   //*****************************************************************************
00155   /// Get the 'begin' const iterator for an array.
00156   ///\ingroup container
00157   //*****************************************************************************
00158   template<typename TValue, const size_t ARRAY_SIZE>
00159   ETL_CONSTEXPR const TValue* begin(const TValue(&data)[ARRAY_SIZE])
00160   {
00161     return &data[0];
00162   }
00163 
00164   //*****************************************************************************
00165   /// Get the 'begin' const iterator for an array.
00166   ///\ingroup container
00167   //*****************************************************************************
00168   template<typename TValue, const size_t ARRAY_SIZE>
00169   ETL_CONSTEXPR const TValue* cbegin(const TValue(&data)[ARRAY_SIZE])
00170   {
00171     return &data[0];
00172   }
00173 
00174   //*****************************************************************************
00175   /// Get the 'begin' reverse_iterator for an array.
00176   ///\ingroup container
00177   //*****************************************************************************
00178   template<typename TValue, const size_t ARRAY_SIZE>
00179   ETL_CONSTEXPR std::reverse_iterator<TValue*> rbegin(const TValue(&data)[ARRAY_SIZE])
00180   {
00181     return std::reverse_iterator<TValue*>(&data[ARRAY_SIZE]);
00182   }
00183 
00184   //*****************************************************************************
00185   /// Get the 'begin' const reverse_iterator for an array.
00186   ///\ingroup container
00187   //*****************************************************************************
00188   template<typename TValue, const size_t ARRAY_SIZE>
00189   ETL_CONSTEXPR std::reverse_iterator<const TValue*> crbegin(const TValue(&data)[ARRAY_SIZE])
00190   {
00191     return std::reverse_iterator<const TValue*>(&data[ARRAY_SIZE]);
00192   }
00193 
00194     //*****************************************************************************
00195     /// Get the 'end' iterator for an array.
00196   ///\ingroup container
00197     //*****************************************************************************
00198     template<typename TValue, const size_t ARRAY_SIZE>
00199   ETL_CONSTEXPR TValue* end(TValue(&data)[ARRAY_SIZE])
00200     {
00201         return &data[ARRAY_SIZE];
00202     }
00203 
00204   //*****************************************************************************
00205   /// Get the 'end' const iterator for an array.
00206   ///\ingroup container
00207   //*****************************************************************************
00208   template<typename TValue, const size_t ARRAY_SIZE>
00209   ETL_CONSTEXPR const TValue* end(const TValue(&data)[ARRAY_SIZE])
00210   {
00211     return &data[ARRAY_SIZE];
00212   }
00213 
00214   //*****************************************************************************
00215   /// Get the 'end' const iterator for an array.
00216   ///\ingroup container
00217   //*****************************************************************************
00218   template<typename TValue, const size_t ARRAY_SIZE>
00219   ETL_CONSTEXPR const TValue* cend(const TValue(&data)[ARRAY_SIZE])
00220   {
00221     return &data[ARRAY_SIZE];
00222   }
00223 
00224   //*****************************************************************************
00225   /// Get the 'end' reverse_iterator for an array.
00226   ///\ingroup container
00227   //*****************************************************************************
00228   template<typename TValue, const size_t ARRAY_SIZE>
00229   ETL_CONSTEXPR std::reverse_iterator<TValue*> rend(const TValue(&data)[ARRAY_SIZE])
00230   {
00231     return std::reverse_iterator<TValue*>(&data[0]);
00232   }
00233 
00234   //*****************************************************************************
00235   /// Get the 'end' const reverse_iterator for an array.
00236   ///\ingroup container
00237   //*****************************************************************************
00238   template<typename TValue, const size_t ARRAY_SIZE>
00239   ETL_CONSTEXPR std::reverse_iterator<const TValue*> crend(const TValue(&data)[ARRAY_SIZE])
00240   {
00241     return std::reverse_iterator<const TValue*>(&data[0]);
00242   }
00243 
00244   //*****************************************************************************
00245     /// Get the next iterator.
00246   ///\ingroup container
00247     //*****************************************************************************
00248   template<class TIterator>
00249   TIterator next(TIterator iterator, ptrdiff_t n = 1)
00250   {
00251       std::advance(iterator, n);
00252       return iterator;
00253   }
00254 
00255   //*****************************************************************************
00256     /// Get the previous iterator.
00257   ///\ingroup container
00258     //*****************************************************************************
00259   template<class TIterator>
00260   TIterator prev(TIterator iterator, ptrdiff_t n = 1)
00261   {
00262       std::advance(iterator, -n);
00263       return iterator;
00264   }
00265 
00266   ///**************************************************************************
00267   /// Get the size of a container.
00268   /// Expects the container to have defined 'size_type'.
00269   ///\ingroup container
00270   ///**************************************************************************
00271   template<typename TContainer>
00272   ETL_CONSTEXPR typename TContainer::size_type size(const TContainer& container)
00273   {
00274     return container.size();
00275   }
00276 
00277   ///**************************************************************************
00278   /// Get the size of an array in elements at run time, or compile time if C++11 or above.
00279   ///\ingroup container
00280   ///**************************************************************************
00281   template<typename TValue, const size_t ARRAY_SIZE>
00282   ETL_CONSTEXPR size_t size(TValue(&)[ARRAY_SIZE])
00283   {
00284     return ARRAY_SIZE;
00285   }
00286 
00287   ///**************************************************************************
00288   /// Get the size of an array in elements at compile time for C++03
00289   ///\code
00290   /// sizeof(array_size(array))
00291   ///\endcode
00292   ///\ingroup container
00293   ///**************************************************************************
00294   template <typename T, const size_t ARRAY_SIZE>
00295   char(&array_size(T(&array)[ARRAY_SIZE]))[ARRAY_SIZE];
00296 }
00297 
00298 #if ETL_CPP11_SUPPORTED
00299   #define ETL_ARRAY_SIZE(a) (etl::size(a))
00300 #else
00301   #define ETL_ARRAY_SIZE(a) sizeof(etl::array_size(a))
00302 #endif
00303 
00304 #endif
00305 
00306