Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ivectorpointer.h Source File

ivectorpointer.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) 2016 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_IVECTOR_POINTER__
00032 #define __ETL_IVECTOR_POINTER__
00033 
00034 #ifndef __ETL_IN_VECTOR_H__
00035 #error  This header is a private element of etl::ivector
00036 #endif
00037 
00038 #include "pvoidvector.h "
00039 
00040 namespace etl
00041 {
00042   //***************************************************************************
00043   /// The base class for specifically sized vectors.
00044   /// Can be used as a reference type for all vectors containing a specific pointer type.
00045   ///\ingroup vector
00046   //***************************************************************************
00047   template <typename T>
00048   class ivector<T*> : public pvoidvector
00049   {
00050   public:
00051 
00052     typedef T*                                    value_type;
00053     typedef T*&                                   reference;
00054     typedef const T* const &                      const_reference;
00055     typedef T**                                   pointer;
00056     typedef const T* const *                      const_pointer;
00057     typedef T**                                   iterator;
00058     typedef const T* const *                      const_iterator;
00059     typedef std::reverse_iterator<iterator>       reverse_iterator;
00060     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00061     typedef size_t                                size_type;
00062     typedef typename std::iterator_traits<iterator>::difference_type difference_type;
00063 
00064   protected:
00065 
00066     typedef value_type parameter_t;
00067 
00068   private:
00069 
00070     typedef pvoidvector base_t;
00071 
00072   public:
00073 
00074     //*********************************************************************
00075     /// Returns an iterator to the beginning of the vector.
00076     ///\return An iterator to the beginning of the vector.
00077     //*********************************************************************
00078     iterator begin()
00079     {
00080       return iterator(base_t::begin());
00081     }
00082 
00083     //*********************************************************************
00084     /// Returns a const_iterator to the beginning of the vector.
00085     ///\return A const iterator to the beginning of the vector.
00086     //*********************************************************************
00087     const_iterator begin() const
00088     {
00089       return const_iterator(base_t::begin());
00090     }
00091 
00092     //*********************************************************************
00093     /// Returns an iterator to the end of the vector.
00094     ///\return An iterator to the end of the vector.
00095     //*********************************************************************
00096     iterator end()
00097     {
00098       return iterator(base_t::end());
00099     }
00100 
00101     //*********************************************************************
00102     /// Returns a const_iterator to the end of the vector.
00103     ///\return A const iterator to the end of the vector.
00104     //*********************************************************************
00105     const_iterator end() const
00106     {
00107       return const_iterator(base_t::end());
00108     }
00109 
00110     //*********************************************************************
00111     /// Returns a const_iterator to the beginning of the vector.
00112     ///\return A const iterator to the beginning of the vector.
00113     //*********************************************************************
00114     const_iterator cbegin() const
00115     {
00116       return const_iterator(base_t::cbegin());
00117     }
00118 
00119     //*********************************************************************
00120     /// Returns a const_iterator to the end of the vector.
00121     ///\return A const iterator to the end of the vector.
00122     //*********************************************************************
00123     const_iterator cend() const
00124     {
00125       return const_iterator(base_t::cend());
00126     }
00127 
00128     //*********************************************************************
00129     /// Returns an reverse iterator to the reverse beginning of the vector.
00130     ///\return Iterator to the reverse beginning of the vector.
00131     //*********************************************************************
00132     reverse_iterator rbegin()
00133     {
00134       return reverse_iterator(iterator(base_t::end()));
00135     }
00136 
00137     //*********************************************************************
00138     /// Returns a const reverse iterator to the reverse beginning of the vector.
00139     ///\return Const iterator to the reverse beginning of the vector.
00140     //*********************************************************************
00141     const_reverse_iterator rbegin() const
00142     {
00143       return const_reverse_iterator(const_iterator(base_t::end()));
00144     }
00145 
00146     //*********************************************************************
00147     /// Returns a reverse iterator to the end + 1 of the vector.
00148     ///\return Reverse iterator to the end + 1 of the vector.
00149     //*********************************************************************
00150     reverse_iterator rend()
00151     {
00152       return reverse_iterator(iterator(base_t::begin()));
00153     }
00154 
00155     //*********************************************************************
00156     /// Returns a const reverse iterator to the end + 1 of the vector.
00157     ///\return Const reverse iterator to the end + 1 of the vector.
00158     //*********************************************************************
00159     const_reverse_iterator rend() const
00160     {
00161       return const_reverse_iterator(const_iterator(base_t::begin()));
00162     }
00163 
00164     //*********************************************************************
00165     /// Returns a const reverse iterator to the reverse beginning of the vector.
00166     ///\return Const reverse iterator to the reverse beginning of the vector.
00167     //*********************************************************************
00168     const_reverse_iterator crbegin() const
00169     {
00170       return const_reverse_iterator(const_iterator(base_t::cend()));
00171     }
00172 
00173     //*********************************************************************
00174     /// Returns a const reverse iterator to the end + 1 of the vector.
00175     ///\return Const reverse iterator to the end + 1 of the vector.
00176     //*********************************************************************
00177     const_reverse_iterator crend() const
00178     {
00179       return const_reverse_iterator(const_iterator(base_t::cbegin()));
00180     }
00181 
00182     //*********************************************************************
00183     /// Resizes the vector.
00184     /// If asserts or exceptions are enabled and the new size is larger than the
00185     /// maximum then a vector_full is thrown.
00186     ///\param new_size The new size.
00187     //*********************************************************************
00188     void resize(size_t new_size)
00189     {
00190       base_t::resize(new_size);
00191     }
00192 
00193     //*********************************************************************
00194     /// Resizes the vector.
00195     /// If asserts or exceptions are enabled and the new size is larger than the
00196     /// maximum then a vector_full is thrown.
00197     ///\param new_size The new size.
00198     ///\param value   The value to fill new elements with. Default = default constructed value.
00199     //*********************************************************************
00200     void resize(size_t new_size, value_type value)
00201     {
00202       base_t::resize(new_size, value);
00203     }
00204 
00205     //*********************************************************************
00206     /// Returns a reference to the value at index 'i'
00207     ///\param i The index.
00208     ///\return A reference to the value at index 'i'
00209     //*********************************************************************
00210     reference operator [](size_t i)
00211     {
00212       return reference(base_t::operator[](i));
00213     }
00214 
00215     //*********************************************************************
00216     /// Returns a const reference to the value at index 'i'
00217     ///\param i The index.
00218     ///\return A const reference to the value at index 'i'
00219     //*********************************************************************
00220     const_reference operator [](size_t i) const
00221     {
00222       return const_reference(base_t::operator[](i));
00223     }
00224 
00225     //*********************************************************************
00226     /// Returns a reference to the value at index 'i'
00227     /// If asserts or exceptions are enabled, emits an etl::vector_out_of_bounds if the index is out of range.
00228     ///\param i The index.
00229     ///\return A reference to the value at index 'i'
00230     //*********************************************************************
00231     reference at(size_t i)
00232     {
00233       return reference(base_t::at(i));
00234     }
00235 
00236     //*********************************************************************
00237     /// Returns a const reference to the value at index 'i'
00238     /// If asserts or exceptions are enabled, emits an etl::vector_out_of_bounds if the index is out of range.
00239     ///\param i The index.
00240     ///\return A const reference to the value at index 'i'
00241     //*********************************************************************
00242     const_reference at(size_t i) const
00243     {
00244       return const_reference(base_t::at(i));
00245     }
00246 
00247     //*********************************************************************
00248     /// Returns a reference to the first element.
00249     ///\return A reference to the first element.
00250     //*********************************************************************
00251     reference front()
00252     {
00253       return reference(base_t::front());
00254     }
00255 
00256     //*********************************************************************
00257     /// Returns a const reference to the first element.
00258     ///\return A const reference to the first element.
00259     //*********************************************************************
00260     const_reference front() const
00261     {
00262       return const_reference(base_t::front());
00263     }
00264 
00265     //*********************************************************************
00266     /// Returns a reference to the last element.
00267     ///\return A reference to the last element.
00268     //*********************************************************************
00269     reference back()
00270     {
00271       return reference(base_t::back());
00272     }
00273 
00274     //*********************************************************************
00275     /// Returns a const reference to the last element.
00276     ///\return A const reference to the last element.
00277     //*********************************************************************
00278     const_reference back() const
00279     {
00280       return const_reference(base_t::back());
00281     }
00282 
00283     //*********************************************************************
00284     /// Returns a pointer to the beginning of the vector data.
00285     ///\return A pointer to the beginning of the vector data.
00286     //*********************************************************************
00287     pointer data()
00288     {
00289       return pointer(base_t::data());
00290     }
00291 
00292     //*********************************************************************
00293     /// Returns a const pointer to the beginning of the vector data.
00294     ///\return A const pointer to the beginning of the vector data.
00295     //*********************************************************************
00296     const_pointer data() const
00297     {
00298       return const_pointer(base_t::data());
00299     }
00300 
00301     //*********************************************************************
00302     /// Assigns values to the vector.
00303     /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
00304     /// If asserts or exceptions are enabled, emits vector_iterator if the iterators are reversed.
00305     ///\param first The iterator to the first element.
00306     ///\param last  The iterator to the last element + 1.
00307     //*********************************************************************
00308     template <typename TIterator>
00309     void assign(TIterator first, TIterator last)
00310     {
00311       base_t::initialise();
00312 
00313       while (first != last)
00314       {
00315         *p_end++ = (void*)*first++;
00316       }
00317     }
00318 
00319     //*********************************************************************
00320     /// Assigns values to the vector.
00321     /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
00322     ///\param n     The number of elements to add.
00323     ///\param value The value to insert for each element.
00324     //*********************************************************************
00325     void assign(size_t n, parameter_t value)
00326     {
00327       base_t::assign(n, value);
00328     }
00329 
00330     //*************************************************************************
00331     /// Clears the vector.
00332     //*************************************************************************
00333     void clear()
00334     {
00335       base_t::clear();
00336     }
00337 
00338     //*************************************************************************
00339     /// Increases the size of the vector by one, but does not initialise the new element.
00340     /// If asserts or exceptions are enabled, throws a vector_full if the vector is already full.
00341     //*************************************************************************
00342     void push_back()
00343     {
00344       base_t::push_back();
00345     }
00346 
00347     //*********************************************************************
00348     /// Inserts a value at the end of the vector.
00349     /// If asserts or exceptions are enabled, emits vector_full if the vector is already full.
00350     ///\param value The value to add.
00351     //*********************************************************************
00352     void push_back(parameter_t value)
00353     {
00354       base_t::push_back(value);
00355     }
00356 
00357     //*************************************************************************
00358     /// Removes an element from the end of the vector.
00359     /// Does nothing if the vector is empty.
00360     //*************************************************************************
00361     void pop_back()
00362     {
00363       base_t::pop_back();
00364     }
00365 
00366     //*********************************************************************
00367     /// Inserts a value to the vector.
00368     /// If asserts or exceptions are enabled, emits vector_full if the vector is already full.
00369     ///\param position The position to insert before.
00370     ///\param value    The value to insert.
00371     //*********************************************************************
00372     iterator insert(iterator position, parameter_t value)
00373     {
00374       return iterator(base_t::insert(base_t::iterator(position), value));
00375     }
00376 
00377     //*********************************************************************
00378     /// Inserts 'n' values to the vector.
00379     /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
00380     ///\param position The position to insert before.
00381     ///\param n        The number of elements to add.
00382     ///\param value    The value to insert.
00383     //*********************************************************************
00384     void insert(iterator position, size_t n, parameter_t value)
00385     {
00386       base_t::insert(base_t::iterator(position), n, value);
00387     }
00388 
00389     //*********************************************************************
00390     /// Inserts a range of values to the vector.
00391     /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
00392     ///\param position The position to insert before.
00393     ///\param first    The first element to add.
00394     ///\param last     The last + 1 element to add.
00395     //*********************************************************************
00396     template <class TIterator>
00397     void insert(iterator position, TIterator first, TIterator last)
00398     {
00399       base_t::insert(base_t::iterator(position), first, last);
00400     }
00401 
00402     //*********************************************************************
00403     /// Erases an element.
00404     ///\param i_element Iterator to the element.
00405     ///\return An iterator pointing to the element that followed the erased element.
00406     //*********************************************************************
00407     iterator erase(iterator i_element)
00408     {
00409       return iterator(base_t::erase(base_t::iterator(i_element)));
00410     }
00411 
00412     //*********************************************************************
00413     /// Erases a range of elements.
00414     /// The range includes all the elements between first and last, including the
00415     /// element pointed by first, but not the one pointed by last.
00416     ///\param first Iterator to the first element.
00417     ///\param last  Iterator to the last element.
00418     ///\return An iterator pointing to the element that followed the erased element.
00419     //*********************************************************************
00420     iterator erase(iterator first, iterator last)
00421     {
00422       return iterator(base_t::erase(base_t::iterator(first), base_t::iterator(last)));
00423     }
00424 
00425     //*************************************************************************
00426     /// Assignment operator.
00427     //*************************************************************************
00428     ivector& operator = (const ivector& rhs)
00429     {
00430       if (&rhs != this)
00431       {
00432         assign(rhs.cbegin(), rhs.cend());
00433       }
00434 
00435       return *this;
00436     }
00437 
00438   protected:
00439 
00440     //*********************************************************************
00441     /// Constructor.
00442     //*********************************************************************
00443     ivector(T** p_buffer_, size_t MAX_SIZE_)
00444       : pvoidvector(reinterpret_cast<void**>(p_buffer_), MAX_SIZE_)
00445     {
00446     }
00447   };
00448 
00449   //***************************************************************************
00450   /// Equal operator.
00451   ///\param lhs Reference to the first vector.
00452   ///\param rhs Reference to the second vector.
00453   ///\return <b>true</b> if the arrays are equal, otherwise <b>false</b>
00454   ///\ingroup vector
00455   //***************************************************************************
00456   template <typename T>
00457   bool operator ==(const etl::ivector<T*>& lhs, const etl::ivector<T*>& rhs)
00458   {
00459     return pvoidvector_equal(lhs, rhs);
00460   }
00461 
00462   //***************************************************************************
00463   /// Not equal operator.
00464   ///\param lhs Reference to the first vector.
00465   ///\param rhs Reference to the second vector.
00466   ///\return <b>true</b> if the arrays are not equal, otherwise <b>false</b>
00467   ///\ingroup vector
00468   //***************************************************************************
00469   template <typename T>
00470   bool operator !=(const etl::ivector<T*>& lhs, const etl::ivector<T*>& rhs)
00471   {
00472     return pvoidvector_not_equal(lhs, rhs);
00473   }
00474 
00475   //***************************************************************************
00476   /// Less than operator.
00477   ///\param lhs Reference to the first vector.
00478   ///\param rhs Reference to the second vector.
00479   ///\return <b>true</b> if the first vector is lexicographically less than the second, otherwise <b>false</b>
00480   ///\ingroup vector
00481   //***************************************************************************
00482   template <typename T>
00483   bool operator <(const etl::ivector<T*>& lhs, const etl::ivector<T*>& rhs)
00484   {
00485     return pvoidvector_less_than(lhs, rhs);
00486   }
00487 
00488   //***************************************************************************
00489   /// Greater than operator.
00490   ///\param lhs Reference to the first vector.
00491   ///\param rhs Reference to the second vector.
00492   ///\return <b>true</b> if the first vector is lexicographically greater than the second, otherwise <b>false</b>
00493   ///\ingroup vector
00494   //***************************************************************************
00495   template <typename T>
00496   bool operator >(const etl::ivector<T*>& lhs, const etl::ivector<T*>& rhs)
00497   {
00498     return pvoidvector_greater_than(lhs, rhs);
00499   }
00500 
00501   //***************************************************************************
00502   /// Less than or equal operator.
00503   ///\param lhs Reference to the first vector.
00504   ///\param rhs Reference to the second vector.
00505   ///\return <b>true</b> if the first vector is lexigraphically less than or equal to the second, otherwise <b>false</b>
00506   ///\ingroup vector
00507   //***************************************************************************
00508   template <typename T>
00509   bool operator <=(const etl::ivector<T*>& lhs, const etl::ivector<T*>& rhs)
00510   {
00511     return pvoidvector_less_than_equal(lhs, rhs);
00512   }
00513 
00514   //***************************************************************************
00515   /// Greater than or equal operator.
00516   ///\param lhs Reference to the first vector.
00517   ///\param rhs Reference to the second vector.
00518   ///\return <b>true</b> if the first vector is lexigraphically greater than or equal to the second, otherwise <b>false</b>
00519   ///\ingroup vector
00520   //***************************************************************************
00521   template <typename T>
00522   bool operator >=(const etl::ivector<T*>& lhs, const etl::ivector<T*>& rhs)
00523   {
00524     return pvoidvector_greater_than_equal(lhs, rhs);
00525   }
00526 
00527   //***************************************************************************
00528   // Helper functions
00529   //***************************************************************************
00530   inline bool pvoidvector_equal(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
00531   {
00532     return operator ==(lhs, rhs);
00533   }
00534 
00535   inline bool pvoidvector_not_equal(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
00536   {
00537     return operator !=(lhs, rhs);
00538   }
00539 
00540   inline bool pvoidvector_less_than(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
00541   {
00542     return operator <(lhs, rhs);
00543   }
00544 
00545   inline bool pvoidvector_greater_than(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
00546   {
00547     return operator >(lhs, rhs);
00548   }
00549 
00550   inline bool pvoidvector_less_than_equal(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
00551   {
00552     return operator <=(lhs, rhs);
00553   }
00554 
00555   inline bool pvoidvector_greater_than_equal(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
00556   {
00557     return operator >=(lhs, rhs);
00558   }
00559 }
00560 
00561 #endif
00562