iterator over multiple std containers subsequently - c++

I have the following class (which obviously does not yet work as intended):
class A
{
private:
std::vector<int> firstVector, secondVector;
public:
std::vector<int>::iterator begin(){
return firstVector.begin();
}
std::vector<int>::iterator end(){
return secondVector.end();
}
};
How can I define an iterator which will go over the two member containers subsequently, e.g. after firstVector.end()-1 secondVector.begin() is returned and going all the way to secondVector.end() ?

Basically you need to define some custom iterator that internally checks for the end of the first range, then goes on to the next one.
However, this sort of stuff occurs a lot. Eventually you'd ask why an iterator for two vectors, why an iterator for two vectors, why a sequence of the same container type, and so on. Nir Tzachar & I have written a C++ port of Python itertools that does this sort of common stuff. In this case, you'd just use
chain(firstVector, secondVector)
It can be downloaded from this bitbucket repo.

Nothing to stop you from rolling your own. Can even make it random access!
struct chain_iterator
: std::iterator<std::random_access_iterator_tag, int>
{
using it = std::vector<int>::iterator;
std::pair<it, it> v1, v2;
bool first;
it cur;
};
We keep the initial iterator pairs so that we can do random access correctly.
Incrementing is what you'd expect:
chain_iterator& operator++() {
++cur;
if (first && cur == v1.second) {
first = false;
cur = v2.first;
}
return *this;
}
Dereference is trivial:
int& operator*() { return *cur; }
Advance has to do some extra checking:
chain_iterator& operator+=(size_t n) {
if (!first) {
// trivial case
cur += n;
}
else {
size_t d = v1.second - cur;
if (d < n) {
cur += n;
}
else {
first = false;
cur = v2.first + (d - n);
}
}
return *this;
}
I'll leave the rest of the operations as an exercise.

You could write your own function to do the incrementing:
std::vector<int>::iterator& inc(std::vector<int>::iterator& it) {
++it;
if (it == firstVector.end())
it = secondVector.begin();
return it;
}
This is also a good indication to others that the increment doesn't happen normally.

Related

iterator returned by std::find() is not dereferenceable

This is an insert() function of an implementation of a HashTable with chaining. In order to avoid duplications in the linked_list I ckecked if a value already exists. If it does then I just replace the existing value as it can be seen almost at the end where it comments "update value". That line issues an exception telling me that the iterator is not dereferenceable. Why can't I dereference the iterator returned by std::find()? Is there another way to update the value that was found?
virtual void insert(const K& k, const V& v) {
auto index = hashFctn(k, m_table.capacity());
if (needsToGrow() || m_table[index].m_list.size() >= m_load_factor) {
rehash();
insert(k, v);
}
else {
auto it = std::find(m_table[index].m_list.begin(),
m_table[index].m_list.end(), v);
if (it != m_table[index].m_list.end()) { // if found add it
m_table[index].m_flag = flag::IN_USE;
m_table[index].m_key = k;
m_table[index].m_list.push_back(v);
m_nbrOfElements++;
} else {
*it = v; // update value if exists
}
}
}
You have
if (it != m_table[index].m_list.end()) { // if found add it
// Irrelevant...
} else {
*it = v; // update value if exists
}
If the iterator it is not the end-iterator you do some irrelevant things. But in the else case the iterator it is equal to the end-iterator, and that is not dereferencable. And yet you dereference it.
I think the condition should be the opposite, using == instead.

Iterator end() function not working with pointer arithmetic

When I run the following I miss the last data in the list and get the previous data. When I add a counter and try to subtract by 1 I crash. Any help on this would be much appreciated.
template <typename T>
Iterator<T> Iterator<T>::operator--()
{
ptr = ptr->backward;
return *this;
}
template <typename T>
Iterator<T> DoublyLinkedList<T>::end() const
{
Iterator<T> iObj;
iObj.ptr = this->last;
iObj.capacity = this->count;
return iObj;
}
int main() {
DoublyLinkedList<int> *d = new DoublyLinkedList<int>;
for (int i = 2; i <= 20; i += 2) {
d->insertLast(i);
}
//Get an Iterator which points at the end of the list
Iterator<int> iter = d->end();
--iter;
//Test that it does point to the first
checkTest("testIteratorsDecrement #1", 20, *iter);
//Test that our Iterator can move forward;
--iter;
checkTest("testIteratorsDecrement #2", 18, *iter);
//move it some more
for (int i = 0; i < 7; i++) {
--iter;
}
checkTest("testIteratorsDecrement #3", 4, *iter);
--iter;
checkTest("testIteratorsDecrement #4", 2, *iter);
delete d;
return 0;
}
I try to fix it by doing the following but it crashes. count is a protected int.
template <typename T>
Iterator<T> DoublyLinkedList<T>::end() const
{
Iterator<T> iObj;
iObj.ptr = this->last + (count -1);
iObj.capacity = this->count;
return iObj;
}
Usually end() would return a sentinel value which can not be dereferenced. It looks like you are returning a pointer to the last entry, that is the source of your off-by-one error.
As the implementor you can choose your sentinel value but it should not be a valid entry in the list.
As an aside: I don't see a good reason for having a capacity member in an iterator. How is it kept up-to-date?
Unlike arrays or vector, list does not store its elements one-by-one in memory.
Your first element may be at some begin address, but the next element can be begin+5, or begin-10 or something else.
Basically, this means you can't do arithmetic operations with pointers to elements of your list.
If you want some kind end element, I'd suggest making the last element of your list to point to NULLPTR

C++ Bimap Left unordered_map Right sorted mutable multimap

I need to implement the following datastructure for my project. I have a relation of
const MyClass*
to
uint64_t
For every pointer I want to save a counter connected to it, which can be changed over time (in fact only incremented). This would be no problem, I could simply store it in a std::map. The problem is that I need fast access to the pointers which have the highest values.
That is why I came to the conclusion to use a boost::bimap. It is defined is follows for my project:
typedef boost::bimaps::bimap<
boost::bimaps::unordered_set_of< const MyClass* >,
boost::bimaps::multiset_of< uint64_t, std::greater<uint64_t> >
> MyBimap;
MyBimap bimap;
This would work fine, but am I right that I can not modify the uint64_t on pair which were inserted once? The documentation says that multiset_of is constant and therefore I cannot change a value of pair in the bimap.
What can I do? What would be the correct way to change the value of one key in this bimap? Or is there a simpler data structure possible for this problem?
Here's a simple hand-made solution.
Internally it keeps a map to store the counts indexed by object pointer, and a further multi-set of iterators, ordered by descending count of their pointees.
Whenever you modify a count, you must re-index. I have done this piecemeal, but you could do it as a batch update, depending on requirements.
Note that in c++17 there is a proposed splice operation for sets and maps, which would make the re-indexing extremely fast.
#include <map>
#include <set>
#include <vector>
struct MyClass { };
struct store
{
std::uint64_t add_value(MyClass* p, std::uint64_t count = 0)
{
add_index(_map.emplace(p, count).first);
return count;
}
std::uint64_t increment(MyClass* p)
{
auto it = _map.find(p);
if (it == std::end(_map)) {
// in this case, we'll create one - we could throw instead
return add_value(p, 1);
}
else {
remove_index(it);
++it->second;
add_index(it);
return it->second;
}
}
std::uint64_t query(MyClass* p) const {
auto it = _map.find(p);
if (it == std::end(_map)) {
// in this case, we'll create one - we could throw instead
return 0;
}
else {
return it->second;
}
}
std::vector<std::pair<MyClass*, std::uint64_t>> top_n(std::size_t n)
{
std::vector<std::pair<MyClass*, std::uint64_t>> result;
result.reserve(n);
for (auto idx = _value_index.begin(), idx_end = _value_index.end() ;
n && idx != idx_end ;
++idx, --n) {
result.emplace_back((*idx)->first, (*idx)->second);
}
return result;
}
private:
using map_type = std::map<MyClass*, std::uint64_t>;
struct by_count
{
bool operator()(map_type::const_iterator l, map_type::const_iterator r) const {
// note: greater than orders by descending count
return l->second > r->second;
}
};
using value_index_type = std::multiset<map_type::iterator, by_count>;
void add_index(map_type::iterator iter)
{
_value_index.emplace(iter->second, iter);
}
void remove_index(map_type::iterator iter)
{
for(auto range = _value_index.equal_range(iter);
range.first != range.second;
++range.first)
{
if (*range.first == iter) {
_value_index.erase(range.first);
return;
}
}
}
map_type _map;
value_index_type _value_index;
};

the sequence of insert and erase using stl::list

When I am doing practice on leetcode, I met a problem like this:
I used a stl::list container as cache for LRU algorithm. But the sequence of erasing an item and inserting an item made the result different.
I know that it is actually a double list as stl::list. And the sequence of inserting and erasing should not matter when I use iterator.
The code is here
class LRUCache{
public:
map<int, list<pair<int,int>>::iterator> mKey;
list<pair<int,int>> lCache;
int cap;
LRUCache(int capacity) {
cap = capacity;
}
int get(int key) {
auto iter = mKey.find(key);
if(iter != mKey.end()) {
int value = (iter->second)->second;
//**the sequence of next two lines can not be changed!***
lCache.erase(iter->second);
mKey[key] = lCache.insert(lCache.begin(), make_pair(key,value));
return value;
}
return -1;
}
void set(int key, int value) {
auto iter = mKey.find(key);
if(iter == mKey.end()) {
if(lCache.size() < cap) {
mKey[key] = lCache.insert(lCache.begin(), make_pair(key,value));
}
else{
mKey[key] = lCache.insert(lCache.begin(), make_pair(key,value));
mKey.erase(lCache.back().first);
lCache.pop_back();
}
}
else {
lCache.erase(iter->second);
mKey[key] = lCache.insert(lCache.begin(), make_pair(key,value));
}
}
};
It's not quite clear what you are asking. If your question is why these two lines can't be reordered:
//**the sequence of next two lines can not be changed!***
lCache.erase(iter->second);
mKey[key] = lCache.insert(lCache.begin(), make_pair(key,value));
then that's simple. iter points to the same node as mKey[key], so the assignment actually changes the value of iter->second. If the assignment would happen first, then iter->second would point to the freshly inserted list node, not the previously existing one.

Specific route with iterator on Standard Container

I have a class with an standard container member, and I'm wondering is that possible that I make an own iterator with a specific route, for example it goes back and forth, and after that stops.
template<class T>
class compressed_string {
vector<T> v;
public:
typedef typename std::vector<T>::iterator iterator;
iterator begin() { return v.begin(); }
iterator end() { return v.end(); }
compressed_string& add(const T& elem) {
v.push_back(elem);
return *this;
}
basic_string<T> not_nice_way_to_make_real_string() {
basic_string<T> tmp;
for(iterator i = v.begin(); i < v.end(); ++i)
tmp += *i;
for(iterator i = --v.end(); i >= v.begin(); --i)
tmp += *i;
return tmp;
}
};
main:
compressed_string<char> s;
s.add('q').add('w').add('e').add('w');
cout << s.not_nice_way_to_make_real_string(); // q w e w w e w q
cout << endl
for ( compressed_string<char>::iterator i = s.begin(); i < s.end(); ++i )
cout << *i;
So with this iterator member the output would be the same in this two lines.
How is this possible?
You'll need an iterator that stores a bit of state:
where it is (e.g. an iterator v_it over v)
where it's going (e.g. bool forward)
where it's bounded (e.g. iterators v_begin = v.begin() and v_end = v.end())
and some otherwise invalid iterator to represent the end, such as {v_end, backward}).
Then implement the increment operator along the lines of:
if (forward) {
if (++v_it == v_end) {
forward = false;
--v_it;
}
} else {
if (v_it-- == v_begin) {
v_it = v_end;
}
}
and similarly for decrement, if you want a bidirectional iterator; in which case, it would be polite to provide a reverse_iterator too. You should provide both pre- and post-increment forms.
You'll also need == and != comparisons, comparing both v_it and forward, and dereference operators * and -> that dereference v_it, and suitable begin and end functions; for bonus points, a const_iterator would be nice.
Note that you'll need random access if you really want the code in your question (i < s.end() rather than the more generic i != s.end()) to work; that's entirely possible, but rather excessive if you don't otherwise need it.
UPDATE: as noted in the comments, this particular implementation could probably be improved a bit; for example, it's possible to remove the need to store v_begin if you're a bit careful about how you define the end iterator.