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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
// Copyright (C) 2008 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_STD_VECTOr_C_ABSTRACT_H_
#ifdef DLIB_STD_VECTOr_C_ABSTRACT_H_
#include <vector>
#include <algorithm>
#include "../assert.h"
namespace dlib
{
template <
typename T,
typename Allocator = std::allocator<T>
>
class std_vector_c : public std::vector<T,Allocator>
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is a simple wrapper around the std::vector object. It
provides an identical interface but also checks the preconditions of
each member function. That is, if you violate a requires
clause the dlib::fatal_error exception is thrown.
!*/
typedef typename std::vector<T,Allocator> base_type;
public:
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
typedef T value_type;
typedef Allocator allocator_type;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
explicit std_vector_c(
const Allocator& alloc = Allocator()
);
/*!
ensures
- #get_allocator() == alloc
- #size() == 0
!*/
explicit std_vector_c (
size_type n,
const T& value = T(),
const Allocator& alloc = Allocator()
);
/*!
ensures
- #size() == n
- #get_allocator() == alloc
- for all valid i:
- (*this)[i] == value
!*/
template <typename InputIterator>
std_vector_c (
InputIterator first,
InputIterator last,
const Allocator& alloc = Allocator()
);
/*!
ensures
- #size() == std::distance(first,last)
- #get_allocator() == alloc
- std::equal(first, last, begin()) == true
!*/
std_vector_c(
const std::vector<T,Allocator>& x
);
/*!
ensures
- #*this == x
!*/
std_vector_c<T,Allocator>& operator= (
const std::vector<T,Allocator>& x
);
/*!
ensures
- #*this == x
- returns #*this
!*/
template <typename InputIterator>
void assign(
InputIterator first,
InputIterator last
);
/*!
ensures
- #size() == std::distance(first,last)
- std::equal(first, last, begin()) == true
!*/
void assign(
size_type n,
const T& value
);
/*!
ensures
- #size() == n
- for all valid i:
- (*this)[i] == value
!*/
allocator_type get_allocator(
) const;
/*!
ensures
- returns the allocator used by this vector
!*/
iterator begin(
);
/*!
ensures
- if (size() > 0) then
- returns an iterator referring to the first element in
this container.
- else
- returns end()
!*/
const_iterator begin(
) const;
/*!
ensures
- if (size() > 0) then
- returns a const_iterator referring to the first element in
this container.
- else
- returns end()
!*/
iterator end(
);
/*!
ensures
- returns an iterator that represents one past the end of
this container
!*/
const_iterator end(
) const;
/*!
ensures
- returns an iterator that represents one past the end of
this container
!*/
reverse_iterator rbegin(
);
/*!
ensures
- returns std::reverse_iterator(end())
!*/
const_reverse_iterator rbegin(
) const;
/*!
ensures
- returns std::reverse_iterator(end())
!*/
reverse_iterator rend(
);
/*!
ensures
- returns std::reverse_iterator(begin())
!*/
const_reverse_iterator rend(
) const;
/*!
ensures
- returns std::reverse_iterator(begin())
!*/
size_type size(
) const;
/*!
ensures
- returns end()-begin()
(i.e. returns the number of elements in this container)
!*/
size_type max_size(
) const;
/*!
ensures
- returns the maximum number of elements this vector can contain
!*/
void resize(
size_type sz,
T c = T()
);
/*!
ensures
- #size() == sz
- any element with index between 0 and sz - 1 which was in the
vector before the call to resize() retains its value and index.
All other elements have a value given by c.
!*/
size_type capacity(
) const;
/*!
ensures
- returns the total number of elements that the vector can hold without
requiring reallocation.
!*/
bool empty(
) const;
/*!
ensures
- if (size() == 0) then
- returns true
- else
- returns false
!*/
void reserve(
size_type n
);
/*!
ensures
- #capacity() >= n
!*/
const_reference at(
size_type n
) const;
/*!
ensures
- if (n < size()) then
- returns a const reference to (*this)[n]
- else
- throws std::out_of_range
!*/
reference at(
size_type n
);
/*!
ensures
- if (n < size()) then
- returns a reference to (*this)[n]
- else
- throws std::out_of_range
!*/
void push_back(
const T& x
);
/*!
ensures
- #size() == size() + 1
- #back() == x
!*/
void swap(
std_vector_c<T,Allocator>& x
);
/*!
ensures
- swaps the state of *this and x
!*/
void clear(
);
/*!
ensures
- #size() == 0
!*/
reference operator[](
size_type n
);
/*!
requires
- n < size()
ensures
- returns a reference to the nth element of this container
!*/
const_reference operator[](
size_type n
) const;
/*!
requires
- n < size()
ensures
- returns a const reference to the nth element of this container
!*/
reference front(
);
/*!
requires
- size() > 0
ensures
- returns a reference to (*this)[0]
!*/
const_reference front(
) const;
/*!
requires
- size() > 0
ensures
- returns a const reference to (*this)[0]
!*/
reference back(
);
/*!
requires
- size() > 0
ensures
- returns a reference to (*this)[size()-1]
!*/
const_reference back(
) const;
/*!
requires
- size() > 0
ensures
- returns a const reference to (*this)[size()-1]
!*/
void pop_back(
);
/*!
requires
- size() > 0
ensures
- #size() == size() - 1
- removes the last element in the vector but leaves the others
unmodified.
!*/
iterator insert(
iterator position,
const T& x
);
/*!
requires
- begin() <= position && position <= end()
(i.e. position references an element in this vector object)
ensures
- #size() == size() + 1
- inserts a copy of x into *this before the given position
- returns an iterator that points to the copy of x inserted
into *this
!*/
void insert(
iterator position,
size_type n,
const T& x
);
/*!
requires
- begin() <= position && position <= end()
(i.e. position references an element in this vector object)
ensures
- #size() == size() + n
- inserts n copies of x into *this before the given position
!*/
template <typename InputIterator>
void insert(
iterator position,
InputIterator first,
InputIterator last
);
/*!
requires
- begin() <= position && position <= end()
(i.e. position references an element in this vector object)
- first and last are not iterators into *this
ensures
- #size() == size() + std::distance(last,first)
- inserts copies of the range of elements [first,last) into *this
before the given position
!*/
iterator erase(
iterator position
);
/*!
requires
- begin() <= position && position < end()
(i.e. position references an element in this vector object)
ensures
- #size() == size() - 1
- removes the element in this vector referenced by position but
leaves all other elements in this vector unmodified.
- if (position < end()-1) then
- returns an iterator referencing the element immediately
following *position prior to the erase.
- else
- returns end()
!*/
iterator erase(
iterator first,
iterator last
);
/*!
requires
- begin() <= first && first <= last && last <= end()
(i.e. the range [first,last) must be inside this container )
ensures
- #size() == size() - (last-first)
- removes the elements in this vector referenced by the
iterator range [first,last) but leaves all other elements
in this vector unmodified.
- if (last < end()-1) then
- returns an iterator referencing the element immediately
following *last prior to the erase.
- else
- returns end()
!*/
};
// ----------------------------------------------------------------------------------------
template <typename T, typename alloc>
void serialize (
const std_vector_c<T,alloc>& item,
std::ostream& out
);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
template <typename T, typename alloc>
void deserialize (
std_vector_c<T, alloc>& item,
std::istream& in
);
/*!
provides deserialization support
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_STD_VECTOr_C_ABSTRACT_H_
|