diff --git a/inst/include/SharedRList.hpp b/inst/include/SharedRList.hpp index 8f698f0b..7847a0e4 100644 --- a/inst/include/SharedRList.hpp +++ b/inst/include/SharedRList.hpp @@ -14,6 +14,9 @@ namespace Rcpp { } +/** + * Named shared list in shared memory. + */ class SharedList : public SharedRObject { struct tuple { @@ -36,6 +39,7 @@ class SharedList : public SharedRObject { }; + //used during open only std::vector > stash; typedef std::string KeyType; @@ -93,6 +97,7 @@ class SharedList : public SharedRObject { typedef typename SMLIST::iterator iterator; typedef typename SMLIST::const_iterator const_iterator; typedef typename SMLIST::reverse_iterator reverse_iterator; + typedef typename SMLIST::const_reverse_iterator const_reverse_iterator; typedef typename SMLIST::node_type node_type; SharedList() { @@ -136,7 +141,7 @@ class SharedList : public SharedRObject { this->segment.destroy(name.c_str()); } - void set(const std::string& key, SEXP object) { + void set(const key_type& key, SEXP object) { if (this->is_SharedRObject(object)) { SMTYPE type = this->getSharedRObjectType(object); @@ -176,7 +181,7 @@ class SharedList : public SharedRObject { } - SEXP get(const std::string& key) { + SEXP get(const key_type& key) { if ((*this->tlist_m)[key].type == SMTYPE::SMNUMERIC) { @@ -199,10 +204,139 @@ class SharedList : public SharedRObject { // // return static_cast (R_ExternalPtrAddr((*this->tlist_m)[key].data)); } - size_t size() { + + //Iterators: + + inline iterator begin() { + return this->tlist_m->begin(); + } + + inline const_iterator begin() const { + return this->tlist_m->begin(); + } + + inline iterator end() { + return this->tlist_m->end(); + } + + inline const_iterator end() const { + return this->tlist_m->end(); + } + + inline reverse_iterator rbegin() { + return this->tlist_m->rbegin(); + } + + inline const_reverse_iterator rbegin() const { + return this->tlist_m->rbegin(); + } + + inline reverse_iterator rend() { + return this->tlist_m->rend(); + } + + inline const_reverse_iterator rend() const { + return this->tlist_m->rend(); + } + + + //Capacity: + + inline bool empty()const { + return this->tlist_m->empty(); + } + + inline size_type size() const { return this->tlist_m->size(); } + inline size_type max_size() const { + return this->tlist_m->max_size(); + } + + //Element access: + + inline SEXP operator[](const key_type& k) { + return this->get(k); + } + + //Modifiers: + + inline std::pair insert(const value_type& val) { + return this->tlist_m->insert(val); + } + + inline iterator insert(iterator position, const value_type& val) { + return this->tlist_m->insert(position, val); + } + + template + void insert(InputIterator first, InputIterator last) { + this->tlist_m->insert(first, last); + } + + inline void erase(iterator position) { + this->tlist_m->erase(position); + } + + inline size_type erase(const key_type& k) { + return this->tlist_m->erase(k); + } + + inline void erase(iterator first, iterator last) { + this->tlist_m->erase(first, last); + } + + inline void swap(SharedList& x) { + this->tlist_m->swap((*x.tlist_m)); + } + + inline void clear() { + this->tlist_m->clear(); + } + + + //Operations: + + inline iterator find(const key_type& k) { + return this->tlist_m->find(k); + } + + inline const_iterator find(const key_type& k) const { + return this->tlist_m->find(k); + } + + inline size_type count(const key_type& k) const { + return this->tlist_m->count(k); + } + + inline iterator lower_bound(const key_type& k) { + return this->tlist_m->lower_bound(k); + } + + inline const_iterator lower_bound(const key_type& k) const { + return this->tlist_m->lower_bound(k); + } + + inline iterator upper_bound(const key_type& k) { + return this->tlist_m->upper_bound(k); + } + + inline const_iterator upper_bound(const key_type& k) const { + return this->tlist_m->upper_bound(k); + } + + inline std::pair equal_range(const key_type& k) { + return this->tlist_m->equal_range(k); + } + + inline std::pair equal_range(const key_type& k) const { + return this->tlist_m->equal_range(k); + } + + + //Allocator: + }; @@ -213,6 +347,7 @@ namespace Rcpp { template <> SEXP wrap(const SharedList& el) { Rcpp::Rcout << __LINE__ << std::endl; Rcpp::Language call("new", Symbol("SharedList")); + return call.eval(); } @@ -236,6 +371,7 @@ namespace Rcpp { return SharedList((* xptr.get())); } catch (...) { + Rcpp::Rcout << __LINE__ << std::endl; ::Rf_error("supplied object could not be converted to SharedList."); } diff --git a/inst/include/SharedRVector.hpp b/inst/include/SharedRVector.hpp index c4cad19f..45f10fef 100644 --- a/inst/include/SharedRVector.hpp +++ b/inst/include/SharedRVector.hpp @@ -25,425 +25,422 @@ typedef allocator ShmemAllocator //its values from the segment typedef vector REALSMVector; - - - - /** * Wrapper class for std::vector types. If this file is compiled with -DTMB_MODEL, * conversion operators are defined for TMB vector types. */ class SharedVector : public SharedRObject { - managed_shared_memory segment; - REALSMVector* vec_m; - - /** - * @brief friiend comparison operator. - */ - friend bool operator==(const SharedVector& lhs, - const SharedVector& rhs); - - void init(const std::string name) { - - - this->name = name; - shared_memory_object::remove(name.c_str()); - - //Create a new segment with given name and size - segment = managed_shared_memory(create_only, name.c_str(), 65536); - - //Initialize shared memory STL-compatible allocator - const ShmemAllocator alloc_inst(segment.get_segment_manager()); - - //Construct a vector named "MyVector" in shared memory with argument alloc_inst - this->vec_m = segment.construct("REALSMVector")(alloc_inst); - - - } - + managed_shared_memory segment; + REALSMVector* vec_m; + + /** + * @brief friiend comparison operator. + */ + friend bool operator==(const SharedVector& lhs, + const SharedVector& rhs); + + void init(const std::string name) { + + + this->name = name; + shared_memory_object::remove(name.c_str()); + + //Create a new segment with given name and size + segment = managed_shared_memory(create_only, name.c_str(), 65536); + + //Initialize shared memory STL-compatible allocator + const ShmemAllocator alloc_inst(segment.get_segment_manager()); + + //Construct a vector named "MyVector" in shared memory with argument alloc_inst + this->vec_m = segment.construct("REALSMVector")(alloc_inst); + + + } + public: - //Member Types - - typedef typename REALSMVector::value_type value_type; /*!*/ - typedef typename REALSMVector::allocator_type allocator_type; /*!*/ - typedef typename REALSMVector::size_type size_type; /*!*/ - typedef typename REALSMVector::difference_type difference_type; /*!*/ - typedef typename REALSMVector::reference reference; /*!*/ - typedef typename REALSMVector::const_reference const_reference; /*!*/ - typedef typename REALSMVector::pointer pointer; /*!*/ - typedef typename REALSMVector::const_pointer const_pointer; /*!*/ - typedef typename REALSMVector::iterator iterator; /*!*/ - typedef typename REALSMVector::const_iterator const_iterator; /*!*/ - typedef typename REALSMVector::reverse_iterator reverse_iterator; /*!*/ - typedef typename REALSMVector::const_reverse_iterator const_reverse_iterator; /*!*/ - - /** - * Default constructor. - */ - SharedVector() { - // std::stringstream ss; - // - // std::time_t t = std::time(0); - // ss << "REALSMVector_" << t; - // this->init(ss.str().c_str()); - } - - /** - * @brief Constructs the container of size copies of elements with value value. - */ - SharedVector(size_t size, const double& value = double()) { - this->vec_m->resize(size, value); - } - - /** - * @brief Copy constructor. - */ - SharedVector(const SharedVector& other) : + //Member Types + + typedef typename REALSMVector::value_type value_type; /*!*/ + typedef typename REALSMVector::allocator_type allocator_type; /*!*/ + typedef typename REALSMVector::size_type size_type; /*!*/ + typedef typename REALSMVector::difference_type difference_type; /*!*/ + typedef typename REALSMVector::reference reference; /*!*/ + typedef typename REALSMVector::const_reference const_reference; /*!*/ + typedef typename REALSMVector::pointer pointer; /*!*/ + typedef typename REALSMVector::const_pointer const_pointer; /*!*/ + typedef typename REALSMVector::iterator iterator; /*!*/ + typedef typename REALSMVector::const_iterator const_iterator; /*!*/ + typedef typename REALSMVector::reverse_iterator reverse_iterator; /*!*/ + typedef typename REALSMVector::const_reverse_iterator const_reverse_iterator; /*!*/ + + /** + * Default constructor. + */ + SharedVector() { + // std::stringstream ss; + // + // std::time_t t = std::time(0); + // ss << "REALSMVector_" << t; + // this->init(ss.str().c_str()); + } + + /** + * @brief Constructs the container of size copies of elements with value value. + */ + SharedVector(size_t size, const double& value = double()) { + this->vec_m->resize(size, value); + } + + /** + * @brief Copy constructor. + */ + SharedVector(const SharedVector& other) : /*segment(other.segment),*/ vec_m(other.vec_m) { - this->name = other.name; - } - - // /** - // * @brief Copy constructor. - // */ - // SharedVector(const SharedVector& other) { - // this-> - // this->vec_m = other.vec_m;//->resize(other.size()); - // // for (size_t i = 0; i < this->vec_m->size(); i++) { - // // this->vec_m->at(i) = other[i]; - // // } - // } - - /** - * @brief Initialization constructor with std::vector type.. - */ - SharedVector(const std::vector& other) { - this->vec_m->resize(other.size()); - for (size_t i = 0; i < this->vec_m->size(); i++) { - this->vec_m->at(i) = other[i]; - } - } - - virtual void create(const std::string& name) { - this->init(name); - } - - virtual void open(const std::string& name) { - //Open the managed segment - this->name = name; - this->segment = managed_shared_memory(open_only, name.c_str()); - - //Find the vector using the c-string name - this->vec_m = segment.find("REALSMVector").first; - } - - virtual void destroy(const std::string& name) { - this->segment.destroy(name.c_str()); - } - - /** - * @brief Returns a reference to the element at specified location pos. No bounds checking is performed. - */ - inline double& operator[](size_t pos) { - return (*this->vec_m)[pos]; - } - - /** - * @brief Returns a constant reference to the element at specified location pos. No bounds checking is performed. - */ - inline const double& operator[](size_t n) const { - return (*this->vec_m)[n]; - } - - /** - * @brief Returns a reference to the element at specified location pos. Bounds checking is performed. - */ - inline double& at(size_t n) { - return this->vec_m->at(n); - } - - /** - * @brief Returns a constant reference to the element at specified location pos. Bounds checking is performed. - */ - inline const double& at(size_t n) const { - return this->vec_m->at(n); - } - + this->name = other.name; + } + + // /** + // * @brief Copy constructor. + // */ + // SharedVector(const SharedVector& other) { + // this-> + // this->vec_m = other.vec_m;//->resize(other.size()); + // // for (size_t i = 0; i < this->vec_m->size(); i++) { + // // this->vec_m->at(i) = other[i]; + // // } + // } + + /** + * @brief Initialization constructor with std::vector type.. + */ + SharedVector(const std::vector& other) { + this->vec_m->resize(other.size()); + for (size_t i = 0; i < this->vec_m->size(); i++) { + this->vec_m->at(i) = other[i]; + } + } + + virtual void create(const std::string& name) { + this->init(name); + } + + virtual void open(const std::string& name) { + //Open the managed segment + this->name = name; + this->segment = managed_shared_memory(open_only, name.c_str()); + + //Find the vector using the c-string name + this->vec_m = segment.find("REALSMVector").first; + } + + virtual void destroy(const std::string& name) { + this->segment.destroy(name.c_str()); + } + + /** + * @brief Returns a reference to the element at specified location pos. No bounds checking is performed. + */ + inline double& operator[](size_t pos) { + return (*this->vec_m)[pos]; + } + + /** + * @brief Returns a constant reference to the element at specified location pos. No bounds checking is performed. + */ + inline const double& operator[](size_t n) const { + return (*this->vec_m)[n]; + } + + /** + * @brief Returns a reference to the element at specified location pos. Bounds checking is performed. + */ + inline double& at(size_t n) { + return this->vec_m->at(n); + } + + /** + * @brief Returns a constant reference to the element at specified location pos. Bounds checking is performed. + */ + inline const double& at(size_t n) const { + return this->vec_m->at(n); + } + /** * @brief Set an element at specified location pos. */ inline void set(size_t pos, double value) { - (*this->vec_m)[pos] = value; + (*this->vec_m)[pos] = value; } /** * @brief Set an element at specified location pos. */ inline double get(size_t pos) { - return this->vec_m->at(pos); - } - - /** - * @brief Returns a reference to the first element in the container. - */ - reference front() { - return this->vec_m->front(); - } - - /** - * @brief Returns a constant reference to the first element in the container. - */ - const_reference front() const { - return this->vec_m->front(); - } - - /** - * @brief Returns a reference to the last element in the container. - */ - reference back() { - return this->vec_m->back(); - } - - /** - * @brief Returns a constant reference to the last element in the container. - */ - const_reference back() const { - return this->vec_m->back(); - } - - /** - * @brief Returns a pointer to the underlying data array. - */ - pointer data() { - return this->vec_m->data(); - } - - /** - * @brief Returns a constant pointer to the underlying data array. - */ - - const_pointer data() const { - return this->vec_m->data(); - } - - //iterators - - /** - * @brief Returns an iterator to the first element of the vector. - */ - iterator begin() { - return this->vec_m->begin(); - } - - /** - * @brief Returns an iterator to the element following the last element of the vector. - */ - iterator end() { - return this->vec_m->end(); - } - - /** - * @brief Returns an constant iterator to the first element of the vector. - */ - const_iterator begin() const { - return this->vec_m->begin(); - } - - /** - * @brief Returns an const iterator to the element following the last element of the vector. - */ - const_iterator end() const { - return this->vec_m->end(); - } - - /** - * @brief Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. - */ - reverse_iterator rbegin() { - return this->vec_m->rbegin(); - } - - /** - * @brief Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. - */ - reverse_iterator rend() { - return this->vec_m->rend(); - } - - /** - * @brief Returns a constant reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. - */ - const_reverse_iterator rbegin() const { - return this->vec_m->rbegin(); - } - - /** - * @brief Returns a constant reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. - */ - const_reverse_iterator rend() const { - return this->vec_m->rend(); - } - - //capacity - - /** - * @brief Checks whether the container is empty. - */ - bool empty() { - return this->vec_m->empty(); - } - - /** - * @brief Returns the number of elements. - */ - size_type size() const { - return this->vec_m->size(); - } - - /** - * @brief Returns the maximum possible number of elements. - */ - size_type max_size() const { - return this->vec_m->max_size(); - } - - /** - * @brief Reserves storage. - */ - void reserve(size_type cap) { - this->vec_m->reserve(cap); - } - - /** - * @brief Returns the number of elements that can be held in currently allocated storage. - */ - size_type capacity() { - return this->vec_m->capacity(); - } - - /** - * @brief Reduces memory usage by freeing unused memory. - */ - void shrink_to_fit() { - this->vec_m->shrink_to_fit(); - } - - //modifiers - - /** - * @brief Clears the contents. - */ - void clear() { - this->vec_m->clear(); - } - - /** - * @brief Inserts value before pos. - */ - iterator insert(const_iterator pos, const double& value) { - return this->vec_m->insert(pos, value); - } - - /** - * @brief Inserts count copies of the value before pos. - */ - iterator insert(const_iterator pos, size_type count, const double& value) { - return this->vec_m->insert(pos, count, value); - } - - /** - * @brief Inserts elements from range [first, last) before pos. - */ - template< class InputIt > - iterator insert(const_iterator pos, InputIt first, InputIt last) { - return this->vec_m->insert(pos, first, last); - } - - /** - * @brief Inserts elements from initializer list ilist before pos. - */ - - iterator insert(const_iterator pos, std::initializer_list ilist) { - return this->vec_m->insert(pos, ilist); - } - - /** - * @brief Constructs element in-place. - */ - template< class... Args > - iterator emplace(const_iterator pos, Args&&... args) { - return this->vec_m->emplace(pos, std::forward(args)...); - } - - /** - * @brief Removes the element at pos. - */ - iterator erase(iterator pos) { - return this->vec_m->erase(pos); - } - - /** - * @brief Removes the elements in the range [first, last). - */ - iterator erase(iterator first, iterator last) { - return this->vec_m->erase(first, last); - } - - /** - * @brief Adds an element to the end. - */ - void push_back(const double&& value) { - this->vec_m->push_back(value); - } - - /** - * @brief Constructs an element in-place at the end. - */ - template< class... Args > - void emplace_back(Args&&... args) { - this->vec_m->emplace_back(std::forward(args)...); - } - - /** - * @brief Removes the last element. - */ - void pop_back() { - this->vec_m->pop_back(); - } - - /** - * @brief Changes the number of elements stored. - */ - void resize(size_t s) { - this->vec_m->resize(s); - } - - // /** - // * @brief Changes the number of elements stored. - // */ - // void resize(size_t s, const value_type& value) { - // this->vec_m->resize(s, value); - // } - // - /** - * @brief Swaps the contents. - */ - void swap(SharedVector& other) { - this->vec_m->swap((*other.vec_m)); - } - - - + return this->vec_m->at(pos); + } + + /** + * @brief Returns a reference to the first element in the container. + */ + inline reference front() { + return this->vec_m->front(); + } + + /** + * @brief Returns a constant reference to the first element in the container. + */ + inline const_reference front() const { + return this->vec_m->front(); + } + + /** + * @brief Returns a reference to the last element in the container. + */ + inline reference back() { + return this->vec_m->back(); + } + + /** + * @brief Returns a constant reference to the last element in the container. + */ + inline const_reference back() const { + return this->vec_m->back(); + } + + /** + * @brief Returns a pointer to the underlying data array. + */ + inline pointer data() { + return this->vec_m->data(); + } + + /** + * @brief Returns a constant pointer to the underlying data array. + */ + + inline const_pointer data() const { + return this->vec_m->data(); + } + + //iterators + + /** + * @brief Returns an iterator to the first element of the vector. + */ + inline iterator begin() { + return this->vec_m->begin(); + } + + /** + * @brief Returns an iterator to the element following the last element of the vector. + */ + inline iterator end() { + return this->vec_m->end(); + } + + /** + * @brief Returns an constant iterator to the first element of the vector. + */ + inline const_iterator begin() const { + return this->vec_m->begin(); + } + + /** + * @brief Returns an const iterator to the element following the last element of the vector. + */ + inline const_iterator end() const { + return this->vec_m->end(); + } + + /** + * @brief Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. + */ + inline reverse_iterator rbegin() { + return this->vec_m->rbegin(); + } + + /** + * @brief Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. + */ + inline reverse_iterator rend() { + return this->vec_m->rend(); + } + + /** + * @brief Returns a constant reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. + */ + inline const_reverse_iterator rbegin() const { + return this->vec_m->rbegin(); + } + + /** + * @brief Returns a constant reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. + */ + inline const_reverse_iterator rend() const { + return this->vec_m->rend(); + } + + //capacity + + /** + * @brief Checks whether the container is empty. + */ + inline bool empty() { + return this->vec_m->empty(); + } + + /** + * @brief Returns the number of elements. + */ + inline size_type size() const { + return this->vec_m->size(); + } + + /** + * @brief Returns the maximum possible number of elements. + */ + inline size_type max_size() const { + return this->vec_m->max_size(); + } + + /** + * @brief Reserves storage. + */ + inline void reserve(size_type cap) { + this->vec_m->reserve(cap); + } + + /** + * @brief Returns the number of elements that can be held in currently allocated storage. + */ + inline size_type capacity() { + return this->vec_m->capacity(); + } + + /** + * @brief Reduces memory usage by freeing unused memory. + */ + inline void shrink_to_fit() { + this->vec_m->shrink_to_fit(); + } + + //modifiers + + /** + * @brief Clears the contents. + */ + inline void clear() { + this->vec_m->clear(); + } + + /** + * @brief Inserts value before pos. + */ + inline iterator insert(const_iterator pos, const double& value) { + return this->vec_m->insert(pos, value); + } + + /** + * @brief Inserts count copies of the value before pos. + */ + inline iterator insert(const_iterator pos, size_type count, const double& value) { + return this->vec_m->insert(pos, count, value); + } + + /** + * @brief Inserts elements from range [first, last) before pos. + */ + template< class InputIt > + iterator insert(const_iterator pos, InputIt first, InputIt last) { + return this->vec_m->insert(pos, first, last); + } + + /** + * @brief Inserts elements from initializer list ilist before pos. + */ + + inline iterator insert(const_iterator pos, std::initializer_list ilist) { + return this->vec_m->insert(pos, ilist); + } + + /** + * @brief Constructs element in-place. + */ + template< class... Args > + iterator emplace(const_iterator pos, Args&&... args) { + return this->vec_m->emplace(pos, std::forward(args)...); + } + + /** + * @brief Removes the element at pos. + */ + inline iterator erase(iterator pos) { + return this->vec_m->erase(pos); + } + + /** + * @brief Removes the elements in the range [first, last). + */ + inline iterator erase(iterator first, iterator last) { + return this->vec_m->erase(first, last); + } + + /** + * @brief Adds an element to the end. + */ + inline void push_back(const double&& value) { + this->vec_m->push_back(value); + } + + /** + * @brief Constructs an element in-place at the end. + */ + template< class... Args > + void emplace_back(Args&&... args) { + this->vec_m->emplace_back(std::forward(args)...); + } + + /** + * @brief Removes the last element. + */ + inline void pop_back() { + this->vec_m->pop_back(); + } + + /** + * @brief Changes the number of elements stored. + */ + inline void resize(size_t s) { + this->vec_m->resize(s); + } + + // /** + // * @brief Changes the number of elements stored. + // */ + // void resize(size_t s, const value_type& value) { + // this->vec_m->resize(s, value); + // } + // + + /** + * @brief Swaps the contents. + */ + inline void swap(SharedVector& other) { + this->vec_m->swap((*other.vec_m)); + } + + + private: - + }; // end fims::Vector class /** * @brief Comparison operator. */ bool operator==(const SharedVector& lhs, - const SharedVector& rhs) { - return (*lhs.vec_m) == (*rhs.vec_m); + const SharedVector& rhs) { + return (*lhs.vec_m) == (*rhs.vec_m); } // // namespace Rcpp { @@ -479,12 +476,12 @@ bool operator==(const SharedVector& lhs, // // } -SharedVector sm_vector(){ +SharedVector sm_vector() { return SharedVector(); } -SharedVector as_shared_vector(SEXP s){ - return Rcpp::as(s); +SharedVector as_shared_vector(SEXP s) { + return Rcpp::as(s); } RCPP_EXPOSED_CLASS(SharedVector)