473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Custom bidimensional iterator and container

Hi,
I need to manage a "layered" collection of objects, where each layer
grows independently, e.g,

o--+--+--+--+--+ 1st layer
|
o 2nd layer (empty)
|
o--+--+--+ 3rd layer
|
o--+--+ 4th layer

This data structure should be a random access container (the goal is to
use it as a custom VertexList in boost.Graph for a graph whose nodes are
partitioned into layers). Besides being able to iterate over all the
elements, (horizontal) iteration over a single layer and (vertical)
iteration over the set of layers are required. Insertion at the end of
each layer must be efficient, so "flattening" the data structure into,
say, a linear vector is not a viable solution.

I haven't found anything that suits my needs, so I came out with the
code below. As this is my first attempt at writing a user defined
container and iterator - and I found the task a bit tricky - I would
appreciate some feedback about how my solution can be improved. In the
worst case, moving the iterator takes time proportional to the number of
layers, so probably this is not a "real" random access container.

Thanks,
Nicola

#include <vector>
#include <iterator>

// Iterator over elements of LayeredContainer<T>
template <typename LayeredCont>
class LayeredIterator :
public std::iterator<std::random_access_iterator_tag, LayeredCont>
{
protected:
typename LayeredCont::InLayerIterator currH; // current pos. in layer
typename LayeredCont::CrossLayersIterator currV; // current layer
typename LayeredCont::CrossLayersIterator firstV; // first layer
typename LayeredCont::CrossLayersIterator lastV; // last layer

public:
typedef LayeredIterator<LayeredCont> iterator;
typedef LayeredIterator<const LayeredCont> const_iterator;

LayeredIterator() : currH(), currV(), firstV(), lastV() { }

// Build an iterator pointing at the first element
explicit LayeredIterator(LayeredCont& c) {
firstV = c.firstLayer();
lastV = c.lastLayer();
currV = firstV;
// Search for the first non-empty layer (if it exists)
while ((*currV).empty() && currV != lastV)
++currV;
currH = (*currV).begin();
}

bool operator==(const LayeredIterator& c) const {
return (this->currH == c.currH);
// '&& this->currV == c.currV' is redundant
}

bool operator!=(const LayeredIterator& c) {
return !(*this == c);
}

typename LayeredCont::reference operator*() const {
return *currH;
}

typename LayeredCont::pointer operator->() const {
return &*currH;
}

// Prefix increment operator
LayeredIterator& operator++() {
++currH;
while (currH == (*currV).end() && currV != lastV) {
++currV;
currH = (*currV).begin();
}
return *this;
}

// Prefix decrement operator
LayeredIterator& operator--() {
while (currH == (*currV).begin()) {
--currV;
currH = (*currV).end();
}
--currH;
return *this;
}

LayeredIterator& operator+=(typename LayeredCont::difference_type n) {
if (n >= 0) { // Move forward
typename LayeredCont::difference_type offset = (*currV).end() -
currH;
while (n >= offset && currV != lastV) {
++currV;
n -= offset;
currH = (*currV).begin();
offset = (*currV).end() - currH;
}
}
else { // Move backwards
typename LayeredCont::difference_type offset = (currH -
(*currV).begin());
while (-n >= offset && currV != firstV) {
--currV;
n += offset;
currH = (*currV).end();
offset = currH - (*currV).begin();
}
}
currH += n;
return *this;
}

LayeredIterator operator+(typename LayeredCont::difference_type n)
const {
LayeredIterator tmp(*this);
return tmp += n;
}

LayeredIterator& operator-=(typename LayeredCont::difference_type n) {
return *this += -n;
}

// Etc.. The rest is more or less easily syntactic sugar.
};
------------------------------------------------------------
#include <vector>
#include <deque>
#include <stdexcept>
#include "LayeredIterator.hpp"

template <typename T, template <typename E, typename ALLOC =
std::allocator<E> > class Layer = std::vector >
class LayeredContainer {
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef std::ptrdiff_t difference_type;
typedef size_t size_type;

// Global iterators
typedef LayeredIterator<LayeredContainer> iterator;
typedef LayeredIterator<const LayeredContainer> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef const std::reverse_iterator<iterator> const_reverse_iterator;

typedef std::deque< Layer<T> > LayersContainer;

// Iterators inside a single layer
typedef typename Layer<T>::iterator InLayerIterator;
typedef typename Layer<T>::const_iterator InLayerConstIterator;
// Iterators across the layers
typedef typename LayersContainer::iterator CrossLayersIterator;
typedef typename LayersContainer::const_iterator
CrossLayersConstIterator;

InLayerIterator beginOfLayer(const int i) {return layers[i].begin();}
InLayerConstIterator beginOfLayer(const int i) const
{ return layers[i].begin();}
InLayerIterator endOfLayer(con st int i) { return layers[i].end(); }
InLayerConstIterator endOfLayer(const int i) const
{ return layers[i].end(); }

CrossLayersIterator firstLayer() { return layers.begin(); }
CrossLayersConstIterator firstLayer() const { return layers.begin(); }
CrossLayersIterator lastLayer() { return layers.end() - 1; } // Well
defined, there is always at least one layer
CrossLayersConstIterator lastLayer() const { return layers.end() - 1;
}
CrossLayersIterator beyondLastLayer() { return layers.end(); }
CrossLayersConstIterator beyondLastLayer() const { return
layers.end(); }
iterator begin() { return _start; }
iterator end() { return _start + _size; }
const_iterator begin() const { return _start; }
const_iterator end() const { return _start + _size; }
reverse_iterator rbegin() { return
reverse_iterator(_start + _size); }
reverse_iterator rend() { return
reverse_iterator(_start); }
const_reverse_iterator rbegin() const { return
const_reverse_iterator(_start + _size); }
const_reverse_iterator rend() const { return
const_reverse_iterator(_start); }

// Default constructor
LayeredContainer() : _size(0) {
layers.push_back(Layer<T>()); // One layer
_start = iterator(*this);
}

LayeredContainer(std::size_t numLayers) {
for (std::size_t i = 0; i < numLayers; ++i)
layers.push_back(Layer<T>());
_size = 0;
_start = iterator(*this);
}

std::size_t numberOfLayers() const {
return layers.size();
}

T get(int _layer, int index) const {
return layers[_layer][index];
}

void add(std::size_t _layer, const T& value) {
layers[_layer].push_back(value);
++_size;
_start = iterator(*this); // How may I improve this?
}

reference operator[](std::size_t index) {
return *(_start + index);
}

size_type size() { return _size; }
bool empty() { return (0 == _size); }

protected:
LayersContainer layers;
iterator _start;
size_type _size;
};
-------------------------------------------------------------
// TEST
#include <iostream>
#include <LayeredVector.hpp>

int main (int argc, char* const argv[]) {
typedef LayeredContainer<int> LayeredVector;
LayeredVector lv(4);
// Insert some elements
lv.add(1, 2);
lv.add(1, 3);
lv.add(1, 5);
lv.add(1, 7);
lv.add(3, 10);
lv.add(3, 20);
lv.add(3, 30);
LayeredVector::iterator it;
for (it = lv.begin(); it != lv.end(); ++it)
std::cout << *it << " ";
std::cout << std::endl << "Second layer: ";
LayeredVector::InLayerConstIterator lit;
for (lit = lv.beginOfLayer(1); lit != lv.endOfLayer(1); ++lit)
std::cout << *lit2 << " ";
return 0;
}
Sep 10 '05 #1
0 1922

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
4
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help...
11
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
8
by: Mateusz Ɓoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
3
by: gallows | last post by:
The container is: template <typename T> class Container { public: // container methods.. // iterator: class const_iterator { public:
7
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
16
by: arnaudk | last post by:
I'm trying to design a simple container class for some data of different types based on a vector of structs, but the vector and struct are protected so that the implemenation of my container class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.