summaryrefslogtreecommitdiff
path: root/src/small_vector.h
blob: 800c1df175feffd7883c598762f678b76dd009db (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef _SMALL_VECTOR_H_

#include <streambuf>  // std::max - where to get this?
#include <cstring>
#include <cassert>

#define __SV_MAX_STATIC 2

class SmallVector {

 public:
  SmallVector() : size_(0) {}

  explicit SmallVector(size_t s, int v = 0) : size_(s) {
    assert(s < 0x80);
    if (s <= __SV_MAX_STATIC) {
      for (int i = 0; i < s; ++i) data_.vals[i] = v;
    } else {
      capacity_ = s;
      size_ = s;
      data_.ptr = new int[s];
      for (int i = 0; i < size_; ++i) data_.ptr[i] = v;
    }
  }

  SmallVector(const SmallVector& o) : size_(o.size_) {
    if (size_ <= __SV_MAX_STATIC) {
      for (int i = 0; i < __SV_MAX_STATIC; ++i) data_.vals[i] = o.data_.vals[i];
    } else {
      capacity_ = size_ = o.size_;
      data_.ptr = new int[capacity_];
      std::memcpy(data_.ptr, o.data_.ptr, size_ * sizeof(int));
    }
  }

  const SmallVector& operator=(const SmallVector& o) {
    if (size_ <= __SV_MAX_STATIC) {
      if (o.size_ <= __SV_MAX_STATIC) {
        size_ = o.size_;
        for (int i = 0; i < __SV_MAX_STATIC; ++i) data_.vals[i] = o.data_.vals[i];
      } else {
        capacity_ = size_ = o.size_;
        data_.ptr = new int[capacity_];
        std::memcpy(data_.ptr, o.data_.ptr, size_ * sizeof(int));
      }
    } else {
      if (o.size_ <= __SV_MAX_STATIC) {
        delete[] data_.ptr;
        size_ = o.size_;
        for (int i = 0; i < size_; ++i) data_.vals[i] = o.data_.vals[i];
      } else {
        if (capacity_ < o.size_) {
          delete[] data_.ptr;
          capacity_ = o.size_;
          data_.ptr = new int[capacity_];
        }
        size_ = o.size_;
        for (int i = 0; i < size_; ++i)
          data_.ptr[i] = o.data_.ptr[i];
      }
    }
    return *this;
  }

  ~SmallVector() {
    if (size_ <= __SV_MAX_STATIC) return;
    delete[] data_.ptr;
  }

  void clear() {
    if (size_ > __SV_MAX_STATIC) {
      delete[] data_.ptr;
    }
    size_ = 0;
  }

  bool empty() const { return size_ == 0; }
  size_t size() const { return size_; }

  inline void ensure_capacity(unsigned char min_size) {
    assert(min_size > __SV_MAX_STATIC);
    if (min_size < capacity_) return;
    unsigned char new_cap = std::max(static_cast<unsigned char>(capacity_ << 1), min_size);
    int* tmp = new int[new_cap];
    std::memcpy(tmp, data_.ptr, capacity_ * sizeof(int));
    delete[] data_.ptr;
    data_.ptr = tmp;
    capacity_ = new_cap;
  }

  inline void copy_vals_to_ptr() {
    capacity_ = __SV_MAX_STATIC * 2;
    int* tmp = new int[capacity_];
    for (int i = 0; i < __SV_MAX_STATIC; ++i) tmp[i] = data_.vals[i];
    data_.ptr = tmp;
  }

  inline void push_back(int v) {
    if (size_ < __SV_MAX_STATIC) {
      data_.vals[size_] = v;
      ++size_;
      return;
    } else if (size_ == __SV_MAX_STATIC) {
      copy_vals_to_ptr();
    } else if (size_ == capacity_) {
      ensure_capacity(size_ + 1);
    }
    data_.ptr[size_] = v;
    ++size_;
  }

  int& back() { return this->operator[](size_ - 1); }
  const int& back() const { return this->operator[](size_ - 1); }
  int& front() { return this->operator[](0); }
  const int& front() const { return this->operator[](0); }

  void resize(size_t s, int v = 0) {
    if (s <= __SV_MAX_STATIC) {
      if (size_ > __SV_MAX_STATIC) {
        int tmp[__SV_MAX_STATIC];
        for (int i = 0; i < s; ++i) tmp[i] = data_.ptr[i];
        delete[] data_.ptr;
        for (int i = 0; i < s; ++i) data_.vals[i] = tmp[i];
        size_ = s;
        return;
      }
      if (s <= size_) {
        size_ = s;
        return;
      } else {
        for (int i = size_; i < s; ++i)
          data_.vals[i] = v;
        size_ = s;
        return;
      }
    } else {
      if (size_ <= __SV_MAX_STATIC)
        copy_vals_to_ptr();
      if (s > capacity_)
        ensure_capacity(s);
      if (s > size_) {
        for (int i = size_; i < s; ++i)
          data_.ptr[i] = v;
      }
      size_ = s;
    }
  }

  int& operator[](size_t i) {
    if (size_ <= __SV_MAX_STATIC) return data_.vals[i];
    return data_.ptr[i];
  }

  const int& operator[](size_t i) const {
    if (size_ <= __SV_MAX_STATIC) return data_.vals[i];
    return data_.ptr[i];
  }

  bool operator==(const SmallVector& o) const {
    if (size_ != o.size_) return false;
    if (size_ <= __SV_MAX_STATIC) {
      for (size_t i = 0; i < size_; ++i)
        if (data_.vals[i] != o.data_.vals[i]) return false;
      return true;
    } else {
      for (size_t i = 0; i < size_; ++i)
        if (data_.ptr[i] != o.data_.ptr[i]) return false;
      return true;
    }
  }

 private:
  unsigned char capacity_;  // only defined when size_ >= __SV_MAX_STATIC
  unsigned char size_;
  union StorageType {
    int vals[__SV_MAX_STATIC];
    int* ptr;
  };
  StorageType data_;

};

inline bool operator!=(const SmallVector& a, const SmallVector& b) {
  return !(a==b);
}

#endif