i'm still interested to know if this is a standard kind of data structure. if not, i think this should suffice:
Code:
template<typename ty>
class agglomerative_list {
private:
std::vector<ty> buf;
size_t sz = 0;
public:
/*
* Constructors
*/
agglomerative_list() {
}
agglomerative_list(const size_t &length)
: sz(0), buf(length) {
}
agglomerative_list(const agglomerative_list &al)
: sz(al.sz), buf(al.buf) {
}
agglomerative_list(const std::initializer_list<ty> &list)
: sz(list.size()), buf(list) {
}
/*
* Add data
*/
void push_back(const ty &val) {
if (sz >= buf.size())
buf.push_back(val);
else
buf[sz] = val;
++sz;
}
/*
* Reset the view into the list
*/
void reset() {
sz = 0;
}
/*
* Size
*/
size_t size() const {
return sz;
}
size_t total_size() const {
return buf.size();
}
/*
* Random access
*/
ty &at(const size_t &pos) {
assert(pos < sz);
return buf[pos];
}
const ty &at(const size_t &pos) const {
assert(pos < sz);
return buf[pos];
}
/*
* Iteration
*/
typedef typename std::vector<ty>::iterator iterator;
typedef typename std::vector<ty>::const_iterator const_iterator;
iterator begin() {
return buf.begin();
}
iterator end() {
return buf.begin() + sz;
}
const_iterator begin() const {
return buf.begin();
}
const_iterator end() const {
return buf.begin() + sz;
}
};
with example usage
Code:
crf::agglomerative_list<int> al;
for (int ii = 0; ii < 10; ++ii)
al.push_back(ii);
std::cout<< "al (" << al.size() << ", " << al.total_size() << "): [";
for( auto &el: al)
std::cout<< el << " ";
std::cout<< "]\n";
al.reset();
std::cout<< "al (" << al.size() << ", " << al.total_size() << "): [";
for( auto &el: al)
std::cout<< el << " ";
std::cout<< "]\n";
for (int ii = 0; ii < 5; ++ii)
al.push_back(ii);
std::cout<< "al (" << al.size() << ", " << al.total_size() << "): [";
for( auto &el: al)
std::cout<< el << " ";
std::cout<< "]\n";
for (int ii = 0; ii < 10; ++ii)
al.push_back(ii);
std::cout<< "al (" << al.size() << ", " << al.total_size() << "): [";
for( auto &el: al)
std::cout<< el << " ";
std::cout<< "]\n";
from which i get the following output
al (10, 10): [0 1 2 3 4 5 6 7 8 9 ]
al (0, 10): []
al (5, 10): [0 1 2 3 4 ]
al (15, 15): [0 1 2 3 4 0 1 2 3 4 5 6 7 8 9 ]