Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Ghost
- {
- usize sizeof()
- void this()
- void this(this)
- operator= (this)
- }
- class vector<Ghost>
- {
- alias T=Ghost
- usize len
- T[] buf
- public const:
- usize size() { return len }
- usize max_size() { return usize.max/T.sizeof }
- usize capacity() { return buf.len }
- bool empty() { return len==0 }
- public:
- T operator [](usize i) {
- in{ assert(i<len, "Out of bounds") }
- return buf[i]
- }
- this() { len=0; buf=new(0); }
- this(usize n) { len=n; buf=new(len) }
- this(usize n, T t) { this(); resize(n, t) }
- //this(this t) //automatically copies len, allocates a new buf then copies all elements
- this(Iterator s, Iterator e)
- {
- //i am not sure how to do ATM
- }
- T front() { in{ assert(len>0); } return buf[0] }
- T back() { in{ assert(len>0); } return buf[len-1] }
- void push_back(T t) { reserve(len+1); buf[len++] = r; }
- void pop_back() { --len; }
- //void swap() { /*unsure howto atm*/ }
- //insert
- //erase
- void clear() { len=0 }
- void reserve(usize n){
- if n<=len
- return;
- buf.resize(max(buf.len*1.5, n))
- }
- void resize(usize n, t = T()) {
- if n>len { buf.resize(max(buf.len*1.5, n))(t) }
- else { len = n; }
- }
- // == and <
- //my extra function. consider the case of reading a file in a C style (or recv with sockets)
- //you have availible a continous length bytes. You do not know how much would be read/recv
- //so use getArray() to get an array to read with, then tell the vector the new length
- //which is the amount you were able to read in
- T[]& getArray() { return buf; }
- void property length { get{return len} set{ in{assert(value<=buf.len)} len = value} }
- }
- class string
- {
- vector<letter> sz;
- public const:
- forward{
- using sz { .size, .capacity, .empty, .operator[], }
- }
- alias { length = size, }
- //c_str, data
- void max_size( sz.max_size-1 ) //for null
- public:
- forward { using sz { .reserve } }
- //this()
- this(this src, usize spos, epos) { sz.reserve(epos-spos); sz[] = src[spos:epos] }
- //other this
- //swap
- //append
- //push_back
- }
- struct SomeData
- {
- chunk { int x, y }
- chuck { Flag flag }
- }
- SomeData[2]
- {
- 0:
- 00: x
- 04: y
- 1:
- 08: x
- 0C: y
- 0:
- 10: flag
- 1:
- 14: flag
- }
- buf = { usize len, *chunk1, *chunk2 }
- -----------
Add Comment
Please, Sign In to add comment