Google OR-Tools: ortools/base/strong_vector.h Source File

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#ifndef ORTOOLS_BASE_STRONG_VECTOR_H_

62#define ORTOOLS_BASE_STRONG_VECTOR_H_

63

64#include <limits>

65#include <type_traits>

66#include <vector>

67

70

72

73template <typename IntType, typename NativeType,

74 typename Alloc = std::allocator<NativeType>>

75class StrongVector : protected std::vector<NativeType, Alloc> {

76 public:

77 typedef std::vector<NativeType, Alloc> ParentType;

78 typedef typename ParentType::size_type size_type;

80 typedef typename ParentType::value_type value_type;

81 typedef typename ParentType::reference reference;

83 typedef typename ParentType::pointer pointer;

85 typedef typename ParentType::iterator iterator;

89

90 public:

113 template <typename InputIteratorType>

120

121

122

124

125

126

127

128

129

131

132

134 return ParentType::operator[](static_cast<size_type>(i.value()));

135 }

137 return ParentType::operator[](static_cast<size_type>(i.value()));

138 }

140 return ParentType::at(static_cast<size_type>(i.value()));

141 }

143 return ParentType::at(static_cast<size_type>(i.value()));

144 }

145

146

147

148

149

150

152

153

154

155

158 return IntType(size());

159 }

160

161

162 bool IsValidSize() const { return ValidSize(size()); }

163

164

165 using ParentType::back;

166 using ParentType::begin;

167 using ParentType::capacity;

168 using ParentType::cbegin;

169 using ParentType::cend;

170 using ParentType::clear;

171 using ParentType::empty;

172 using ParentType::end;

173 using ParentType::erase;

174 using ParentType::front;

175 using ParentType::max_size;

176 using ParentType::pop_back;

177 using ParentType::rbegin;

178 using ParentType::rend;

179 using ParentType::shrink_to_fit;

180

181

182

183

184

185

186

187

188

189

193

194

195

196

197

198

201

203 ParentType::operator=(x.get());

204 return *this;

205 }

208 ParentType::operator=(l);

210 return *this;

211 }

212

213 void swap(StrongVector& x) noexcept { ParentType::swap(*x.mutable_get()); }

214

216 DCHECK(ValidSize(n));

217 ParentType::assign(n, val);

218 }

219 template <typename InputIt>

220 void assign(InputIt f, InputIt l) {

221 ParentType::assign(f, l);

223 }

224 void assign(std::initializer_list<value_type> l) {

225 ParentType::assign(l);

227 }

228

229 template <typename... Args>

231 iterator result = ParentType::emplace(pos, std::forward<Args>(args)...);

233 return result;

234 }

235

236 template <typename... Args>

238 reference value = ParentType::emplace_back(std::forward<Args>(args)...);

240 return value;

241 }

242

244 iterator result = ParentType::insert(pos, x);

246 return result;

247 }

249 iterator result = ParentType::insert(pos, std::move(x));

251 return result;

252 }

254 ParentType::insert(pos, n, x);

256 }

257 template <typename SIT>

259 ParentType::insert(pos, first, last);

261 }

262

264 ParentType::push_back(val);

266 }

268 ParentType::push_back(std::move(val));

270 }

271

273 DCHECK(ValidSize(n));

274 ParentType::reserve(n);

275 }

276

278

280 DCHECK(ValidSize(new_size));

281 ParentType::resize(new_size);

282 }

283

287

289 DCHECK(ValidSize(new_size));

290 ParentType::resize(new_size, x);

291 }

292

296

297 using ParentType::size;

298

299 static_assert(std::is_integral<typename IntType::ValueType>::value,

300 "int type indexed vector must have integral index");

301

302 template <typename H>

304 return H::combine(std::move(h), v.get());

305 }

306

307 private:

308

309 static bool ValidSize(size_type n) {

310 return n <= std::numeric_limits<typename IntType::ValueType>::max();

311 }

312

314 return x.get() == y.get();

315 }

317 return x.get() != y.get();

318 }

320 return x.get() < y.get();

321 }

323 return x.get() > y.get();

324 }

326 return x.get() <= y.get();

327 }

329 return x.get() >= y.get();

330 }

332};

333

334}

335

336#endif

friend H AbslHashValue(H h, const StrongVector &v)

Definition strong_vector.h:303

friend bool operator!=(const StrongVector &x, const StrongVector &y)

Definition strong_vector.h:316

friend void swap(StrongVector &x, StrongVector &y) noexcept

Definition strong_vector.h:331

ParentType::reference reference

Definition strong_vector.h:81

ParentType::const_reverse_iterator const_reverse_iterator

Definition strong_vector.h:88

void assign(size_type n, const value_type &val)

Definition strong_vector.h:215

value_type * data()

Definition strong_vector.h:199

std::vector< NativeType, Alloc > ParentType

Definition strong_vector.h:77

StrongVector(InputIteratorType first, InputIteratorType last, const allocator_type &a=allocator_type())

Definition strong_vector.h:114

StrongVector()

Definition strong_vector.h:91

StrongVector(size_type n, const value_type &v, const allocator_type &a=allocator_type())

Definition strong_vector.h:96

const ParentType & get() const

Definition strong_vector.h:123

void reserve(IntType n)

Definition strong_vector.h:277

void swap(StrongVector &x) noexcept

Definition strong_vector.h:213

void assign(InputIt f, InputIt l)

Definition strong_vector.h:220

StrongVector & operator=(StrongVector &&x)=default

void resize(IntType new_size)

Definition strong_vector.h:284

void resize(IntType new_size, const value_type &x)

Definition strong_vector.h:293

void push_back(const value_type &val)

Definition strong_vector.h:263

friend bool operator>=(const StrongVector &x, const StrongVector &y)

Definition strong_vector.h:328

iterator insert(const_iterator pos, value_type &&x)

Definition strong_vector.h:248

friend bool operator==(const StrongVector &x, const StrongVector &y)

Definition strong_vector.h:313

StrongVector & operator=(std::initializer_list< value_type > l)

Definition strong_vector.h:207

iterator insert(const_iterator pos, const value_type &x)

Definition strong_vector.h:243

StrongVector & operator=(const StrongVector &x)

Definition strong_vector.h:202

const value_type * data() const

Definition strong_vector.h:200

friend bool operator>(const StrongVector &x, const StrongVector &y)

Definition strong_vector.h:322

void reserve(size_type n)

Definition strong_vector.h:272

IntType start_index() const

Definition strong_vector.h:151

const_reference at(IntType i) const

Definition strong_vector.h:142

reference emplace_back(Args &&... args)

Definition strong_vector.h:237

ParentType::const_pointer const_pointer

Definition strong_vector.h:84

StrongVector(std::initializer_list< value_type > l, const allocator_type &a=allocator_type())

Definition strong_vector.h:108

const_reference operator[](IntType i) const

Definition strong_vector.h:136

bool IsValidSize() const

Definition strong_vector.h:162

ParentType::value_type value_type

Definition strong_vector.h:80

~StrongVector()

Definition strong_vector.h:119

IntType end_index() const

Definition strong_vector.h:156

StrongVector(size_type n)

Definition strong_vector.h:93

StrongVector(const allocator_type &a)

Definition strong_vector.h:92

void resize(size_type new_size)

Definition strong_vector.h:279

void resize(size_type new_size, const value_type &x)

Definition strong_vector.h:288

void assign(std::initializer_list< value_type > l)

Definition strong_vector.h:224

StrongVector(StrongVector &&x)=default

StrongVector(IntType n, const value_type &v, const allocator_type &a=allocator_type())

Definition strong_vector.h:101

friend bool operator<=(const StrongVector &x, const StrongVector &y)

Definition strong_vector.h:325

ParentType::reverse_iterator reverse_iterator

Definition strong_vector.h:87

ParentType::const_reference const_reference

Definition strong_vector.h:82

StrongVector(IntType n)

Definition strong_vector.h:94

reference operator[](IntType i)

Definition strong_vector.h:133

ParentType * mutable_get()

Definition strong_vector.h:130

iterator emplace(const_iterator pos, Args &&... args)

Definition strong_vector.h:230

ParentType::size_type size_type

Definition strong_vector.h:78

ParentType::allocator_type allocator_type

Definition strong_vector.h:79

reference at(IntType i)

Definition strong_vector.h:139

ParentType::const_iterator const_iterator

Definition strong_vector.h:86

void insert(const_iterator pos, size_type n, const value_type &x)

Definition strong_vector.h:253

StrongIntRange< IntType > index_range() const

Definition strong_vector.h:190

void insert(const_iterator pos, SIT first, SIT last)

Definition strong_vector.h:258

friend bool operator<(const StrongVector &x, const StrongVector &y)

Definition strong_vector.h:319

ParentType::pointer pointer

Definition strong_vector.h:83

void push_back(value_type &&val)

Definition strong_vector.h:267

StrongVector(const StrongVector &x)

Definition strong_vector.h:104

ParentType::iterator iterator

Definition strong_vector.h:85