| 1 |
#ifndef INCLUDED_STRINGPTR_H_ |
| 2 |
#define INCLUDED_STRINGPTR_H_ |
| 3 |
|
| 4 |
#include <string> |
| 5 |
#include <vector> |
| 6 |
#include <iterator> |
| 7 |
#include <compare> |
| 8 |
|
| 9 |
//STRINGPTR |
| 10 |
struct StringPtr: public std::vector<std::string *> |
| 11 |
{ |
| 12 |
class iterator; |
| 13 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 14 |
|
| 15 |
iterator begin(); |
| 16 |
iterator end(); |
| 17 |
reverse_iterator rbegin(); |
| 18 |
reverse_iterator rend(); |
| 19 |
}; |
| 20 |
|
| 21 |
struct StringPtr::iterator |
| 22 |
{ |
| 23 |
//USING |
| 24 |
using iterator_category = std::random_access_iterator_tag; |
| 25 |
using difference_type = std::ptrdiff_t; |
| 26 |
using value_type = std::string; |
| 27 |
using pointer = value_type *; |
| 28 |
using reference = value_type &; |
| 29 |
//= |
| 30 |
|
| 31 |
friend class StringPtr; |
| 32 |
|
| 33 |
friend bool operator==(iterator const &lhs, iterator const &rhs); |
| 34 |
friend auto operator<=>(iterator const &lhs, iterator const &rhs); |
| 35 |
friend int operator-(iterator const &lhs, iterator const &rhs); |
| 36 |
friend iterator operator+(iterator const &lhs, int step); |
| 37 |
friend iterator operator-(iterator const &lhs, int step); |
| 38 |
|
| 39 |
private: |
| 40 |
std::vector<std::string *>::iterator d_current; |
| 41 |
|
| 42 |
public: |
| 43 |
iterator &operator--(); |
| 44 |
iterator operator--(int); |
| 45 |
iterator &operator++(); |
| 46 |
iterator operator++(int); |
| 47 |
|
| 48 |
iterator &operator+=(int step); // increment over `n' steps |
| 49 |
iterator &operator-=(int step); // decrement over `n' steps |
| 50 |
|
| 51 |
std::string &operator*() const; |
| 52 |
std::string *operator->() const;// access the fields of the |
| 53 |
// struct an iterator points |
| 54 |
// to. E.g., it->length() |
| 55 |
private: |
| 56 |
iterator(std::vector<std::string *>::iterator const ¤t); |
| 57 |
}; |
| 58 |
//= |
| 59 |
|
| 60 |
//PRIVATEIMP |
| 61 |
inline StringPtr::iterator::iterator( |
| 62 |
std::vector<std::string *>::iterator const ¤t) |
| 63 |
: |
| 64 |
d_current(current) |
| 65 |
{} |
| 66 |
//= |
| 67 |
|
| 68 |
//PREDEC |
| 69 |
inline StringPtr::iterator &StringPtr::iterator::operator--() |
| 70 |
{ |
| 71 |
--d_current; |
| 72 |
return *this; |
| 73 |
} |
| 74 |
//= |
| 75 |
//POSTDEC |
| 76 |
inline StringPtr::iterator StringPtr::iterator::operator--(int) |
| 77 |
{ |
| 78 |
return iterator(d_current--); |
| 79 |
} |
| 80 |
//= |
| 81 |
//PREINC |
| 82 |
inline StringPtr::iterator &StringPtr::iterator::operator++() |
| 83 |
{ |
| 84 |
++d_current; |
| 85 |
return *this; |
| 86 |
} |
| 87 |
//= |
| 88 |
//POSTINC |
| 89 |
inline StringPtr::iterator StringPtr::iterator::operator++(int) |
| 90 |
{ |
| 91 |
return iterator(d_current++); |
| 92 |
} |
| 93 |
//= |
| 94 |
//OPEQ |
| 95 |
inline bool operator==(StringPtr::iterator const &lhs, |
| 96 |
StringPtr::iterator const &rhs) |
| 97 |
{ |
| 98 |
return lhs.d_current == rhs.d_current; |
| 99 |
} |
| 100 |
//= |
| 101 |
//OPSPACE |
| 102 |
inline auto operator<=>(StringPtr::iterator const &lhs, |
| 103 |
StringPtr::iterator const &rhs) |
| 104 |
{ |
| 105 |
return lhs.d_current <=> rhs.d_current; |
| 106 |
} |
| 107 |
//= |
| 108 |
//OPSUB |
| 109 |
inline int operator-(StringPtr::iterator const &lhs, |
| 110 |
StringPtr::iterator const &rhs) |
| 111 |
{ |
| 112 |
return lhs.d_current - rhs.d_current; |
| 113 |
} |
| 114 |
//= |
| 115 |
//OP* |
| 116 |
inline std::string &StringPtr::iterator::operator*() const |
| 117 |
{ |
| 118 |
return **d_current; |
| 119 |
} |
| 120 |
//= |
| 121 |
//= |
| 122 |
//OPADD |
| 123 |
inline StringPtr::iterator operator+(StringPtr::iterator const &lhs, |
| 124 |
int step) |
| 125 |
{ |
| 126 |
StringPtr::iterator ret{ lhs }; |
| 127 |
ret.d_current += step; // avoids ambiguity |
| 128 |
return ret; |
| 129 |
} |
| 130 |
//= |
| 131 |
//OP- |
| 132 |
inline StringPtr::iterator operator-(StringPtr::iterator const &lhs, |
| 133 |
int step) |
| 134 |
{ |
| 135 |
StringPtr::iterator ret{ lhs }; |
| 136 |
ret.d_current -= step; // avoids ambiguity |
| 137 |
return ret; |
| 138 |
} |
| 139 |
//= |
| 140 |
//OPARITH |
| 141 |
inline StringPtr::iterator &StringPtr::iterator::operator+=(int step) |
| 142 |
{ |
| 143 |
d_current += step; |
| 144 |
return *this; |
| 145 |
} |
| 146 |
inline StringPtr::iterator &StringPtr::iterator::operator-=(int step) |
| 147 |
{ |
| 148 |
d_current -= step; |
| 149 |
return *this; |
| 150 |
} |
| 151 |
//= |
| 152 |
//OPARROW |
| 153 |
inline std::string *StringPtr::iterator::operator->() const |
| 154 |
{ |
| 155 |
return *d_current; |
| 156 |
} |
| 157 |
//= |
| 158 |
//BEGEND |
| 159 |
inline StringPtr::iterator StringPtr::begin() |
| 160 |
{ |
| 161 |
return iterator(std::vector<std::string *>::begin()); |
| 162 |
} |
| 163 |
inline StringPtr::iterator StringPtr::end() |
| 164 |
{ |
| 165 |
return iterator(std::vector<std::string *>::end()); |
| 166 |
} |
| 167 |
//= |
| 168 |
//RBEGEND |
| 169 |
inline StringPtr::reverse_iterator StringPtr::rbegin() |
| 170 |
{ |
| 171 |
return reverse_iterator(end()); |
| 172 |
} |
| 173 |
inline StringPtr::reverse_iterator StringPtr::rend() |
| 174 |
{ |
| 175 |
return reverse_iterator(begin()); |
| 176 |
} |
| 177 |
//= |
| 178 |
|
| 179 |
#endif |