Skip to content

Commit

Permalink
an alternative vector implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
krangelov committed Aug 27, 2024
1 parent 6a75e4d commit cda3e99
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/runtime/c/pgf/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,55 @@ A *vector_elem(Vector<A> *v, size_t index)
return &v->data[index];
}

template<class A> class PGF_INTERNAL_DECL vector {
private:
object offset;

struct V {
size_t size;
A data[];
};

V* v() const { return (V*) (current_base+offset); }

vector(object o) {
this->offset = o;
}

public:
class iterator {
public:
object offset;

iterator(ref<A> r) {
this->offset = r.as_object();
}

bool operator!=(iterator other) {
return offset != other.offset;
}

void operator++() {
offset += sizeof(A);
}

A &operator*() {
return *((A*) (current_base+offset));
}
};

size_t size() { return v()->size; };
A &operator[] (size_t i) { return v()->data[i]; };
iterator begin() { return iterator(ref<A>::from_ptr(&v()->data[0])); }
iterator end() { return iterator(ref<A>::from_ptr(&v()->data[v()->size])); }

static vector<A> alloc(size_t size)
{
auto res = PgfDB::malloc<vector<A>::V>(size*sizeof(A));
res->size = size;
return vector<A>(res.as_object());
}
};


#endif // VECTOR_H

0 comments on commit cda3e99

Please sign in to comment.