loisduplain

vector.hpp

Feb 23rd, 2022
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.62 KB | None | 0 0
  1. #ifndef VECTOR_HPP
  2. # define VECTOR_HPP
  3. #include <iostream>
  4. #include <iterator>
  5. #include <stdexcept>
  6. #include <memory>
  7. #include <algorithm>
  8. #include "../Iterators/randomAccessIterator.hpp"
  9. #include "../Iterators/randomAccessConstIterator.hpp"
  10. #include "../Iterators/reverseIterator.hpp"
  11. #include "../Iterators/reverseConstIterator.hpp"
  12.  
  13. namespace ft
  14. {
  15.  
  16.     /**
  17.      * Vectors are sequence containers representing dynamic size arrays.
  18.      *
  19.      * @tparam T - type of objects who can be stored in this vector
  20.      * @tparam A - allocator template of type "T" in standard library
  21.      */
  22.  
  23.     template <class T, class A = std::allocator<T> >
  24.     class vector
  25.     {
  26.  
  27.         public:
  28.  
  29.     /**
  30.      *
  31.      * Typedef
  32.      *
  33.      */
  34.  
  35.             typedef A allocator_type;
  36.             typedef typename A::value_type value_type;
  37.             typedef typename A::reference reference;
  38.             typedef typename A::const_reference const_reference;
  39.             typedef typename A::pointer pointer;
  40.             typedef typename A::difference_type difference_type;
  41.             typedef typename A::size_type size_type;
  42.  
  43.             typedef reverseIterator<T, A> reverse_iterator;
  44.             typedef reverseConstIterator<T, A> const_reverse_iterator;
  45.             typedef iterator<T, A> iterator;
  46.             typedef const_iterator<T, A> const_iterator;
  47.  
  48.     /**
  49.      *
  50.      * Constructor
  51.      *
  52.      */
  53.  
  54.         /**
  55.          * Create new empty vector with a capacity of 1.
  56.          *
  57.          * @param alloc - Allocator's address, set to default allocator
  58.          */
  59.             explicit vector(const allocator_type& alloc = allocator_type()): _alloc(alloc)
  60.             {
  61.                 _capacity = 1;
  62.                 _size = 0;
  63.                 _datas = _alloc.allocate(_capacity);
  64.             }
  65.  
  66.         /**
  67.          * Create new vector with a capacity of n, completed by value.
  68.          *
  69.          * @param n - capacity of new vector
  70.          * @param value - value of elements in new vector
  71.          * @param alloc - Allocator's address, set to default allocator
  72.          */
  73.             explicit vector(size_type n, const value_type& value = value_type(), const allocator_type& alloc = allocator_type()): _alloc(alloc)
  74.             {
  75.                 _capacity = n;
  76.                 _size = n;
  77.                 _datas = _alloc.allocate(_capacity);
  78.                 for (size_type i = 0; i < _size; i++)
  79.                     _alloc.construct(&_datas[i], value);
  80.             }
  81.  
  82.         /**
  83.          * Create new vector with value between first and last,
  84.          * the size of vector is the number of objects between first to last.
  85.          * This function use an input iterator.
  86.          *
  87.          * @param first - starting iterator
  88.          * @param last - ending iterator
  89.          * @param alloc - Allocator's address, set to default allocator
  90.          */
  91.             template <class InputIterator>
  92.             vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()): _alloc(alloc)
  93.             {
  94.                 _size = 0;
  95.                 _capacity = 1;
  96.                 _datas = _alloc.allocate(_capacity);
  97.                 for ( ; first != last; first++)
  98.                     push_back(*first);
  99.             }
  100.  
  101.         /**
  102.          * Create new vector with all attributes of instance reference.
  103.          *
  104.          * @param instance - reference of an other vector
  105.          */
  106.             vector(const vector &instance)
  107.             {
  108.                 if (this != instance)
  109.                 {
  110.                     _capacity = instance._capacity;
  111.                     _alloc = instance._alloc;
  112.                     _size = instance._size;
  113.                     _datas = instance._datas;
  114.                 }
  115.             }
  116.  
  117.         /**
  118.          * Assign all attributes of a vector to this vector object.
  119.          *
  120.          * @param rhs - reference to an other vector
  121.          * @return reference of this
  122.          */
  123.             vector &operator=(vector const &rhs)
  124.             {
  125.                 if (this != rhs)
  126.                 {
  127.                     _capacity = rhs._capacity;
  128.                     _alloc = rhs._alloc;
  129.                     _size = rhs._size;
  130.                     _datas = rhs._datas;
  131.                 }
  132.                 return *this;
  133.             }
  134.  
  135.         /**
  136.          * Destroy vector and clear all elements to deallocate memory.
  137.          */
  138.             ~vector(void)
  139.             {
  140.                 clear();
  141.             }
  142.  
  143.     /**
  144.      *
  145.      *  Iterators
  146.     *
  147.     */
  148.  
  149.         /**
  150.          * Get the first vector array element.
  151.          *
  152.          * @return address of the first vector array element
  153.          */
  154.             iterator begin() {
  155.                 return iterator(&_datas[0]);
  156.             }
  157.  
  158.         /**
  159.          * Get the first constant vector array element.
  160.          *
  161.          * @return address of the first vector array element
  162.          */
  163.             const_iterator begin() const {
  164.                 return const_iterator(&_datas[0]);
  165.             }
  166.  
  167.         /**
  168.          * Get the last vector array element.
  169.          *
  170.          * @return address of the last vector array element
  171.          */
  172.             iterator end() {
  173.                 return iterator(&_datas[_size]);
  174.             }
  175.  
  176.         /**
  177.          * Get the last constant vector array element.
  178.          *
  179.          * @return address of the last vector array element
  180.          */
  181.             const_iterator end() const {
  182.                 return const_iterator(&_datas[_size]);
  183.             }
  184.  
  185.         /**
  186.          * Get the last vector array element.
  187.          *
  188.          * @return address of the last vector array element
  189.          */
  190.             reverse_iterator rbegin() {
  191.                 return reverse_iterator(&_datas[_size]);
  192.             }
  193.  
  194.         /**
  195.          * Get the last constant vector array element.
  196.          *
  197.          * @return address of the last vector array element
  198.          */
  199.             const_reverse_iterator rbegin() const {
  200.                 return const_reverse_iterator(&_datas[_size]);
  201.             }
  202.  
  203.         /**
  204.          * Get the first vector array element.
  205.          *
  206.          * @return address of the first vector array element
  207.          */
  208.             reverse_iterator rend() {
  209.                 return reverse_iterator(&_datas[0]);
  210.             }
  211.  
  212.         /**
  213.          * Get the first constant vector array element.
  214.          *
  215.          * @return address of the first vector array element
  216.          */
  217.             const_reverse_iterator rend() const {
  218.                 return const_reverse_iterator(&_datas[0]);
  219.             }
  220.  
  221.     /**
  222.      *
  223.      * Capacity
  224.      *
  225.      */
  226.  
  227.         /**
  228.          * Get the size of vector.
  229.          *
  230.          * @return the size of vector
  231.          */
  232.             size_type size() const
  233.             {
  234.                 return _size;
  235.             }
  236.  
  237.         /**
  238.          * Get memory max size can be allocated. This size change by the type "size_type".
  239.          *
  240.          * @return memory max size can be allocated
  241.          */
  242.             size_type max_size() const
  243.             {
  244.                 return  std::numeric_limits<size_type>::max() / sizeof(value_type);
  245.             }
  246.  
  247.         /**
  248.          * Change the size of datas array, delete datas if the new size is smaller than before.
  249.          * If the new size is bigger, fill the new empty value with NULL if the parameter "val" is not used.
  250.          * If the new size is bigger, fill the new empty values with "val".
  251.          *
  252.          * @param n - the new size of array
  253.          * @param val - value to fill (can be empty)
  254.          * @return void
  255.          */
  256.             void resize (size_type n, value_type val = value_type())
  257.             {
  258.                 if (_capacity < n)
  259.                 {
  260.                     // Allocate new capacity
  261.                     value_type *tmp = _datas;
  262.                     _datas = _alloc.allocate(n);
  263.  
  264.                     // Fill new array with ancient values
  265.                     for (size_type i = 0; i < _size; i++)
  266.                     {
  267.                         _alloc.construct(&_datas[i], tmp[i]);
  268.                         _alloc.destroy(&tmp[i]);
  269.                     }
  270.                     // Add new values in array
  271.                     for (size_type i = _size; i < n; i++)
  272.                         _alloc.construct(&_datas[i], val);
  273.  
  274.                     _alloc.deallocate(tmp, _capacity);
  275.                     _capacity = n;
  276.                     _size = n;
  277.                 }
  278.                 else if (_size > n)
  279.                 {
  280.                     // Allocate capacity in new array
  281.                     value_type *tmp = _datas;
  282.                     _datas = _alloc.allocate(_capacity);
  283.  
  284.                     // Fill new array with "n" ancient values
  285.                     for (size_type i = 0; i < n; i++)
  286.                     {
  287.                         _alloc.construct(&_datas[i], tmp[i]);
  288.                         _alloc.destroy(&tmp[i]);
  289.                     }
  290.  
  291.                     for (size_type i = n; i < _size; i++)
  292.                         _alloc.destroy(&tmp[i]);
  293.  
  294.                     _alloc.deallocate(tmp, _capacity);
  295.                     _size = n;
  296.                 }
  297.                 else if (_size < n)
  298.                 {
  299.                     // Add new values in array
  300.                     for (size_type i = _size; i < n; i++)
  301.                         _alloc.construct(&_datas[i], val);
  302.                     _size = n;
  303.                 }
  304.             }
  305.  
  306.         /**
  307.          * Get capacity of vector, capacity is the maximum size allocated for the datas array.
  308.          *
  309.          * @return capacity of vector array
  310.          */
  311.             size_type capacity() const
  312.             {
  313.                 return _capacity;
  314.             }
  315.  
  316.         /**
  317.          * Check if array is empty
  318.          *
  319.          * @return true if array is empty
  320.          */
  321.             bool empty() const
  322.             {
  323.                 if (_size == 0)
  324.                     return true;
  325.                 return false;
  326.             }
  327.  
  328.         /**
  329.          * Allocate datas array for a new capacity. The new capacity needs to be bigger than before.
  330.          *
  331.          * @param n - new capacity
  332.          * @return void
  333.          */
  334.             void reserve (size_type n)
  335.             {
  336.                 if (_capacity < n || _datas == NULL)
  337.                 {
  338.                     // Allocate new capacity
  339.                     pointer tmp = _datas;
  340.                     _datas = _alloc.allocate(n);
  341.  
  342.                     // Fill new array with ancient values
  343.                     for (size_type i = 0; i < _size; i++)
  344.                     {
  345.                         _alloc.construct(&_datas[i], tmp[i]);
  346.                         _alloc.destroy(&tmp[i]);
  347.                     }
  348.  
  349.                     if (_datas != NULL)
  350.                         _alloc.deallocate(tmp, _capacity);
  351.                     _capacity = n;
  352.                 }
  353.             }
  354.  
  355.     /**
  356.      *
  357.      * Element access
  358.      *
  359.      */
  360.  
  361.         /**
  362.          * Get reference to the element at index "n" in array.
  363.          *
  364.          * @param n - index of element
  365.          * @return reference to the value
  366.          */
  367.             reference operator[](size_type n)
  368.             {
  369.                 return _datas[n];
  370.             }
  371.  
  372.         /**
  373.          * Get reference to the element at index "n" in array as constant.
  374.          *
  375.          * @param n - index of element
  376.          * @return reference to the value
  377.          */
  378.             const_reference operator[](size_type n) const
  379.             {
  380.                 return _datas[n];
  381.             }
  382.  
  383.  
  384.         /**
  385.          * Get reference to the element at index "n" in array.
  386.          *
  387.          * @param n - index of element
  388.          * @throw std::out_of_range - throw if "n" is bigger than size
  389.          * @return reference to the value
  390.          */
  391.             reference at (size_type n)
  392.             {
  393.                 if (_size < n)
  394.                     throw std::out_of_range("vector");
  395.                 return _datas[n];
  396.             }
  397.  
  398.         /**
  399.          * Get reference to the element at index "n" in array as constant.
  400.          *
  401.          * @param n - index of element
  402.          * @throw std::out_of_range - throw if "n" is bigger than size
  403.          * @return reference to the value
  404.          */
  405.             const_reference at (size_type n) const
  406.             {
  407.                 if (_size < n)
  408.                     throw std::out_of_range("vector");
  409.                 return _datas[n];
  410.             }
  411.  
  412.         /**
  413.          * Get reference of the first element in array.
  414.          *
  415.          * @return reference of the last value in array
  416.          */
  417.             reference front ()
  418.             {
  419.                 return _datas[0];
  420.             }
  421.  
  422.         /**
  423.          * Get reference of the first element in array as constant.
  424.          *
  425.          * @return reference of the last value in array
  426.          */
  427.             const_reference front () const
  428.             {
  429.                 return _datas[0];
  430.             }
  431.  
  432.         /**
  433.          * Get reference of the last element in array.
  434.          *
  435.          * @return reference of the last value in array
  436.          */
  437.             reference back ()
  438.             {
  439.                 return _datas[_size - 1];
  440.             }
  441.  
  442.         /**
  443.          * Get reference of the last element in array as contant.
  444.          *
  445.          * @return reference of the last value in array
  446.          */
  447.             const_reference back () const
  448.             {
  449.                 return _datas[_size - 1];
  450.             }
  451.  
  452.     /**
  453.      *
  454.      * Modifiers
  455.      *
  456.      */
  457.  
  458.         /**
  459.          * Clear vector datas array and create new one with the values between "first" and "last".
  460.          * Capacity can be only increased.
  461.          * This function use an input iterator.
  462.          *
  463.          * @param first - starting iterator
  464.          * @param last - ending iterator
  465.          * @return void
  466.          */
  467.             template <class InputIterator>
  468.             void assign (InputIterator first, InputIterator last)
  469.             {
  470.                 clear();
  471.                 check_capacity();
  472.                 for ( ; first != last; first++)
  473.                     push_back(*first);
  474.             }
  475.        
  476.         /**
  477.          * Clear vector datas array and create new one with size "n" and fill it with value "val".
  478.          * Capacity can be only increased.
  479.          *
  480.          * @param n - new size of array
  481.          * @param val - value filled
  482.          * @return void
  483.          */
  484.             void assign (size_type n, const value_type& val)
  485.             {
  486.                 clear();
  487.                 check_capacity(n);
  488.                 for (size_type i = 0; i < n; i++)
  489.                     push_back(val);
  490.             }
  491.  
  492.         /**
  493.          * Add new element "val" in array, if the size equal capacity,
  494.          * increase capacity by multiplicating this actual capacity by 2.
  495.          *
  496.          * @param val - value to add
  497.          * @return void
  498.          */
  499.             void push_back (const value_type& val)
  500.             {
  501.                 check_capacity();
  502.                 _alloc.construct(&_datas[_size], val);
  503.                 _size++;
  504.             }
  505.  
  506.         /**
  507.          * Delete the last element in array.
  508.          *
  509.          * @return void
  510.          */
  511.             void pop_back ()
  512.             {
  513.                 _alloc.destroy(&_datas[_size - 1]);
  514.                 _size--;
  515.             }
  516.  
  517.         /**
  518.          * Insert into array the value "val" in "position". "position" is an iterator of its own array.
  519.          *
  520.          * @param position - iterator of its own array, position of the new added value
  521.          * @param val - value to add
  522.          * @return iterator of the first element of array
  523.          */
  524.             iterator insert (iterator position, const value_type& val)
  525.             {
  526.                 size_type index = position - begin();
  527.                 if (index <= _capacity)
  528.                 {
  529.                     check_capacity();
  530.                     for (size_type i = _size; i >= 0; i--)
  531.                     {
  532.                         // Move value in actual index to the next index and put the new one at the index
  533.                         if (i == index)
  534.                         {
  535.                             _alloc.construct(&_datas[i + 1], _datas[i]);
  536.                             _alloc.destroy(&_datas[i]);
  537.                             _alloc.construct(&_datas[i], val);
  538.                             break;
  539.                         }
  540.                         // Move value in actual index to the next index to have one free index when will be on the right index
  541.                         else
  542.                         {
  543.                             _alloc.construct(&_datas[i + 1], _datas[i]);
  544.                             _alloc.destroy(&_datas[i]);
  545.                         }
  546.                     }
  547.                     _size++;
  548.                 }
  549.                 return _datas;
  550.             }
  551.  
  552.         /**
  553.          * Insert in array "n" elements of the value "val" in "position", "position" is an iterator of its own array.
  554.          *
  555.          * @param position - iterator of its own array, position of the new added value
  556.          * @param n - number of added elements
  557.          * @param val - value to add
  558.          * @return void
  559.          */
  560.             void insert (iterator position, size_type n, const value_type& val)
  561.             {
  562.                 size_type index = position - begin();
  563.                 if (index <= _capacity)
  564.                 {
  565.                     if (_capacity - _size < n)
  566.                         reserve(_capacity + n);
  567.                     else
  568.                         check_capacity();
  569.  
  570.                     for (size_type i = _size; i >= 0; i--)
  571.                     {
  572.                         // Move value in actual index to the next index + "n" and put the new one at the index
  573.                         if (i == index)
  574.                         {
  575.                             _alloc.construct(&_datas[i + n], _datas[i]);
  576.                             _alloc.destroy(&_datas[i]);
  577.                             for (size_type j = 0; j < n; j++)
  578.                                 _alloc.construct(&_datas[i + j], val);
  579.                             break;
  580.                         }
  581.                         // Move value in actual index to the next index + "n" to have one free index when will be on the right index
  582.                         else
  583.                         {
  584.                             _alloc.construct(&_datas[i + n], _datas[i]);
  585.                             _alloc.destroy(&_datas[i]);
  586.                         }
  587.                     }
  588.                     _size += n;
  589.                 }
  590.             }
  591.  
  592.  
  593.         /**
  594.          * Insert in array, all elements pointed between "first" and "last" in "position", "position" is an iterator of its own array.
  595.          * This function use an input iterator.
  596.          *
  597.          * @param position - iterator of its own array, position of the new added value
  598.          * @param first - starting iterator
  599.          * @param last - endding iterator
  600.          * @return void
  601.          */
  602.             template <class InputIterator>
  603.             void insert (iterator position, InputIterator first, InputIterator last)
  604.             {
  605.                 size_type index = position - begin();
  606.                 if (index <= _capacity)
  607.                 {
  608.                     // Create new array
  609.                     value_type *tmp = _datas;
  610.                     size_type   size_tmp = _size;
  611.                     size_type   capacity_tmp = _capacity;
  612.                     _datas = _alloc.allocate(_capacity);
  613.  
  614.                     // Fill new array with ancient values to position
  615.                     for (size_type i = 0; i < index; i++)
  616.                     {
  617.                         _alloc.construct(&_datas[i], tmp[i]);
  618.                         _alloc.destroy(&tmp[i]);
  619.                     }
  620.  
  621.                     _size = index;
  622.  
  623.                     // Add values between "first" and "last"
  624.                     for ( ; first != last; first++)
  625.                         push_back(*first);
  626.  
  627.                     // Fill new array with ancient values
  628.                     for (size_type i = index; i < size_tmp; i++)
  629.                     {
  630.                         push_back(tmp[i]);
  631.                         _alloc.destroy(&tmp[i]);
  632.                     }
  633.                     _alloc.deallocate(tmp, capacity_tmp);
  634.                 }
  635.             }
  636.  
  637.         /**
  638.          * Delete in array the element at "position".
  639.          *
  640.          * @param position - iterator of its own array, position of deleted value
  641.          * @return iterator of the first element of array
  642.          */
  643.             iterator erase (iterator position)
  644.             {
  645.                 size_type index = position - begin();
  646.  
  647.                 // Destroy the element in index
  648.                 _alloc.destroy(&_datas[index]);
  649.  
  650.                 // Replace all next elements to fill correctly the array
  651.                 for (size_type i = index; i < _size; i++)
  652.                 {
  653.                     _alloc.construct(&_datas[i], _datas[i + 1]);
  654.                     _alloc.destroy(&_datas[i + 1]);
  655.                 }
  656.  
  657.                 _alloc.destroy(&_datas[_size - 1]);
  658.                 _size--;
  659.                 return _datas;
  660.             }
  661.  
  662.         /**
  663.          * Delete all elements pointed between "first" and "last".
  664.          * "first" and "last" are iterators of its own array.
  665.          *
  666.          * @param first - starting iterator
  667.          * @param last - endding iterator
  668.          * @return iterator of the first element of array
  669.          */
  670.             iterator erase (iterator first, iterator last)
  671.             {
  672.                 size_type index;
  673.                 size_type index_tmp;
  674.                 iterator tmp = first;
  675.                 // Destroy the elements from "first" to "last"
  676.                 for ( ; first != last; first++)
  677.                 {
  678.                     index = first - begin();
  679.                     _alloc.destroy(&_datas[index]);
  680.                     _size--;
  681.                 }
  682.                 // Replace all elements to fill correctly the array
  683.                 for ( ; tmp != end(); tmp++)
  684.                 {
  685.                     index = last - begin();
  686.                     index_tmp = tmp - begin();
  687.                     _alloc.construct(&_datas[index_tmp], _datas[index]);
  688.                     _alloc.destroy(&_datas[index]);
  689.                     last++;
  690.                 }
  691.                 return _datas;
  692.             }
  693.  
  694.         /**
  695.          * Swap all informations of himself and reference of vector.
  696.          *
  697.          * @param x - address of vector we want to swap
  698.          * @return void
  699.          */
  700.             void swap (vector &x)
  701.             {
  702.                 std::swap(_capacity, x._capacity);
  703.                 std::swap(_size, x._size);
  704.                 std::swap(_datas, x._datas);
  705.                 std::swap(_alloc, x._alloc);
  706.             }
  707.  
  708.         /**
  709.          * Clear all datas in array and deallocate memory.
  710.          *
  711.          * @return void
  712.          */
  713.             void clear ()
  714.             {
  715.                 for (size_type i = 0; i < _size; i++)
  716.                     _alloc.destroy(&_datas[i]);
  717.                
  718.                 _alloc.deallocate(_datas, _capacity);
  719.                 _datas = NULL;
  720.                 _size = 0;
  721.             }
  722.  
  723.     /**
  724.      *
  725.      * Allocator
  726.      *
  727.      */
  728.  
  729.         /**
  730.          * Get allocator of vector.
  731.          *
  732.          * @return allocator reference
  733.          */
  734.             allocator_type get_allocator() const
  735.             {
  736.                 return _alloc;
  737.             }
  738.  
  739.     /**
  740.      *
  741.      * Utils
  742.      *
  743.      */
  744.  
  745.         /**
  746.          * Check if size of array are equal to capacity, if its true, increase capacity by multiplicate the actual value of capacity by two.
  747.          * If array is empty, allocate capacity
  748.          *
  749.          * @return void
  750.          */
  751.             void check_capacity ()
  752.             {
  753.                 if (_size == _capacity)
  754.                     reserve(_capacity * 2);
  755.                 if (_datas == NULL)
  756.                     reserve(_capacity);
  757.             }
  758.  
  759.         /**
  760.          * Check if capacity are smaller than "n", if its true, allocate "n" capacity.
  761.          * Check if size of array are equal to capacity, if its true, increase capacity by multiplicate the actual value of capacity by two.
  762.          * If array is empty, allocate capacity
  763.          *
  764.          * @param n - capacity value to check
  765.          * @return void
  766.          */
  767.             void check_capacity (size_type n)
  768.             {
  769.                 if (_capacity < n)
  770.                     reserve(n);
  771.                 else if (_size == _capacity)
  772.                     reserve(_capacity * 2);
  773.                 else if (_datas == NULL)
  774.                     reserve(_capacity);
  775.             }
  776.  
  777.         private:
  778.             value_type      *_datas;
  779.             allocator_type  _alloc;
  780.             size_type       _size;
  781.             size_type       _capacity;
  782.  
  783.     };
  784.  
  785.     /**
  786.      *
  787.      * Non-member function overload
  788.      *
  789.      */
  790.  
  791.     /**
  792.      * Overload of "==" operator
  793.      *
  794.      * @param lhs - address of vector
  795.      * @param rhs - address of vector
  796.      * @return true if the two vectors are equal
  797.      */
  798.     template <class T, class A>
  799.     bool operator== (const vector<T,A>& lhs, const vector<T,A>& rhs)
  800.     {
  801.         if (lhs.max_size() == rhs.max_size() && lhs.get_allocator() == rhs.get_allocator() && lhs.size() == rhs.size() && lhs.capacity() == rhs.capacity())
  802.         {
  803.             for (size_type i = 0; i < lhs.size(); i++)
  804.             {
  805.                 if (lhs.at(i) != rhs.at(i))
  806.                     return false;
  807.             }
  808.             return true;
  809.         }
  810.         return false;
  811.     }
  812.  
  813.     /**
  814.      * Overload of "!=" operator
  815.      *
  816.      * @param lhs - address of vector
  817.      * @param rhs - address of vector
  818.      * @return true if the two vectors are not equal
  819.      */
  820.     template <class T, class A>
  821.     bool operator!= (const vector<T,A>& lhs, const vector<T,A>& rhs)
  822.     {
  823.         if (lhs.max_size() == rhs.max_size() && lhs.get_allocator() == rhs.get_allocator() && lhs.size() == rhs.size() && lhs.capacity() == rhs.capacity())
  824.         {
  825.             for (size_type i = 0; i < lhs.size(); i++)
  826.             {
  827.                 if (lhs.at(i) != rhs.at(i))
  828.                     return true;
  829.             }
  830.             return false;
  831.         }
  832.         return true;
  833.     }
  834.  
  835.     /**
  836.      * Overload of "<" operator.
  837.      *
  838.      * @param lhs - address of vector
  839.      * @param rhs - address of vector
  840.      * @return true if "lhs" are smaller than "rhs"
  841.      */
  842.     template <class T, class A>
  843.     bool operator<  (const vector<T,A>& lhs, const vector<T,A>& rhs)
  844.     {
  845.         return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  846.     }
  847.  
  848.     /**
  849.      * Overload of "<=" operator.
  850.      *
  851.      * @param lhs - address of vector
  852.      * @param rhs - address of vector
  853.      * @return true if "lhs" are smaller than "rhs" or equal
  854.      */
  855.     template <class T, class A>
  856.     bool operator<= (const vector<T,A>& lhs, const vector<T,A>& rhs)
  857.     {
  858.         return (lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()) || lhs == rhs);
  859.     }
  860.  
  861.     /**
  862.      * Overload of ">" operator.
  863.      *
  864.      * @param lhs - address of vector
  865.      * @param rhs - address of vector
  866.      * @return true if "lhs" are bigger than "rhs"
  867.      */
  868.     template <class T, class A>
  869.     bool operator>  (const vector<T,A>& lhs, const vector<T,A>& rhs)
  870.     {
  871.         return (!lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()) && lhs != rhs);
  872.     }
  873.  
  874.     /**
  875.      * Overload of ">=" operator.
  876.      *
  877.      * @param lhs - address of vector
  878.      * @param rhs - address of vector
  879.      * @return true if "lhs" are bigger than "rhs" or equal
  880.      */
  881.     template <class T, class A>
  882.     bool operator>= (const vector<T,A>& lhs, const vector<T,A>& rhs)
  883.     {
  884.         return (!lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()) || lhs == rhs);
  885.     }
  886.  
  887.     /**
  888.      * Overload of "swap" function.
  889.      * Swap all informations of himself and reference of vector.
  890.      *
  891.      * @param lhs - address of vector
  892.      * @param rhs - address of vector
  893.      * @return void
  894.      */
  895.     template <class T, class A>
  896.     void swap (vector<T,A>& x, vector<T,A>& y)
  897.     {
  898.         x.swap(y);
  899.     }
  900. }
  901. #endif
Advertisement
Add Comment
Please, Sign In to add comment