-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_access_iterator.hpp
More file actions
82 lines (75 loc) · 4.71 KB
/
random_access_iterator.hpp
File metadata and controls
82 lines (75 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef RANDOM_ACCESS_ITERATOR_HPP
#define RANDOM_ACCESS_ITERATOR_HPP
#include "iterator.hpp"
namespace ft {
template <typename T>
class random_access_iterator : public ft::iterator<ft::random_access_iterator_tag, T> {
public:
typedef typename ft::iterator<random_access_iterator_tag, T>::iterator_category iterator_category;
typedef typename ft::iterator<random_access_iterator_tag, T>::pointer pointer;
typedef typename ft::iterator<random_access_iterator_tag, T>::reference reference;
typedef typename ft::iterator<random_access_iterator_tag, T>::value_type value_type;
typedef typename ft::iterator<random_access_iterator_tag, T>::difference_type difference_type;
private:
pointer _ptr;
public:
random_access_iterator() : _ptr(NULL) {}
random_access_iterator(const random_access_iterator &other) : _ptr(other._ptr) {}
template<typename Iter>
random_access_iterator(const random_access_iterator<Iter> &other) : _ptr(other.base()) {}
random_access_iterator(pointer ptr) : _ptr(ptr) {}
virtual ~random_access_iterator() {}
random_access_iterator &operator=(const random_access_iterator &other) {
if (this != &other)
_ptr = other._ptr;
return *this;
}
//operator overloading
pointer base() const { return _ptr; }
reference operator*(){ return *_ptr; }
const reference operator*() const { return *_ptr; }
random_access_iterator operator+(difference_type n) const { return random_access_iterator(_ptr + n); }
random_access_iterator operator++(int) { return random_access_iterator(_ptr++); }
random_access_iterator &operator++() { ++_ptr; return *this; }
random_access_iterator operator+=(difference_type n) { _ptr += n; return *this; }
random_access_iterator operator-(difference_type n) const { return random_access_iterator(_ptr - n); }
random_access_iterator operator--(int) { return random_access_iterator(_ptr--); }
random_access_iterator &operator--() { --_ptr; return *this; }
random_access_iterator operator-=(difference_type n) { _ptr -= n; return *this; }
pointer operator->() const { return _ptr; }
reference operator[](difference_type n) { return _ptr[n]; }
const reference operator[](difference_type n) const { return _ptr[n]; }
};
//operator overloading
template <typename T>
random_access_iterator<T> operator+(typename random_access_iterator<T>::difference_type n, const random_access_iterator<T> &it) { return random_access_iterator<T>(it.base() + n); }
template <typename T>
typename random_access_iterator<T>::difference_type operator-(const random_access_iterator<T> &lhs, const random_access_iterator<T> &rhs) { return lhs.base() - rhs.base(); }
template <typename T1, typename T2>
typename random_access_iterator<T1>::difference_type operator-(const random_access_iterator<T1> &lhs, const random_access_iterator<T2> &rhs) { return lhs.base() - rhs.base(); }
template <typename T>
bool operator==(const random_access_iterator<T> &lhs, const random_access_iterator<T> &rhs) { return lhs.base() == rhs.base(); }
template <typename T1, typename T2>
bool operator==(const random_access_iterator<T1> &lhs, const random_access_iterator<T2> &rhs) { return lhs.base() == rhs.base(); }
template <typename T>
bool operator!=(const random_access_iterator<T> &lhs, const random_access_iterator<T> &rhs) { return lhs.base() != rhs.base(); }
template <typename T1, typename T2>
bool operator!=(const random_access_iterator<T1> &lhs, const random_access_iterator<T2> &rhs) { return lhs.base() != rhs.base(); }
template <typename T>
bool operator<(const random_access_iterator<T> &lhs, const random_access_iterator<T> &rhs) { return lhs.base() < rhs.base(); }
template <typename T1, typename T2>
bool operator<(const random_access_iterator<T1> &lhs, const random_access_iterator<T2> &rhs) { return lhs.base() < rhs.base(); }
template <typename T>
bool operator<=(const random_access_iterator<T> &lhs, const random_access_iterator<T> &rhs) { return lhs.base() <= rhs.base(); }
template <typename T1, typename T2>
bool operator<=(const random_access_iterator<T1> &lhs, const random_access_iterator<T2> &rhs) { return lhs.base() <= rhs.base(); }
template <typename T>
bool operator>(const random_access_iterator<T> &lhs, const random_access_iterator<T> &rhs) { return lhs.base() > rhs.base(); }
template <typename T1, typename T2>
bool operator>(const random_access_iterator<T1> &lhs, const random_access_iterator<T2> &rhs) { return lhs.base() > rhs.base(); }
template <typename T>
bool operator>=(const random_access_iterator<T> &lhs, const random_access_iterator<T> &rhs) { return lhs.base() >= rhs.base(); }
template <typename T1, typename T2>
bool operator>=(const random_access_iterator<T1> &lhs, const random_access_iterator<T2> &rhs) { return lhs.base() >= rhs.base(); }
}
#endif