dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
ScopedVector.hpp
1
11#pragma once
12
13#include <algorithm>
14#include <cstddef>
15#include <iostream>
16#include <stdexcept>
17#include <utility>
18#include <vector>
19
20namespace dlinear {
21
22template <typename T>
24 public:
25 typedef std::vector<T> vector;
26 typedef typename vector::value_type value_type;
27 typedef typename vector::iterator iterator;
28 typedef typename vector::const_iterator const_iterator;
29 typedef typename vector::reverse_iterator reverse_iterator;
30 typedef typename vector::const_reverse_iterator const_reverse_iterator;
31 typedef typename vector::size_type size_type;
32 typedef typename vector::reference reference;
33 typedef typename vector::const_reference const_reference;
34
35 ScopedVector() = default;
36 ScopedVector(const ScopedVector &) = default;
37 ScopedVector(ScopedVector &&) noexcept = default;
38 ScopedVector &operator=(const ScopedVector &) = default;
39 ScopedVector &operator=(ScopedVector &&) noexcept = default;
40 ~ScopedVector() = default;
41
42 iterator begin() { return vector_.begin(); }
43 iterator end() { return vector_.end(); }
44 const_iterator begin() const { return vector_.cbegin(); }
45 const_iterator end() const { return vector_.cend(); }
46 const_iterator cbegin() const { return vector_.cbegin(); }
47 const_iterator cend() const { return vector_.cend(); }
48 reverse_iterator rbegin() { return vector_.rbegin(); }
49 reverse_iterator rend() { return vector_.rend(); }
50 const_reverse_iterator rbegin() const { return vector_.crbegin(); }
51 const_reverse_iterator rend() const { return vector_.crend(); }
52 const_reverse_iterator crbegin() const { return vector_.crbegin(); }
53 const_reverse_iterator crend() const { return vector_.crend(); }
54
55 void push_back(value_type const &v) { vector_.push_back(v); }
56 void push_back(value_type &&v) { vector_.push_back(std::move(v)); }
57 void push() { scopes_.push_back(vector_.size()); }
58 size_t pop() {
59 if (scopes_.empty()) {
60 throw std::runtime_error("Nothing to pop.");
61 }
62 size_t count = 0;
63 size_t const prev_size = scopes_.back();
64 scopes_.pop_back();
65 size_t cur_size = vector_.size();
66 while (cur_size-- > prev_size) {
67 vector_.pop_back();
68 count++;
69 }
70 return count;
71 }
72
73 [[nodiscard]] bool empty() const { return vector_.empty(); }
74 [[nodiscard]] size_t size() const { return vector_.size(); }
75 [[nodiscard]] vector const &get_vector() const { return vector_; }
76 [[nodiscard]] vector get_vector() { return vector_; }
77
78 reference first() { return vector_.at(0); }
79 const_reference first() const { return vector_.at(0); }
80 reference last() { return vector_.at(size() - 1); }
81 const_reference last() const { return vector_.at(size() - 1); }
82 reference operator[](size_type n) { return vector_[n]; }
83 const_reference operator[](size_type n) const { return vector_[n]; }
84 bool operator<(ScopedVector<T> const &v) const {
85 for (size_t i = 0; i < vector_.size(); i++) {
86 if (vector_[i] < v.vector_[i]) {
87 return true;
88 }
89 }
90 return false;
91 }
92 vector get_reverse() const {
93 vector tmp = vector_;
94 std::reverse(tmp.begin(), tmp.end());
95 return tmp;
96 }
97
98 friend std::ostream &operator<<(std::ostream &os, ScopedVector<T> const &v) {
99 for (auto const &e : v) os << e << std::endl;
100 return os;
101 }
102
103 private:
104 vector vector_;
105 std::vector<size_t> scopes_;
106};
107
108} // namespace dlinear
Global namespace for the dlinear library.