3 #ifndef INCL_MADARA_RCW_TRACKED_H 4 #define INCL_MADARA_RCW_TRACKED_H 18 #include <type_traits> 19 #include <initializer_list> 23 #include "madara/MADARA_export.h" 33 template<
class T,
class Impl>
64 template<
class T,
class Impl>
92 explicit Tracked(T val) : val_(val), dirty_(true) {}
101 explicit operator const T& ()
const 181 explicit operator bool()
183 return val_ ?
true :
false;
188 template<class U, typename std::enable_if<std::is_convertible<T, U>::value,
189 void*>::type =
nullptr>
230 #define MADARA_AUTOTYPE_BODY(body) \ 231 -> decltype(body) { return (body); } 233 #define MADARA_MAKE_BINARY_COMPARE_OP(op) \ 234 template<class T, class U> \ 235 auto operator op(const Tracked<T> &lhs, const Tracked<U> &rhs) \ 236 MADARA_AUTOTYPE_BODY(lhs.get() op rhs.get()) \ 237 template<class T, class U> \ 238 auto operator op(const Tracked<T> &lhs, U &&rhs) \ 239 MADARA_AUTOTYPE_BODY(lhs.get() op std::forward<U>(rhs)) \ 240 template<class T, class U> \ 241 auto operator op(T &&lhs, const Tracked<U> &rhs) \ 242 MADARA_AUTOTYPE_BODY(std::forward<T>(lhs) op rhs.get()) 251 #define MADARA_MAKE_COMPOUND_OP(op) \ 252 template<class T, class U> \ 253 auto operator op##=(Tracked<T> &lhs, const Tracked<U> &rhs) \ 254 MADARA_AUTOTYPE_BODY(lhs.get_mut() op##= rhs.get()) \ 255 template<class T, class U> \ 256 auto operator op##=(Tracked<T> &lhs, U &&rhs) \ 257 MADARA_AUTOTYPE_BODY(lhs.get_mut() op##= std::forward<U>(rhs)) \ 258 template<class T, class U> \ 259 auto operator op##=(T &lhs, const Tracked<U> &rhs) \ 260 MADARA_AUTOTYPE_BODY(lhs op##= rhs.get()) 262 #define MADARA_MAKE_BINARY_ARITH_OP(op) \ 263 template<class T, class U> \ 264 auto operator op(const Tracked<T> &lhs, const Tracked<U> &rhs) \ 265 MADARA_AUTOTYPE_BODY(lhs.get() op rhs.get()) \ 266 template<class T, class U> \ 267 auto operator op(const Tracked<T> &lhs, U &&rhs) \ 268 MADARA_AUTOTYPE_BODY(lhs.get() op std::forward<U>(rhs)) \ 269 template<class T, class U> \ 270 auto operator op(T &&lhs, const Tracked<U> &rhs) \ 271 MADARA_AUTOTYPE_BODY(std::forward<T>(lhs) op rhs.get()) \ 272 MADARA_MAKE_COMPOUND_OP(op) 285 #define MADARA_MAKE_UNARY_ARITH_OP(op) \ 287 auto operator op(const Tracked<T> &lhs) \ 288 MADARA_AUTOTYPE_BODY(op lhs.get()) 295 #define MADARA_MAKE_INCDEC_OP(op) \ 296 template<class T> auto operator op(Tracked<T> &lhs) \ 297 MADARA_AUTOTYPE_BODY(op lhs.get_mut()) \ 298 template<class T> auto operator op(Tracked<T> &lhs, int) \ 299 MADARA_AUTOTYPE_BODY(lhs.get_mut() op) 331 template<
typename T,
typename Impl>
335 Impl &
impl() {
return static_cast<Impl&
>(*this); }
336 const Impl &
impl()
const {
return static_cast<const Impl&
>(*this); }
345 void set(
size_t i, value_type val)
347 impl().val_.at(i) = val;
352 const value_type &
at(
size_t i)
const 354 return impl().val_.at(i);
360 return impl().val_[i];
364 const value_type &
get(
size_t i)
const 374 return impl().val_.at(i);
396 size_t size() {
return impl().val_.size(); }
398 bool empty() {
return impl().val_.empty(); }
400 size_t max_size() {
return impl().val_.max_size(); }
411 return at(size() - 1);
422 return at_mut(size() - 1);
428 return impl().val_.begin();
438 const_iterator
end()
const 440 return impl().val_.end();
451 template<
typename Char,
typename Impl>
455 Impl &
impl() {
return static_cast<Impl&
>(*this); }
456 const Impl &
impl()
const {
return static_cast<const Impl&
>(*this); }
459 typedef typename std::basic_string<Char>::value_type
value_type;
473 return impl().is_dirty();
480 return impl().clear_dirty();
486 impl().val_.resize(count);
493 return impl().val_.capacity();
499 impl().val_.reserve(count);
505 impl().val_.push_back(value);
512 impl().val_.pop_back();
524 const value_type *
c_str()
const {
return impl().val_.c_str(); }
526 const value_type *
data()
const {
return impl().val_.data(); }
591 typedef std::vector<T>
Vec;
610 size_t shift = i - (idx << 6);
611 return {idx, 1 << shift};
617 return (i + 63) >> 6;
627 Tracked() : val_(), dirty_(), all_dirty_(true), size_dirty_(true) {}
630 : val_(val), dirty_(to_dirty_size(val.size())),
631 all_dirty_(true), size_dirty_(true) {}
634 const std::vector<T>&
get()
const 669 void set(
const std::vector<T> &val)
672 dirty_.resize(to_dirty_size(val_.size()));
709 for (
const uint64_t &cur : dirty_)
720 for (uint64_t &cur : dirty_)
727 dirty_.resize(to_dirty_size(val_.size()));
728 std::pair<size_t, uint64_t> x = to_dirty_bit(i);
729 dirty_.at(x.first) |= x.second;
735 dirty_.resize(to_dirty_size(val_.size()));
736 std::pair<size_t, uint64_t> x = to_dirty_bit(i);
737 dirty_.at(x.first) &= ~x.second;
743 std::pair<size_t, uint64_t> x = to_dirty_bit(i);
744 if (x.first >= dirty_.size())
746 return dirty_.at(x.first) & x.second;
753 dirty_.resize(to_dirty_size(val_.size()));
760 size_t vc = val_.capacity();
761 size_t dc = to_dirty_size(dirty_.capacity());
762 return vc > dc ? dc : vc;
769 dirty_.reserve(to_dirty_size(count));
775 val_.push_back(value);
776 dirty_.resize(to_dirty_size(val_.size()));
785 dirty_.resize(to_dirty_size(val_.size()));
799 using Base::get_mutable;
815 swap(lhs.all_dirty_, rhs.all_dirty_);
816 swap(lhs.size_dirty_, rhs.size_dirty_);
831 return t.is_all_dirty();
838 return t.is_size_dirty();
866 #endif // INCL_MADARA_RCW_TRACKED_H bool all_dirty_
dirty status to treat entire vector as modified
Tracked< int16_t > TrackedI16
Tracked< unsigned long > TrackedULong
static std::pair< size_t, uint64_t > to_dirty_bit(size_t i)
Get dirty flag bit offset into dirty_ vector, plus mask.
value_type & get_mut(size_t i)
Synonym for.
Tracked< unsigned long long > TrackedULongLong
size_t max_size()
Pass through to max_size method of underlying collection.
Tracked()
Default constructor; default constructs wrapped object.
#define MADARA_MAKE_BINARY_COMPARE_OP(op)
Tracked< std::string > TrackedString
friend bool operator<=(const Tracked &lhs, const Tracked &rhs)
bool is_dirty() const
Get modification status.
const value_type & back() const
Equivalent to back method of underlying collection.
void pop_back()
Pass through pop_back method to underlying vector.
Tracked< long double > TrackedLongDouble
bool is_dirty(const Tracked< std::vector< T >> &t, size_t i)
Return dirty flag at index of Tracked vector.
Tracked< bool > TrackedBool
#define MADARA_MAKE_BINARY_ARITH_OP(op)
bool is_dirty(size_t i) const
Check i-th element modification status.
bool is_size_dirty(const Tracked< std::vector< T >> &t)
Return size changed dirty flag of Tracked vector.
bool is_all_dirty() const
Has entire vector been marked as modified? Note that this will still return false if every entry has ...
const_iterator cend() const
Pass through to cend method of underlying collection.
Tracked(std::vector< T > val)
Construct from an existing vector.
Tracked< uint32_t > TrackedU32
char dirty_
Wrapped value.
T & get_mutable()
Get a non-const ref to underlying object.
Tracked< int > TrackedInt
Tracked< long long > TrackedLongLong
Tracked< int32_t > TrackedI32
Tracked< signed char > TrackedSChar
void modify()
Flag entire vector as modified.
friend void swap(T &lhs, Tracked &rhs)
Implement swap between tracked object, and object of underlying type.
friend void swap(Tracked &lhs, Tracked &rhs)
Implement swap between two tracked objects of same type.
bool is_size_dirty() const
Has the size of this vector changed?
Tracked< short > TrackedShort
T::const_iterator const_iterator
const std::vector< T > * operator->() const
Use to call const methods on underlying vector.
void clear_dirty()
Clear all modification tracking.
Tracks the modification status of a wrapped object of the given type.
const value_type & operator[](size_t i) const
Pass through operator[] to underlying collection.
Tracked< long > TrackedLong
value_type & get_mutable(size_t i)
Synonym for.
#define MADARA_MAKE_UNARY_ARITH_OP(op)
void resize(size_t count)
Pass through resize method to underlying vector.
Tracked & operator=(T val)
Assignment writes to the underlying object.
void clear_dirty(Tracked< std::vector< T >> &t, size_t i)
Clear dirty flag at index of Tracked vector.
Tracked< std::vector< double > > TrackedDoubleVector
Vec val_
the vector we're wrapping
size_t capacity() const
Pass through capacity method to underlying vector.
value_type & at_mutable(size_t i)
Synonym for.
void clear_dirty()
Resets modification status.
const T & operator*() const
Dereference to underlying object.
friend bool operator==(const Tracked &lhs, const Tracked &rhs)
const_iterator end() const
Pass through to const end method of underlying collection.
std::vector< uint64_t > dirty_
dirty status for each entry
bool is_all_dirty(const Tracked< std::vector< T >> &t)
Return global dirty flag of Tracked vector.
bool size_dirty_
dirty status for size of vector
std::vector< T > & get_mut()
Get non-const reference to underlying vector; mark all elements as dirty immediately.
const T * operator->() const
Pass through to underlying object.
TrackedExtra< T, Tracked< T > > Base
Flag for modification status.
Tracked< uint64_t > TrackedU64
std::vector< T > & get_mutable()
Get non-const reference to underlying vector; mark all elements as dirty immediately.
T & get_mut()
Get a non-const ref to underlying object.
bool empty()
Pass through to empty method of underlying collection.
Tracked< int64_t > TrackedI64
Tracked< double > TrackedDouble
Tracked< uint8_t > TrackedU8
const T & get() const
Get a const ref to the underlying object.
value_type & back()
Equivalent to non-const back method of underlying collection.
Tracked< typename std::decay< T >::type > track(T &&val)
Create a tracked version of a given object.
friend void swap(Tracked &lhs, Tracked &rhs)
Implement swap for tracked vectors.
Tracked< char > TrackedChar
bool is_dirty() const
Has anything about this vector changed?
Tracked & operator=(const std::vector< T > &val)
Set entire vector to new vector.
Used internally by Tracked.
Tracked< float > TrackedFloat
Tracked< uint16_t > TrackedU16
void clear()
Pass through clear method to underlying vector.
void push_back(const value_type &value)
Pass through push_back method to underlying vector.
#define MADARA_MAKE_INCDEC_OP(op)
const_iterator begin() const
Pass through to const begin method of underlying collection.
void modify()
Treat object as modified, even if it isn't.
void reserve(size_t count)
Pass through reserve method to underlying vector.
Tracked< unsigned int > TrackedUInt
Tracked< unsigned char > TrackedUChar
value_type & front_mut()
Equivalent to non-const front method of underlying collection.
friend bool operator<(const Tracked &lhs, const Tracked &rhs)
const T & get_value(const T &t)
Fallback definition of get_value; simply passes through the value.
const Impl & impl() const
Tracked< int8_t > TrackedI8
Provides functions and classes for the distributed knowledge base.
const std::vector< T > & operator*() const
Dereference to const reference.
void set(T val)
Set the underlying object, and mark as modified.
void modify(size_t i)
Mark i-th element as modified.
size_t size()
Pass through to size method of underlying collection.
Tracked(T val)
Create a Tracked version of the given object, which is copied.
void set_value(T &t, const T &v)
Fallback definition of set_value; simply passes through the value.
const_iterator cbegin() const
Pass through to cbegin method of underlying collection.
static size_t to_dirty_size(size_t i)
Get size of dirty_ vector needed to fit i-th dirty bit.
void clear_dirty(size_t i)
Clear i-th element modification status.
std::vector< T >::value_type value_type
friend bool operator>=(const Tracked &lhs, const Tracked &rhs)
Copyright (c) 2015 Carnegie Mellon University.
TrackedCollection< Vec, Tracked< std::vector< T > > > Base
Tracked< std::vector< int64_t > > TrackedIntVector
friend bool operator>(const Tracked &lhs, const Tracked &rhs)
value_type & at_mut(size_t i)
Pass through to non-const version of at method of underlying collection Immediately mark that index a...
Tracked< unsigned short > TrackedUShort
friend void swap(Tracked &lhs, T &rhs)
Implement swap between tracked object, and object of underlying type.
std::vector< T >::const_iterator const_iterator
const value_type & at(size_t i) const
Pass through at method to underlying collection.
const value_type & front() const
Equivalent to front method of underlying collection.
friend bool operator!=(const Tracked &lhs, const Tracked &rhs)