Skip to content

Commit 2911916

Browse files
committed
Vector (const) view
1 parent 19afef1 commit 2911916

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/common/View.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef OPENSMT_VIEW_H
2+
#define OPENSMT_VIEW_H
3+
4+
#include <ranges>
5+
6+
namespace opensmt {
7+
8+
template<typename V>
9+
class VectorView : public std::ranges::view_interface<VectorView<V>> {
10+
public:
11+
using Vector = V;
12+
using Iterator = typename Vector::const_iterator;
13+
14+
VectorView() = default;
15+
VectorView(Vector const & vec) : VectorView(vec.cbegin(), vec.cend()) {}
16+
VectorView(Iterator begin_, Iterator end_) : _begin(begin_), _end(end_) {}
17+
18+
Iterator begin() const { return _begin; }
19+
Iterator end() const { return _end; }
20+
21+
private:
22+
Iterator _begin{};
23+
Iterator _end{};
24+
};
25+
26+
} // namespace opensmt
27+
28+
#endif // OPENSMT_VIEW_H

src/minisat/mtl/Vec.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class vec {
5252
static inline void nextCap(int& cap){ cap += ((cap >> 1) + 2) & ~1; }
5353

5454
public:
55+
// types for STL compatibility
56+
using iterator = T *;
57+
using const_iterator = T const *;
58+
5559
// Constructors:
5660
vec() : data(NULL) , sz(0) , cap(0) { }
5761
explicit vec(int size) : data(NULL) , sz(0) , cap(0) { growTo(size); }
@@ -100,10 +104,12 @@ class vec {
100104
T& operator [] (int index) { return data[index]; }
101105

102106
// methods for STL compatibility
103-
T* begin() { return data; }
104-
const T* begin() const { return data; }
105-
T* end() { return data + sz; }
106-
const T* end() const { return data + sz; }
107+
iterator begin() { return data; }
108+
const_iterator begin() const { return data; }
109+
iterator end() { return data + sz; }
110+
const_iterator end() const { return data + sz; }
111+
const_iterator cbegin() const { return begin(); }
112+
const_iterator cend() const { return end(); }
107113

108114

109115
// Duplicatation (preferred instead):

0 commit comments

Comments
 (0)