Index retrieval as a function of the boost::multi_index::sequenced<> offset - c++

I am in a situation where I am forced to use a std::vector container as my underlying data structure. I'm trying to exploit boost::multi_index::sequenced<> to shadow the vector offset, and provide a mechanism for richer data queries. I really don't want to unnecessarily copy all the data from one container to another.
In the example code snippet below I have a class BarInterface that manages the insertion and removal of elements to the Bar::foos container. Initially I tried to store references to the vector elements as elements of the typedef boost::multi_index_container, but these are not stable against insertions.
What I'd like to do is have a custom key extractor that is a function of the _bar container and the boost::multi_index::sequenced<> index. So, for example to get the name of an element, I'd find the sequence offset x, and then _bar.foos[x].name. So, I'm really just using boost::multi_index as a proxy for richer queries against a vector of variable size.
struct Foo {
std::string name;
};
struct Bar {
std::vector<Foo> foos;
};
class BarInterface {
public:
struct Dummy {};
BarInterface(Bar& bar) : _bar(bar) {
for (auto& foo : bar.foos) {
_idx.get<1>().insert(Dummy{});
}
}
// Index
struct Name {};
typedef boost::multi_index_container<
Dummy, // We're not actually storing anything here
boost::multi_index::indexed_by<
boost::multi_index::sequenced<>, // Kept inline with vector order!
boost::multi_index::ordered_unique<
boost::multi_index::tag<Name>,
// I need something here that takes the boost::multi_index::sequenced<>
// key to get the offset x, and then returns this->_bar.foos[x].name!
>
>
> MultiIndex;
// Insert
void insert(const Foo& foo) {
_bar.foos.push_back(foo);
_idx.get<1>().insert(Dummy{});
}
// Remove
template <typename T>
void remove(T it) {
_bar.foos.erase(_bar.foos.begin() + std::distance(_idx.begin(), _idx.project<0>(it)));
_idx.erase(_idx.project<0>(it));
}
protected:
Bar& _bar;
MultiIndex _idx;
};
I know that boost::multi_index supports all sorts of key extractors -- for member variables, member functions, global function, etc. However, I can't seem to find an example showing how to generate a key as a function of a boost::multi_index::sequenced<> index. Is this possible, or is there an elegant alternative?

This is extremely brittle and I wouldn't recommend going to production with such code, but since you asked for it:
Live Coliru Demo
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <iostream>
#include <string>
#include <vector>
struct Foo {
std::string name;
};
struct Bar {
std::vector<Foo> foos;
};
class BarInterface;
struct Dummy
{
Dummy(const Foo& x): p(&x){}
Dummy(const Dummy&): p(nullptr){}
const Foo* p;
};
struct NameExtractor
{
NameExtractor(BarInterface* p): p(p){}
using result_type=std::string;
const result_type& operator()(const Dummy& d)const;
BarInterface* p;
};
class BarInterface {
public:
BarInterface(Bar& bar) :
_idx(boost::make_tuple(
boost::tuple<>(),
boost::make_tuple(NameExtractor(this), std::less<std::string>())
)),
_bar(bar)
{
for (auto& foo : bar.foos) {
_idx.get<1>().insert(bar.foos.back());
}
}
// Index
struct Name {};
typedef boost::multi_index_container<
Dummy, // We're not actually storing anything here
boost::multi_index::indexed_by<
boost::multi_index::random_access<>, // Kept inline with vector order!
boost::multi_index::ordered_unique<
boost::multi_index::tag<Name>,
NameExtractor
>
>
> MultiIndex;
// Insert
void insert(const Foo& foo) {
_bar.foos.push_back(foo);
_idx.get<1>().insert(_bar.foos.back());
}
// Remove
template <typename T>
void remove(T it) {
_bar.foos.erase(_bar.foos.begin() + std::distance(_idx.begin(), _idx.project<0>(it)));
_idx.erase(_idx.project<0>(it));
}
void remove(const char* name) {
remove(_idx.get<1>().find(name));
}
void print()const
{
auto key=_idx.get<1>().key_extractor();
for(const auto& d: _idx.get<1>()){
std::cout<<key(d)<<" ";
}
std::cout<<"\n";
}
protected:
friend NameExtractor;
Bar& _bar;
MultiIndex _idx;
};
const NameExtractor::result_type& NameExtractor::operator()(const Dummy& d)const
{
if(d.p){
return d.p->name;
}
else{
std::size_t offset=p->_idx.iterator_to(d)-p->_idx.begin();
return p->_bar.foos[offset].name;
}
}
int main()
{
Bar bar;
BarInterface bi(bar);
bi.insert(Foo{"hello"});
bi.insert(Foo{"bye"});
bi.insert(Foo{"Boost"});
bi.print();
bi.remove("bye");
bi.print();
bi.insert(Foo{"MultiIndex"});
bi.print();
}
Output
Boost bye hello
Boost hello
Boost MultiIndex hello

Related

C++ discourages base class of collection - is there anyway to fake it?

There are no similar concept of (Java) Collection in C++.
I can understand the reason, but I want to know whether there is any way to fake it elegantly.
Example
I have implemented many custom Collections.
They all have Iterator that works correctly, similar to std::vector, std::unordered_set, etc.
They are MyArray<T> , MyBinaryTree<T> and MySet<T>.
Here I will show a working code that show the location I want to fake it.
Let's say that I have 2 levels of program : library and user.
It does only one thing - User commands Library to eat all Orange*s in a bucket.
Library.h
class Library{
public: static void eatAll(const MyArray<Orange*>& bucket);
};
Library.cpp
#include "Orange.h"
void Library::eatAll(const MyArray<Orange*>& bucket){
for(auto orange:bucket){
orange->eaten();
}
}
User.h
MyArray<Orange*> bucket;
Library::eatAll(bucket);
It is OK.
Now, I want Library::eatAll to also support MyBinaryTree<Orange*>, I have some not-so-desirable approaches as below.
My poor solution
1. Java way
Make MyBinaryTree<T> and MyArray<Orange*> (and their iterator) inherit from a new class Collection<T> (and CollectionIterator<T>).
change signature to Library::eatAll(const Collection<T>&)
Disadvantage : performance penalty from "virtual" of some functions in Collection<T>.
2. Template v1
//Library.h
template<class T> void eatAll(const T&t ){
for(auto orange : t){
orange->eaten();
}
}
make eatAll a template function
Disadvantage : The implementation of the eatAll must be in header.
I have to #include orange.h in Library.h.
Sometimes, I really want to just forward declaration.
3. Template v2
//Library.h
template<class T> void eatAll(const T&t ){
for(auto orange : t){
eatAnOrange(orange)
}
}
private: void eatAnOrange(Orange* orange){
//below implementation is inside Library.cpp
orange->eaten();
}
create a middle-man function eatAnOrange
Disadvantage :
Code is less readable, not concise, cause a little maintainability problem.
If there are a lot of other functions e.g. squeezeAll(), I probably have to create a lot of middle-man functions, e.g. squeezeAnOrange().
4. Operator=()
Create converter among the 3 collection classes via implicit constructor.
Disadvantage : Suffer performance from creating a new instance of collection.
//Here is what it will do, internally (roughly speaking)
MyBinaryTree<Orange*> bucket;
Library::eatAll(MyArray<Orange*>(bucket));
I believe my solutions are inelegant.
Are there any solutions that don't suffer the mentioned disadvantage?
Edit:
Both of current answers are elegant than my approaches (thank!), but still has disadvantage :-
- Oliv's requires #include "orange.h" in User.h.
- Richard Hodges's has virtual function calling.
In C++, collections are traversed using the iterator design pattern. The entire STL is designed around this concept. It may fit your needs:
You could define eatAll as a function that accept two iterators:
template<class Iterator,class Sentinel>
void eatAll(Iterator it, Sentinel s){
for (;it!=s;++it)
it->eaten();
}
Or the range like algorithm interface:
template<class Range>
void eatAll(Range& r){
for (auto& v:r)
v.eaten();
}
You will have to define you binary tree as a range (it must implement begin() and end()). Hopefully trees are kind of graph that can be linearised. All the smart work will then go in the iterator implementation!
If you want it truly polymorphic, then we have to deal with 2 things:
the actual type of the container
The fact that the result of dereferencing a map is a pair containing key and value references.
My view is that the answer to this is not to derive from containers, which is limiting, but to create a polymorphic "value iterator", which models all iterators and extracts their values correctly.
Then we can write code like this:
int main()
{
std::vector<Orange> vo {
Orange(), Orange()
};
std::map<int, Orange> mio {
{ 1, Orange() },
{ 2, Orange() },
{ 3, Orange() }
};
std::cout << "vector:\n";
auto first = makePolymorphicValueIterator(vo.begin());
auto last = makePolymorphicValueIterator(vo.end());
do_orange_things(first, last);
std::cout << "\nmap:\n";
first = makePolymorphicValueIterator(mio.begin());
last = makePolymorphicValueIterator(mio.end());
do_orange_things(first, last);
}
To get this:
vector:
Orange
Orange
map:
Orange
Orange
Orange
Here's a minimal, complete implementation:
#include <typeinfo>
#include <memory>
#include <iostream>
#include <vector>
#include <map>
#include <iterator>
// define an orange
struct Orange {
};
// a meta-function to get the type of the value of some iterated value_type
template<class ValueType> struct type_of_value
{
using type = ValueType;
};
// specialise it for maps and unordered maps
template<class K, class V> struct type_of_value<std::pair<K, V>>
{
using type = V;
};
template<class ValueType> using type_of_value_t = typename type_of_value<ValueType>::type;
// function to extract a value from an instance of a value_type
template<class ValueType> struct value_extractor
{
template<class V>
auto& operator()(V&& v) const {
return v;
}
};
// specialised for maps
template<class K, class V> struct value_extractor<std::pair<K, V>>
{
template<class Arg>
auto& operator()(Arg&& v) const {
return std::get<1>(v);
}
};
template<class Iter>
auto extract_value(Iter const& iter) -> auto&
{
using value_type = typename std::iterator_traits<Iter>::value_type;
auto e = value_extractor<value_type> {};
return e(*iter);
}
// a polymorphic (forward only at the moment) iterator
// which delivers the value (in the case of maps) or the element (every other container)
template<class ValueType>
struct PolymorphicValueIterator {
using value_type = type_of_value_t<ValueType>;
private:
struct iterator_details {
std::type_info const &type;
void *address;
};
struct concept {
virtual std::unique_ptr<concept> clone() const = 0;
virtual value_type& invoke_deref() const = 0;
virtual void invoke_next(std::size_t distance = 1) = 0;
virtual iterator_details get_details() = 0;
virtual bool is_equal(const iterator_details &other) const = 0;
virtual ~concept() = default;
};
template<class Iter>
struct model final : concept {
model(Iter iter)
: iter_(iter)
{}
std::unique_ptr<concept> clone() const override
{
return std::make_unique<model>(iter_);
}
virtual value_type& invoke_deref() const override {
return extract_value(iter_);
}
void invoke_next(std::size_t distance = 1) override
{
iter_ = std::next(iter_, distance);
}
iterator_details get_details() override {
return {
typeid(Iter),
std::addressof(iter_)
};
}
bool is_equal(const iterator_details &other) const override {
if (typeid(Iter) != other.type) {
return false;
}
auto pother = reinterpret_cast<Iter const*>(other.address);
Iter const& iother = *pother;
return iter_ == iother;
}
Iter iter_;
};
std::unique_ptr<concept> concept_ptr_;
public:
bool operator==(PolymorphicValueIterator const &r) const {
return concept_ptr_->is_equal(r.concept_ptr_->get_details());
}
bool operator!=(PolymorphicValueIterator const &r) const {
return not concept_ptr_->is_equal(r.concept_ptr_->get_details());
}
PolymorphicValueIterator &operator++() {
concept_ptr_->invoke_next(1);
return *this;
}
value_type& operator*() const {
return concept_ptr_->invoke_deref();
}
template<class Iter>
PolymorphicValueIterator(Iter iter)
{
concept_ptr_ = std::make_unique<model<Iter>>(iter);
}
PolymorphicValueIterator(PolymorphicValueIterator const& r)
: concept_ptr_(r.concept_ptr_->clone())
{}
PolymorphicValueIterator& operator=(PolymorphicValueIterator const& r)
{
concept_ptr_ = r.concept_ptr_->clone();
return *this;
}
};
template<class Iter>
auto makePolymorphicValueIterator(Iter iter)
{
using iter_value_type = typename std::iterator_traits<Iter>::value_type;
using value_type = type_of_value_t<iter_value_type>;
return PolymorphicValueIterator<value_type>(iter);
}
// a test
void do_orange_things(PolymorphicValueIterator<Orange> first, PolymorphicValueIterator<Orange> last)
{
while(first != last) {
std::cout << "Orange\n";
++first;
}
}
int main()
{
std::vector<Orange> vo {
Orange(), Orange()
};
std::map<int, Orange> mio {
{ 1, Orange() },
{ 2, Orange() },
{ 3, Orange() }
};
std::cout << "vector:\n";
auto first = makePolymorphicValueIterator(vo.begin());
auto last = makePolymorphicValueIterator(vo.end());
do_orange_things(first, last);
std::cout << "\nmap:\n";
first = makePolymorphicValueIterator(mio.begin());
last = makePolymorphicValueIterator(mio.end());
do_orange_things(first, last);
}

Java hashmap.get() in c++

In java you could have something like:
Map<Foo, List<Bar>> things;
for(Foo foo : things.getKeySet()){
List<bar> = things.get(foo);
}
Is there an equivalent for c++, maybe in std::map? Thanks for any help.
See std::map and std::vector (ArrayList) and maybe std::unordered_map (HashMap) and std::list (LinkedList)
For example:
#include <map>
#include <vector>
struct Foo {};
struct Bar {};
int main()
{
std::map<Foo, std::vector<Bar>> things;
for(auto& thing: things) {
const Foo& foo = thing.first; // key
std::vector<Bar>& bars = thing.second; // value
// use foo & bars here
}
}
Note: A std::map requires that a comparison operator be defined for user defined types like Foo:
struct Foo
{
int i = 0;
Foo(int i): i(i) {}
// need a comparison operator for ordered containers
bool operator<(const Foo& foo) const { return i < foo.i; }
};

Calling parametrised method on list items with different template parameters

I'm trying to store and manipulate a list of template class objects with different parameter types; the template class has two parametrised methods, one returning the parameter type and a void one accepting it as input.
More specifically, I have a template class defined as follows:
template<typename T>
class Test
{
public:
virtual T a() = 0;
virtual void b(T t) = 0;
};
And different specifications of it, such as:
class TestInt : public Test<int>
{
public:
int a() {
return 1;
}
void b(int t) {
std::cout << t << std::endl;
}
};
class TestString : public Test<std::string>
{
public:
std::string a() {
return "test";
}
void b(std::string t) {
std::cout << t << std::endl;
}
};
I'd like to be able to store in one single list different objects of both TestInt and TestString type and loop through it calling one method as input for the other, as in:
for (auto it = list.begin(); it != list.end(); ++it)
(*it)->b((*it)->a());
I've looked into boost::any but I'm unable to cast the iterator to the specific class, because I don't know the specific parameter type of each stored object. Maybe this cannot be done in a statically typed language as C++, but I was wondering whether there could be a way around it.
Just for the sake of completeness, I'll add that my overall aim is to develop a "parametrised observer", namely being able to define an observer (as with the Observer Pattern) with different parameters: the Test class is the observer class, while the list of different types of observers that I'm trying to properly define is stored within the subject class, which notifies them all through the two methods a() and b().
The virtuals have actually no meaning here, since for each T the signatures are distinct.
So it seems you have Yet Another version of the eternal "how can we emulate virtual functions templates" or "how to create an interface without virtual functions":
Generating an interface without virtual functions?
How to achieve "virtual template function" in C++
The first one basically contains an idea that you could employ here.
Here's an idea of what I'd do:
Live On Coliru
#include <algorithm>
#include <iostream>
namespace mytypes {
template <typename T>
struct Test {
T a() const;
void b(T t) { std::cout << t << std::endl; }
};
template <> int Test<int>::a() const { return 1; }
template <> std::string Test<std::string>::a() const { return "test"; }
using TestInt = Test<int>;
using TestString = Test<std::string>;
}
#include <boost/variant.hpp>
namespace mytypes {
using Value = boost::variant<int, std::string>;
namespace detail {
struct a_f : boost::static_visitor<Value> {
template <typename T>
Value operator()(Test<T> const& o) const { return o.a(); }
};
struct b_f : boost::static_visitor<> {
template <typename T>
void operator()(Test<T>& o, T const& v) const { o.b(v); }
template <typename T, typename V>
void operator()(Test<T>&, V const&) const {
throw std::runtime_error(std::string("type mismatch: ") + __PRETTY_FUNCTION__);
}
};
}
template <typename O>
Value a(O const& obj) {
return boost::apply_visitor(detail::a_f{}, obj);
}
template <typename O, typename V>
void b(O& obj, V const& v) {
boost::apply_visitor(detail::b_f{}, obj, v);
}
}
#include <vector>
int main()
{
using namespace mytypes;
using AnyTest = boost::variant<TestInt, TestString>;
std::vector<AnyTest> list{TestInt(), TestString(), TestInt(), TestString()};
for (auto it = list.begin(); it != list.end(); ++it)
b(*it, a(*it));
}
This prints
1
test
1
test
Bonus Points
If you insist, you can wrap the AnyTest variant into a proper class and have a() and b(...) member functions on that:
Live On Coliru
int main()
{
using namespace mytypes;
std::vector<AnyTest> list{AnyTest(TestInt()), AnyTest(TestString()), AnyTest(TestInt()), AnyTest(TestString())};
for (auto it = list.begin(); it != list.end(); ++it)
it->b(it->a());
}
Expanding on my comment above, the simplest what I can currently think of to achieve what you are trying to do - at least as I understood it from your example code - is the following:
/* Interface for your container, better not forget the destructor! */
struct Test {
virtual void operate(void) = 0;
virtual ~Test() {}
};
/* Implementation hiding actual type */
template<typename T>
struct TestImpl : public T, public Test {
void operate(void) {
T::b(T::a());
}
};
/* Actual code as template policies */
struct IntTest {
int a(void) {
return 42;
}
void b(int value) {
std::cout << value << std::endl;
}
};
struct StringTest {
std::string a(void) {
return "Life? Don't talk to me about life.";
}
void b(std::string value) {
std::cout << value << std::endl;
}
};
You would then need to create a container for objects of class Test and fill it with objects of the respective TestImpl<IntTest>, TestImpl<StringTest>, and so on. To avoid object slicing you need reference or pointer semantics, that is std::vector<std::unique_ptr<Test> > for example.
for (auto it = list.begin(); it != list.end(); ++it) {
(*it)->operate();
}

std::hash for unique ptr in unordered map

I'm trying to hold a polymorphic type as a key in a map.
I came up with the following two structures:
Note that Game is an abstract class and the data structure I use is :
std::unordered_map<gamePtr,int> _allGames;
while gamePtr is a typedef for:
unique_ptr<Game>
template<>
struct std::hash<std::unique_ptr<Game>> {
size_t operator()(std::unique_ptr<Game> game) const {
return (std::hash<string>()(std::to_string(game->firstTeamFinalScore()) + game->firstTeam() + game->secondTeam()));
}
};
struct cmp_games {
bool operator() (std::unique_ptr<Game> game1, std::unique_ptr<Game> game2) const {
return *game1 == *game2;
}
};
The cmp_games comparator seems to work fine but the std::hash does not because it tries to copy a unique_ptr (Which is ofc impossible) and I've no idea how to get over it.
Would love to hear some suggestions (If that is even possible).
EDIT: The comparator also doesn't seem to work properly. how do I make this map work correctly with unique_ptr as a key?
EDIT2:
Came up with:
template<>
struct std::hash<std::unique_ptr<Game>> {
size_t operator()(const std::unique_ptr<Game>& game) const {
return (std::hash<string>()(std::to_string(game->firstTeamFinalScore()) + game->firstTeam() + game->secondTeam()));
}
};
template<>
struct std::equal_to<std::unique_ptr<Game>> {
bool operator() (const std::unique_ptr<Game>& game1,const std::unique_ptr<Game>& game2) const {
return *game1 == *game2;
}
};
Should they be enough?
The standard provides a specilization so that std::hash<unique_ptr<T>> is the same as std::hash<T*>. So provide a specialization for std::hash<Game *>. For example:
#include <iostream>
#include <memory>
#include <unordered_map>
#include <cstdlib>
struct foo
{
foo(unsigned i) : i(i) {}
unsigned i;
};
namespace std {
template<>
struct hash<foo *>
{
size_t operator()(foo const *f) const
{
std::cout << "Hashing foo: " << f->i << '\n';
return f->i;;
}
};
}
int main()
{
std::unordered_map<std::unique_ptr<foo>, int> m;
m.insert(std::make_pair(std::unique_ptr<foo>(new foo(10)), 100));
m.insert(std::make_pair(std::unique_ptr<foo>(new foo(20)), 200));
}
Live demo
Another option is to change your existing std::hash specialization so that it takes the unique_ptr by reference.
size_t operator()(std::unique_ptr<Game> const& game) const
// ^^^^^^ no more copying
EDIT: std::unique_ptr provides comparison operators that compare the managed pointers. If you want the unordered_map to test the Game objects themselves for equality, provide an operator== overload instead of specializing std::equal_to
inline bool operator==(const std::unique_ptr<Game>& game1,
const std::unique_ptr<Game>& game2)
{
return *game1 == *game2;
}
This, in turn, requires that you've provided an equality operator for Game (or you could just add the logic to the function above).
inline bool operator==(Game const& game1, Game const& game2)
{
return // however you want to compare these
}
Pass the game by const reference into std::hash::operator():
template<>
struct std::hash<std::unique_ptr<Game>> {
size_t operator()(const std::unique_ptr<Game>& game) const;
}
The same applies to cmp_games::operator().

C++ member template for boost ptr_vector

I'm trying to write a container class using boost::ptr_vector. Inside the ptr_vector I would like to include different classes. I'm trying to achieve that using static templates, but so far I'm not able to do that. For example, the container class is
class model {
private:
boost::ptr_vector<elem_type> elements;
public:
void insert_element(elem_type *a) {
element_list.push_back(a);
}
};
and what I'm trying to achieve is be able to use different elem_type classes. The code below doesn't satisfy my requirements:
template <typename T>class model {
private:
boost::ptr_vector<T> elements;
public:
void insert_element(T *a) {
element_list.push_back(a);
}
};
because when I initialize the container class I can only use one class as template:
model <elem_type_1> model_thing;
model_thing.insert_element(new elem_type_1)
but not elem_type_2:
model_thing.insert_element(new elem_type_2)//error, of course
It is possible to do something like using templates only on the member?
class model {
private:
template <typename T> boost::ptr_vector<T> elements;
public:
void insert_element(T *a) {
element_list.push_back(a);
}
}; //wrong
So I can call the insert_element on the specific class that I want to insert? Note that I do not want to use virtual members.
Thanks!
Try using a vector of boost::variant:
#include <iostream>
#include <vector>
#include <boost/variant.hpp>
#include <boost/foreach.hpp>
struct Foo
{
Foo(int v=0) : a(v) {}
int a;
};
struct Bar
{
Bar(int v=0) : b(v) {}
int b;
};
struct print_visitor : public boost::static_visitor<>
{
void operator()(const Foo& foo) const
{
std::cout << "Foo " << foo.a << "\n";
}
void operator()(const Bar& bar) const
{
std::cout << "Bar " << bar.b << "\n";
}
};
int main()
{
typedef boost::variant<Foo, Bar> Variant;
std::vector<Variant> bag;
bag.push_back(Foo(123));
bag.push_back(Bar(456));
BOOST_FOREACH(const Variant& element, bag)
{
boost::apply_visitor(print_visitor(), element);
}
}
boost::variant's apply_visitor functions are useful for avoiding excessive casting back to the original type.
Vector is contains elements where each element has the same type as others. If you want to create vector of elements of different classes you could use vector of elements of type boost::any.