Skip to content

Commit

Permalink
Vector (const) view
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomaqa committed Oct 31, 2024
1 parent 3254d14 commit 77b3048
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/common/View.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef OPENSMT_VIEW_H
#define OPENSMT_VIEW_H

#include <ranges>

namespace opensmt {

template<typename V>
class VectorView : public std::ranges::view_interface<VectorView<V>> {
public:
using Vector = V;
using Iterator = typename Vector::const_iterator;

VectorView() = default;
VectorView(Vector const & vec) : VectorView(vec.cbegin(), vec.cend()) {}
VectorView(Iterator begin_, Iterator end_) : _begin(begin_), _end(end_) {}

Iterator begin() const { return _begin; }
Iterator end() const { return _end; }

private:
Iterator _begin{};
Iterator _end{};
};

} // namespace opensmt

#endif // OPENSMT_VIEW_H
14 changes: 10 additions & 4 deletions src/minisat/mtl/Vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class vec {
static inline void nextCap(int& cap){ cap += ((cap >> 1) + 2) & ~1; }

public:
// types for STL compatibility
using iterator = T *;
using const_iterator = T const *;

// Constructors:
vec() : data(NULL) , sz(0) , cap(0) { }
explicit vec(int size) : data(NULL) , sz(0) , cap(0) { growTo(size); }
Expand Down Expand Up @@ -100,10 +104,12 @@ class vec {
T& operator [] (int index) { return data[index]; }

// methods for STL compatibility
T* begin() { return data; }
const T* begin() const { return data; }
T* end() { return data + sz; }
const T* end() const { return data + sz; }
iterator begin() { return data; }
const_iterator begin() const { return data; }
iterator end() { return data + sz; }
const_iterator end() const { return data + sz; }
const_iterator cbegin() const { return begin(); }
const_iterator cend() const { return end(); }


// Duplicatation (preferred instead):
Expand Down

0 comments on commit 77b3048

Please sign in to comment.