Guest User

Untitled

a guest
Jun 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. interface Ghost
  2. {
  3. usize sizeof()
  4. void this()
  5. void this(this)
  6. operator= (this)
  7. }
  8.  
  9. class vector<Ghost>
  10. {
  11. alias T=Ghost
  12. usize len
  13. T[] buf
  14.  
  15. public const:
  16. usize size() { return len }
  17. usize max_size() { return usize.max/T.sizeof }
  18. usize capacity() { return buf.len }
  19. bool empty() { return len==0 }
  20.  
  21. public:
  22. T operator [](usize i) {
  23. in{ assert(i<len, "Out of bounds") }
  24. return buf[i]
  25. }
  26.  
  27. this() { len=0; buf=new(0); }
  28. this(usize n) { len=n; buf=new(len) }
  29. this(usize n, T t) { this(); resize(n, t) }
  30. //this(this t) //automatically copies len, allocates a new buf then copies all elements
  31. this(Iterator s, Iterator e)
  32. {
  33. //i am not sure how to do ATM
  34. }
  35.  
  36. T front() { in{ assert(len>0); } return buf[0] }
  37. T back() { in{ assert(len>0); } return buf[len-1] }
  38. void push_back(T t) { reserve(len+1); buf[len++] = r; }
  39. void pop_back() { --len; }
  40. //void swap() { /*unsure howto atm*/ }
  41. //insert
  42. //erase
  43. void clear() { len=0 }
  44.  
  45.  
  46. void reserve(usize n){
  47. if n<=len
  48. return;
  49.  
  50. buf.resize(max(buf.len*1.5, n))
  51. }
  52. void resize(usize n, t = T()) {
  53. if n>len { buf.resize(max(buf.len*1.5, n))(t) }
  54. else { len = n; }
  55. }
  56. // == and <
  57.  
  58. //my extra function. consider the case of reading a file in a C style (or recv with sockets)
  59. //you have availible a continous length bytes. You do not know how much would be read/recv
  60. //so use getArray() to get an array to read with, then tell the vector the new length
  61. //which is the amount you were able to read in
  62. T[]& getArray() { return buf; }
  63. void property length { get{return len} set{ in{assert(value<=buf.len)} len = value} }
  64. }
  65.  
  66. class string
  67. {
  68. vector<letter> sz;
  69. public const:
  70. forward{
  71. using sz { .size, .capacity, .empty, .operator[], }
  72. }
  73. alias { length = size, }
  74. //c_str, data
  75. void max_size( sz.max_size-1 ) //for null
  76. public:
  77. forward { using sz { .reserve } }
  78. //this()
  79. this(this src, usize spos, epos) { sz.reserve(epos-spos); sz[] = src[spos:epos] }
  80. //other this
  81. //swap
  82. //append
  83. //push_back
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. struct SomeData
  92. {
  93. chunk { int x, y }
  94. chuck { Flag flag }
  95. }
  96.  
  97. SomeData[2]
  98. {
  99. 0:
  100. 00: x
  101. 04: y
  102. 1:
  103. 08: x
  104. 0C: y
  105. 0:
  106. 10: flag
  107. 1:
  108. 14: flag
  109. }
  110. buf = { usize len, *chunk1, *chunk2 }
  111. -----------
Add Comment
Please, Sign In to add comment