Question Synopsis
Given a std::vector<T>, how can I create a view that exposes the interface of a std::vector<std::pair<T, T>>, where each pair consists of two consecutive elements in the underlying vector?
Details
The goal is to create multiple container abstractions over the same storage, which is a std::vector<T>. The type T is some sort of discriminated union, à la Boost Variant. The storage requirement is given, otherwise I would simply use a std::vector<std::pair<T, T>>. The views over the storage I would like to support are sets (unique elements) and tables (associative array, unique keys). While the former is straight-forward by ensuring the set uniqueness property, the latter requires handling keys and values.
To support associative array semantics over a std::vector<T>, I am currently thinking that the best way would be to create a view of the form std::vector<std::pair<T, T>>, and that this view would allow me use STL algorithms to maintain the required properties. Does this sound like a good strategy? Are there any other ideas?
Related
If I had an iterator i that goes over every even element and iterator j that goes through every odd element, Boost's zip iterator comes to mind, which would enable iteration in (i,j) pairs. But my use case is slightly different in that I do not have two separate containers.
It seems that Boost's iterator_facade is indeed what I want. Here is a toy example (with rough edges):
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/iterator/iterator_facade.hpp>
template <typename Value>
class pair_iterator
: public boost::iterator_facade<
pair_iterator<Value>
, Value
, boost::random_access_traversal_tag
, std::pair<Value&, Value&>
, typename std::vector<Value>::difference_type
>
{
public:
typedef std::vector<Value> vector_type;
typedef typename vector_type::difference_type difference_type;
typedef typename vector_type::iterator iterator;
pair_iterator()
: i_(0)
{
}
explicit pair_iterator(iterator i)
: i_(i)
{
}
private:
friend class boost::iterator_core_access;
bool equal(pair_iterator<Value> const& other) const
{
return i_ == other.i_;
}
void increment()
{
++i_;
++i_;
}
std::pair<Value&, Value&> dereference() const
{
return { std::ref(*i_), std::ref(*(i_ + 1)) };
}
void advance(difference_type n)
{
i_ += n << 1;
}
difference_type distance_to(pair_iterator<Value> const& other) const
{
return other.i_ - i_;
}
iterator i_;
};
int main()
{
typedef pair_iterator<int> int_map_iterator;
std::vector<int> v{2, 20, 3, 30, 5, 50, 7, 70};
int_map_iterator first(v.begin());
int_map_iterator last(v.end());
std::for_each(first + 1, last,
[](std::pair<int&, int&> p)
{
std::cout
<< p.first << " -> "
<< p.second << std::endl;
});
return 0;
}
The output is:
3 -> 30
5 -> 50
7 -> 70
Issues
Conversion from iterator to const_iterator has not yet been addressed by this example.
The iterator only works when the underlying vector has even size and needs a more conservative implementation of dereference().
The first thing to note is that you won't be able to expose a std::pair<T const, T>& as a means to modify the objects. What may be sufficantly close, however, is a std::pair<T const, T&> as you'll only be able to change the second part.
With this out of the way it seems you need
An iterator type which skips every other value and is used to iterate over the keys (elements with even indices) and the values (elements with odd indices).
Something like a "zip iterator" which takes two iterators and exposes a std::pair<T const, T&> obtained from them.
Related
Suppose I have a vector named spot_deals of SpotDeal that is a class:
class SpotDeal
{
public:
int deal_id_; // primary key, and vector is sorted by id
string ccy_pair_; // ccy pair, e.g. GBPUSD, AUDUSD
double amount_;
}
Say I need to pass two subset of spot_deals to a function foo for some computation. I could make copies, however, that would cost memory and time. Actually foo only needs iterators of deals. So can I make 2 iterators of vector<SpotDeal>, namely it1 and it2 and pass them to foo?
The two subset of spot_deals could be filtered by ccy_pair_, e.g. deals of GBPUSD and AUDUSD, or by other conditions. So I'm looking for a way to define an iterator defined by a vector and a lambda function (could equivalently be a functor though).
Is there a way to write a helper function make_filtered_iterator so that I can have something like below?
auto it1 = make_filtered_iterator(spot_deals, filter_lambda1);
auto it2 = make_filtered_iterator(spot_deals, filter_lambda2);
foo(it1, it2);
The answer is certainly "yes." C++ iterators in the STL style can be made to do all sorts of tricks. A common but basic one is making an iterator for std::map which when dereferenced gives only the key or the value.
In your particular case, a simple implementation might be like this:
template <typename BaseIterator>
struct filtered_iterator : BaseIterator
{
typedef std::function<bool (const value_type&)> filter_type;
filtered_iterator() = default;
filtered_iterator(filter_type filter, BaseIterator base, BaseIterator end = {})
: BaseIterator(base), _end(end), _filter(filter_type) {
while (*this != _end && !_filter(**this)) {
++*this;
}
}
filtered_iterator& operator++() {
do {
BaseIterator::operator++();
} while (*this != _end && !_filter(**this));
}
filtered_iterator operator++(int) {
filtered_iterator copy = *this;
++*this;
return copy;
}
private:
BaseIterator _end;
filter_type _filter;
};
template <typename BaseIterator>
filtered_iterator<BaseIterator> make_filtered_iterator(
typename filtered_iterator<BaseIterator>::filter_type filter,
BaseIterator base, BaseIterator end = {}) {
return {filter, base, end};
}
I set a default value for end because typically you can use a default-constructed iterator for that. But in some cases you might want to filter only a subset of the container, in which case specifying the end makes it easy.
Yes, it would be possible to create an iterator type. However, I suspect your question is an example of an XY problem (https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - you want to find a way to operate differently on two subsets of a vector (X), have decided the solution must involve implementing a special-purpose iterator (Y), and have asked how to do Y rather than X. I am going to provide an option to do X without needing to do Y.
I suggest it would be easier to use standard algorithm std::stable_partition() to separate the container into two ranges.
auto false_partition = std::stable_partition(your_vector.begin(), your_vector.end(), your_filter);
The begin() and end() iterators of the vector do not change (i.e. are not invalidated), but the elements between them are reorganised into two ranges, such that the elements for which your_filter returns true precedes the set of elements for which your_filter returns false. false_partition is therefore simultaneously the "past the end" iterator for the first range, and the beginning of the second range. The order of elements in each range is the same as in the original vector.
These can be used as follows
// a loop to operates on the elements for which your_filter returned true
for (auto i = your_vector.begin(); i != false_partition; ++i)
{
// do whatever
}
// a loop to operates on the elements for which your_filter returned false
for (auto i = false_partition; i != your_vector.end(); ++i)
{
// do whatever
}
Before C++11, the auto keyword can be replaced with appropriate iterator types (e.g. std::vector<int>::iterator or std::vector<int>::const_iterator, depending on whether you want the elements to be changed using the iterators).
I may suggest viewers of this question get a crash course with Eric Nibbler's rangev3 because that's the paradigm adopted for C++20 standard lib.
https://github.com/ericniebler/range-v3
How to do your filter iteration:
for (auto element : spot_deals | views::filter([](auto i) { return condition(i); }))
{ //....
}
I wouldn't use iterators pointing to your original vector, because they cannot convey the size of your subsets. (Basically, you would need one more iterator per subset to represent the end of the subsets.) As of C++20, I would work with ranges from the Ranges library, as mentioned in v.oddou's answer. More specifically, for your use case, I would use the range adaptor std::views::filter as follows:
auto gbpusd = [](const auto& sd) { return sd.ccy_pair_ == "GBPUSD"; };
auto audusd = [](const auto& sd) { return sd.ccy_pair_ == "AUDUSD"; };
auto range1 = spot_deals | std::views::filter(gbpusd);
auto range2 = spot_deals | std::views::filter(audusd);
foo(range1, range2);
This solution does not create temporary vectors for the filtered spot deals, because the view adaptors create ranges that don't contain elements. The resulting ranges range1 and range2 are just views over the vector spot_deals, but with customized iteration behaviors.
The declaration of foo() is a bit tricky, because the data types of the ranges are quite complex. I would therefore use the placeholder type auto for the function parameters and thus make foo() a function template:
void foo(auto& r1, auto& r2) {
for (auto const& sd : r1)
std::cout << sd.deal_id_ << std::endl;
for (auto const& sd : r2)
std::cout << sd.amount_ << std::endl;
}
(Alternatively, you could pass spot_deals by reference to foo() and declare the filtered ranges inside foo().)
Code on Wandbox
By chance, I've recently worked on this exact problem. As it turns out, filtering is the most complicated of quite some operations on a container, and also contains the most pitfalls.
template<typename Range, typename Pred>
class filter
{
public:
friend class const_iterator;
class const_iterator : public std::iterator_traits<typename Range::const_iterator>
{
using underlying = typename Range::const_iterator;
public:
auto operator*() {return *u;}
const_iterator& operator++()
{
++u;
normalize();
return *this;
}
const_iterator operator++(int)
{
auto t = *this;
u++;
normalize();
return t;
}
bool operator==(const const_iterator& rhs) const {return u == rhs.u;}
bool operator!=(const const_iterator& rhs) const {return !(*this == rhs);}
private:
friend filter;
const_iterator(underlying u, const filter& f) : u{std::move(u)}, f{f} {normalize();}
void normalize()
{
for(; u != f.r.end() && !f.p(*u); u++);
}
underlying u;
const filter& f;
};
filter(const Range& r, const Pred& p) : r{r}, p{p} {}
auto begin() const {return const_iterator{r.begin(), *this};}
auto end() const {return const_iterator{r.end(), *this};}
private:
const Range& r;
Pred p;
};
We use it as (with c++17 guide)
vector<int> v{1, 2, 3, 4, 5};
auto f = filter(v, [](int x){return x & 1;});
for(auto i : f)
// all i in v that is odd
Let me explain the pitfalls:
The first element might be filtered out, *r.begin() might not be an element in the filtered range. This means the iterator has to be checked on construction.
r.end() might be invalidated without invalidating other iterators, that is to say, saving a copy of r.end() in any iterators to compare with is a logic error.
operator== is not very straightforward, two underlying iterators referring to different elements in the original range might be referring to the same element in the filtered view because filtered out elements doesn't count as an element.
I have classes that behave like a lazy container, generating values on the fly. Then in some cases, I would like to filter the values. Boost::range::adaptors::filtered seems to be well-suited. However it does not keep any reference to the "range", it just store the begin/end iterators.
The following code mimics my use case. But it does not work: the container is destroyed before r is used.
#include <iostream>
#include <vector>
#include <boost/range/adaptor/filtered.hpp>
#define PING() std::cerr << __PRETTY_FUNCTION__ << '\n'
using ints = std::vector<int>;
struct container
{
container() { PING(); }
~container() { PING(); }
using value_type = typename ints::value_type;
using iterator = typename ints::iterator;
using const_iterator = typename ints::const_iterator;
iterator begin() { PING(); return std::begin(c_); }
iterator end() { PING(); return std::end(c_); }
const_iterator begin() const { PING(); return std::cbegin(c_); }
const_iterator end() const { PING(); return std::cend(c_); }
ints c_ = { 1, 2, 3, 4, 5 };
};
int main()
{
auto r = container{} | boost::adaptors::filtered([](auto&& v) { return v % 2; });
std::cerr << "Loop\n";
for (auto i: r)
std::cout << i << '\n';
}
It results in (live code):
container::container()
const_iterator container::begin() const
const_iterator container::end() const
const_iterator container::end() const
const_iterator container::end() const
container::~container()
Loop
1
3
5
Is there a simple way to ensure that everybody lives as long as I need it? Of course in main I could declare a variable to store the container{}, but that's inadequate for my real world use where this container is actually obtained by querying some objects ; I don't want the client side to have to deal with this.
It seems that the simplest would be to rewrite some version of filtered that would keep a copy of the range, but I'd like to look for a solution that would avoid writing too much code. And I'm really looking for a Range-v2 solution: it's probably too soon for me to depend upon Range-v3.
So this is nasty.
The trick is you have to store the container first, then apply the pipe to the stored container, then pretend to be that range-like.
template<class X>struct store{ X data; };
template<class Src, class Range>
struct save_src_range:
private store<Src>,
Range
{
// boilerplate for copy/move goes here (TODO)
template<class S, class RangeFactory>
save_src_range( S&& s, RangeFactory&& f ):
store<Src>{std::forward<S>(s)},
Range( std::forward<RangeFactory>(f)(this->data) )
{}
};
now that needs to be gussed up with a deducing creation function and the like.
Next, we need a syntactically pretty way to insert that capability into the existing syntax.
One approach is like:
keep_a_copy( source ) | boost::adapters::filter( ... blah ... )
where we do some judo and make it work magically, maybe even after chaining.
Or
source | keep_source_copy( boost::adapters::filter( ... blah ... ) )
which is a bit easier I think.
I took a stab at it, and it is modestly painful, but I don't see anything fundamentally impossible. It definitely involves writing too much code.
I created a simple bidirectional map class that works by internally storing two std::map instances, with opposite key/value types, and providing a user-friendly interface:
template<class T1, class T2> class Bimap
{
std::map<T1, T2> map1;
std::map<T2, T1> map2;
// ...
};
Is there a more efficient method of implementing a bidirectional map that doesn't require twice the memory?
How is a bimap usually implemented?
EDIT:
Should bimap element be mutable or immutable? (Changing one element in map1 should change the key in map2, but keys are const and that's impossible - what's the solution?)
Ownership of elements is also another problem: when a user inserts a key-value pair in the bimap, the bimap should make a copy of that key-value pair and store it, then the internal second map (with inverted key/value) should not copy but point to the original pair. How can this be achieved?
EDIT 2:
I've posted a possible implementation I made on Code Review.
There is a certain problem with double-storing your data in all simple implementations of a bimap. If you can break it down to a bimap of pointers from outside, then you can readily ignore this and simply keep both maps of the form std::map<A*,B*> like Arkaitz Jimenez already suggested (though contrary to his answer you have to care about the storage from outside to avoid a A->A* lookup). But if you have the pointers anyway, why not simply store a std::pair<A,B> at the point where you would otherwise store A and B separately?
It would be nice to have std::map<A,B*> instead of std::map<A*,B*> as this would allow for example the lookup of an element associated to an string by a newly created string with the same content instead of the pointer to the original string that created the pair. But it is customary to store a full copy of the key with every entry and only rely on the hash to find the right bucket. This way the returned item will be the correct one even in the case of a hash-collision...
If you want to have it quick and dirty though, there is this
hackish solution:
Create two maps std::map<size_t, A> mapA and std::map<size_t, B> mapB. Upon insertion hash both elements that are to be inserted to get the keys to the respective maps.
void insert(const A &a, const B &b) {
size_t hashA = std::hash<A>(a);
size_t hashB = std::hash<B>(b);
mapA.insert({hashB, a});
mapB.insert({hashA, b});
}
Lookup is implemented analogously.
Using a multimap instead of a map and verifying every element you get with a lookup in the respectively other map (get candidate b from mapA, hash b and look in mapB if it matches the wanted key, iterate to the next candidate b otherwise) this is a valid implementation - but still hackish in my opinion...
You can get a much nicer solution by using the copies of the elements that are used to compare the entries (see above) as only storage. It is a bit harder to get your head around that though. To elaborate:
a nicer solution:
Create two sets of pairs as std::set<pair<A, B*>> and std::set<pair<B, A*>> and overload the operator< and operator== to only take the first element of the pairs into account (or provide an corresponding comparion class). It is necessary to create sets of pairs instead of maps (which internally look similarly) because we need a guarantee that A and B will be at constant positions in memory. Upon insertion of an pair<A, B> we split it into two elements that fit into the above sets.
std::set<pair<B, A*>> mapA;
std::set<pair<A, B*>> mapB;
void insert(const A &a, const B &b) {
auto aitr = mapA.insert({b, nullptr}).first; // creates first pair
B *bp = &(aitr->first); // get pointer of our stored copy of b
auto bitr = mapB.insert({a, bp}).first;
// insert second pair {a, pointer_to_b}
A *ap = &(bitr->first); // update pointer in mapA to point to a
aitr->second = ap;
}
Lookup can now simply be done by a simple std::set lookup and a pointer dereference.
This nicer solution is similar to the solution that boost uses - even though they use some annonymized pointers as second elements of the pairs and thus have to use reinterpret_casts.
Note that the .second part of the pairs need to be mutable (so I'm not sure std::pair can be used), or you have to add another layer of abstraction (std::set<pair<B, A**>> mapA) even for this simple insertion. In both solutions you need temporary elements to return non-const references to elements.
It would be more efficient to store all elements in a vector and have 2 maps of <T1*,T2*> and <T2*,T1*> that way you would not have everything copied twice.
The way I see it you are trying to store 2 things, elements themselves and the relationship between them, if you are aiming to scalar types you could leave it as is 2 maps, but if you aim to treat complex types it makes more sense to separate the storage from the relationships, and handle relationships outside the storage.
Boost Bimap makes use of Boost Mutant Idiom.
From the linked wikipedia page:
Boost mutant idiom makes use of reinterpret_cast and depends heavily on assumption that the memory layouts of two different structures with identical data members (types and order) are interchangeable. Although the C++ standard does not guarantee this property, virtually all the compilers satisfy it.
template <class Pair>
struct Reverse
{
typedef typename Pair::first_type second_type;
typedef typename Pair::second_type first_type;
second_type second;
first_type first;
};
template <class Pair>
Reverse<Pair> & mutate(Pair & p)
{
return reinterpret_cast<Reverse<Pair> &>(p);
}
int main(void)
{
std::pair<double, int> p(1.34, 5);
std::cout << "p.first = " << p.first << ", p.second = " << p.second << std::endl;
std::cout << "mutate(p).first = " << mutate(p).first << ", mutate(p).second = " << mutate(p).second << std::endl;
}
The implementation in boost sources is of course fairly hairier.
If you create a set of pairs to your types std::set<std::pair<X,Y>> you pretty much have your functionallity implemented and rules about mutabillity and constness preset (OK maybe the settings aren't what you want but tweaks can be made). So here is the code :
#ifndef MYBIMAP_HPP
#define MYBIMAP_HPP
#include <set>
#include <utility>
#include <algorithm>
using std::make_pair;
template<typename X, typename Y, typename Xless = std::less<X>,
typename Yless = std::less<Y>>
class bimap
{
typedef std::pair<X, Y> key_type;
typedef std::pair<X, Y> value_type;
typedef typename std::set<key_type>::iterator iterator;
typedef typename std::set<key_type>::const_iterator const_iterator;
struct Xcomp
{
bool operator()(X const &x1, X const &x2)
{
return !Xless()(x1, x2) && !Xless()(x2, x1);
}
};
struct Ycomp
{
bool operator()(Y const &y1, Y const &y2)
{
return !Yless()(y1, y2) && !Yless()(y2, y1);
}
};
struct Fless
{ // prevents lexicographical comparison for std::pair, so that
// every .first value is unique as if it was in its own map
bool operator()(key_type const &lhs, key_type const &rhs)
{
return Xless()(lhs.first, rhs.first);
}
};
/// key and value type are interchangeable
std::set<std::pair<X, Y>, Fless> _data;
public:
std::pair<iterator, bool> insert(X const &x, Y const &y)
{
auto it = find_right(y);
if (it == end()) { // every .second value is unique
return _data.insert(make_pair(x, y));
}
return make_pair(it, false);
}
iterator find_left(X const &val)
{
return _data.find(make_pair(val,Y()));
}
iterator find_right(Y const &val)
{
return std::find_if(_data.begin(), _data.end(),
[&val](key_type const &kt)
{
return Ycomp()(kt.second, val);
});
}
iterator end() { return _data.end(); }
iterator begin() { return _data.begin(); }
};
#endif
Example usage
template<typename X, typename Y, typename In>
void PrintBimapInsertion(X const &x, Y const &y, In const &in)
{
if (in.second) {
std::cout << "Inserted element ("
<< in.first->first << ", " << in.first->second << ")\n";
}
else {
std::cout << "Could not insert (" << x << ", " << y
<< ") because (" << in.first->first << ", "
<< in.first->second << ") already exists\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
bimap<std::string, int> mb;
PrintBimapInsertion("A", 1, mb.insert("A", 1) );
PrintBimapInsertion("A", 2, mb.insert("A", 2) );
PrintBimapInsertion("b", 2, mb.insert("b", 2));
PrintBimapInsertion("z", 2, mb.insert("z", 2));
auto it1 = mb.find_left("A");
if (it1 != mb.end()) {
std::cout << std::endl << it1->first << ", "
<< it1->second << std::endl;
}
auto it2 = mb.find_right(2);
if (it2 != mb.end()) {
std::cout << std::endl << it2->first << ", "
<< it2->second << std::endl;
}
return 0;
}
NOTE: All this is a rough code sketching of what a full implementation would be and even after polishing and extending the code I'm not implying that this would be an alternative to boost::bimap but merely a homemade way of having an associative container searchable by both the value and the key.
Live example
One possible implementation that uses an intermediate data structure and an indirection is:
int sz; // total elements in the bimap
std::map<A, int> mapA;
std::map<B, int> mapB;
typedef typename std::map<A, int>::iterator iterA;
typedef typename std::map<B, int>::iterator iterB;
std::vector<pair<iterA, iterB>> register;
// All the operations on bimap are indirected through it.
Insertion
Suppose you have to insert (X, Y) where X, Y are instances of A and B respectively, then:
Insert (X, sz) in mapA --- O(lg sz)
Insert (Y, sz) in mapB --- O(lg sz)
Then push_back (IterX, IterY) in register --- O(1). Here IterX and IterY are iterators to the corresponding element in mapA and mapB and are obtained from (1) and (2) respectively.
Lookup
Lookup for the image of an element, X, of type A:
Get the int mapped to X in mapA. --- O(lg n)
Use the int to index into register and get corresponding IterY. --- O(1)
Once you have IterY, you can get Y through IterY->first. --- O(1)
So both the operations are implemented in O(lg n).
Space: All the copies of the objects of A and B are required to be stored only once. There is, however, a lot of bookkeeping stuff. But when you have large objects, that would also be not much significant.
Note: This implementation relies on the fact that the iterators of a map are never invalidated. Hence, the contents of register are always valid.
A more elaborate version of this implementation can be found here
How about this?
Here, we avoid double storage of one type (T1). The other type (T2) is still double stored.
// Assume T1 is relatively heavier (e.g. string) than T2 (e.g. int family).
// If not, client should instantiate this the other way.
template <typename T1, typename T2>
class UnorderedBimap {
typedef std::unordered_map<T1, T2> Map1;
Map1 map1_;
std::unordered_map<T2, Map1::iterator> map2_;
};
I am using C++ to implement a square list which is a doubly-linked list of doubly-linked lists. The list is sorted, and the idea is that the structure maintains the shape of a square as elements are inserted and erased. If the list has four elements in total that square would be in a 2x2 shape.
It looks something like this:
I've done some research on linked lists and understand that the general idea is to create nodes that hold each data element, as well as pointers to and from the surrounding elements, but one thing that's throwing me off is how I would (or could) implement an iterator to traverse the square list.
This is my first time attempting to build a data structure, so the process in general is pretty new to me. Any tips appreciated!
There are a few design decisions you need to make, like whether it matters if the overall iteration exhausts each "inner" list in turn, or should return the first elements of all inner lists, then the second etc..
As a taste of how to implement this:
#include <iostream>
#include <list>
template <typename T>
struct Iterator
{
typedef typename std::list<std::list<T> >::iterator outer_iterator;
typedef typename std::list<T>::iterator inner_iterator;
outer_iterator outer_;
bool inner_initialised_;
inner_iterator inner_;
Iterator(outer_iterator begin)
: outer_(begin), inner_initialised_(false)
{ }
T& operator*()
{
if (!inner_initialised_)
{
inner_ = outer_->begin();
inner_initialised_ = true;
}
return *inner_;
}
T& operator->() { return operator*(); }
Iterator& operator++()
{
if (++inner_ == outer_->end())
{
++outer_;
inner_initialised_ = false;
}
return *this;
}
bool operator!=(outer_iterator i) const { return outer_ != i; }
};
int main()
{
std::list<std::list<int>> lli;
std::list<int> li;
li.push_back(42);
li.push_back(13);
lli.push_back(li);
li.push_back(999);
lli.push_back(li);
for (Iterator<int> i = lli.begin(); i != lli.end(); ++i)
std::cout << *i << ' ';
std::cout << '\n';
}
Output:
42 13 42 13 999
Notice the bool inner_initialised_ variable - it ensures no attempt is made to call ->begin() on an outer iterator value equal to end().
You'll probably want to flesh that out a little with operator==, ++(int), --, a const version for const_iterators etc. for more general usage. I'd normally make the custom iterator class a member of the custom container class, providing begin() and end() that yield custom iterator objects, but I'm not sure if you're planning to have an actual "square list" class or just use list<list<T>> everywhere.
basically, I have the
map<std::string, int>
so if i have
foo 5
bar 10
jack 3
in the map, I want to display it (notice the reverse order)
bar 10
foo 5
jack 3
And every time it is updated, I want iterate through all the elements, cout them, sorted by value. What is the good way to implement that? should I provide a comparator to the constructor?
I want to note that values in the map will be updated at least 100 million times, so efficiency is crucial, where as extra-space is no problem
Please no Boost solutions...thx
struct keyval_t { std::string key; int val; };
int operator<(const keyval_t &a, const ketval_t &b)
{ return a.val<b.val || (a.val==b.val && a.key<b.key); }
Then you need one map and one set:
map<std::string, int>; set<keyval_t>;
On update, you need to look up the map first to determine the key-value pair and then update both map and set. On printing, you just iterate through the set. In terms of theoretical time complexity, this is optimal. It doubles the memory, though. Does this meet your goal?
To reduce memory, you may consider the following:
map<std::string,uint64_t>; set<uint64_t>;
The value of the map (also the key of the set) is: (uint64_t)val<<32|counter, where counter is something that differentiates identical values. For example, whenever you insert a key, increase the counter by 1. You do not need to update the counter when you update the value. If you do not like uint64_t, use pair<int,int> instead. This solution is also faster as it avoids comparisons between strings.
If you want a performant map sorted by both key and value, you want Boost MultiIndex, it gets updated (resorted) on every update (which you have to do manually) and has a good documentation.
The previous responses have the inconvenience not to take into account the initial requirements (the key is std::string and the value is int).
EDITED: following the comments, I suppose presenting it directly with a Bimap is better :)
So here we go, right in!
#include <boost/bimap.hpp>
class MyMap
{
struct name {};
struct value {};
typedef boost::tagged<name, std::string> tagged_name;
typedef boost::tagged<value, int> tagged_value;
// unordered_set_of: guarantees only unicity (not order)
// multi_set_of: guarantees only order (not unicity)
typedef boost::bimap< boost::unordered_set_of< tagged_name >,
boost::multi_set_of< tagged_value,
std::greater< tagged_value >
>
> impl_type;
public:
// Redefine all usual types here
typedef typename impl_type::map_by<name>::const_iterator const_iterator;
typedef typename impl_type::value_type value_type;
// Define the functions you want
// the bimap will not allow mutators because the elements are used as keys
// so you may want to add wrappers
std::pair< iterator, bool > insert(const value_type & x)
{
std::pair< iterator, bool > result = m_impl.insert(x);
if (result.second) this->display();
return result;
} // insert
iterator insert(iterator position, const value_type & x)
{
iterator result = m_impl.insert(x);
this->display();
return result;
} // insert
template< class InputIterator >
void insert(InputIterator first, InputIterator last)
{
m_impl.insert(first, last);
this->display();
} // insert
private:
void display() const
{
// Yeah I know about std::for_each...
typedef typename impl_type::map_by<value>::const_iterator const_it;
for (const_it it = m_impl.begin(), end = m_impl.end(); it != end; ++it)
{
// Note the inversion of the 'second' and 'first',
// we are looking at it from the right
std::cout << it->second << " " << it->first << std::endl;
}
}
impl_type m_impl;
}; // class MyMap
Here you go.
I strongly suggest that you consult the bimap documentation though. There are a lot of possibilities for storing (set_of, unordered_set_of, unconstrained_set_of, the muli variants, the list_of variant...) so there is probably one that could do what you want.
Then you also have the possibility to just sort each time you display:
#include <set>
#include <map>
// Just use a simple std::map<std::string,int> for your impl_type
// Mutators are allowed since the elements are sorted each time you display
struct Comparator
{
bool operator(const value_type& lhs, const value_type& rhs) const
{
return lhs.second < rhs.value;
}
};
void display() const
{
typedef std::multi_set<value_type, Comparator> sort_type;
sort_type mySet;
std::copy(m_impl.begin(), m_impl.end(), std::inserter(mySet, mySet.end()));
for (sort_type it = mySet.begin(), end = mySet.end(); it != end; ++it)
{
std::cout << it->first<< " " << it->second << std::endl;
}
}
It should be easier to understand my point now :)
Well, you have to sort by key. The easiest way to "sort by value" is to use a multimap with the key and value switched. So here's a way to do that (note -- i don't have access to a compiler right now, so if it doesn't compile, I'm sorry):
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <utility>
typedef std::multimap<int, std::string, std::greater<int> > MapType;
struct MapKeyOutput
{
void operator()(const MapType::value_type& val)
{
std::cout << val.second << " " << val.first << "\n";
}
}
void display_insert(MapType& m, const std::string& str, int val)
{
m.insert(std::make_pair(val, str));
std::for_each(m.begin(), m.end(), MapKeyOutput());
}
int main()
{
MapType m;
display_insert(m, "Billy", 5);
display_insert(m, "Johnny", 10);
}
You could also make a map class that uses a multimap internally, I just didn't want to type it out. Then display_insert would be some member function instead. This should demonstrate the point, though. Points of interest:
typedef std::multimap<int, std::string, std::greater<int> > MapType;
Note the comparator is greater<int> to sort descending. We're using a multimap so more than one name can have the same number.
struct MapKeyOutput
{
void operator()(const MapType::value_type& val)
{
std::cout << val.second << " " << val.first << "\n";
}
}
This is a function object to output one element in the map. second is output before first so the order is what you want.
std::for_each(m.begin(), m.end(), MapKeyOutput());
This applies our function object to every element in m.