File: vector.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (84 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once

/*
vector allocator
sizeof(string) == 16 (amd64)

utilizes a raw string pointer
always allocates memory onto the heap when string is not empty

pros:
* potential for in-place resize
* simplicity

cons:
* always allocates heap memory on (capacity > 0)
* pass-by-value requires heap allocation

*/

namespace nall {

template<typename T>
inline auto string::get() -> T* {
  if(_capacity == 0) reserve(1);
  return (T*)_data;
}

template<typename T>
inline auto string::data() const -> const T* {
  if(_capacity == 0) return (const T*)"";
  return (const T*)_data;
}

inline auto string::reset() -> type& {
  if(_data) { memory::free(_data); _data = nullptr; }
  _capacity = 0;
  _size = 0;
  return *this;
}

inline auto string::reserve(u32 capacity) -> type& {
  if(capacity > _capacity) {
    _capacity = bit::round(capacity + 1) - 1;
    _data = memory::resize<char>(_data, _capacity + 1);
    _data[_capacity] = 0;
  }
  return *this;
}

inline auto string::resize(u32 size) -> type& {
  reserve(size);
  get()[_size = size] = 0;
  return *this;
}

inline auto string::operator=(const string& source) -> type& {
  if(&source == this) return *this;
  reset();
  _data = memory::allocate<char>(source._size + 1);
  _capacity = source._size;
  _size = source._size;
  memory::copy(_data, source.data(), source.size() + 1);
  return *this;
}

inline auto string::operator=(string&& source) -> type& {
  if(&source == this) return *this;
  reset();
  _data = source._data;
  _capacity = source._capacity;
  _size = source._size;
  source._data = nullptr;
  source._capacity = 0;
  source._size = 0;
  return *this;
}

inline string::string() {
  _data = nullptr;
  _capacity = 0;
  _size = 0;
}

}