-
Notifications
You must be signed in to change notification settings - Fork 1
/
small_vector.hpp
91 lines (81 loc) · 2.02 KB
/
small_vector.hpp
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
/**
* shark - Mapping-free filtering of useless RNA-Seq reads
* Copyright (C) 2020 Tamara Ceccato, Luca Denti, Yuri Pirola, Marco Previtali
*
* This file is part of shark.
*
* shark is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* shark is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with shark; see the file LICENSE. If not, see
* <https://www.gnu.org/licenses/>.
**/
#include <cstdint>
#include <vector>
struct small_vector_t {
union {
struct {
uint8_t flag;
uint8_t size;
uint16_t arr[3];
} s;
std::vector<uint16_t>* l;
} v;
small_vector_t() {
v.s.flag = 1u;
v.s.size = 0;
}
~small_vector_t() {
if ((v.s.flag & 0x1) == 0) {
delete v.l;
}
}
void push_back(uint16_t x) {
if ((v.s.flag & 0x1) != 0) {
if (v.s.size < 3) v.s.arr[v.s.size++] = x;
else {
std::vector<uint16_t>* ptr = new std::vector<uint16_t>(v.s.arr, v.s.arr + 3);
ptr->push_back(x);
v.l = ptr;
}
} else {
v.l->push_back(x);
}
}
size_t size() const {
if ((v.s.flag & 0x1) != 0) {
return v.s.size;
} else {
return v.l->size();
}
}
uint16_t last() const {
if ((v.s.flag & 0x1) != 0) {
return v.s.arr[v.s.size - 1];
} else {
return v.l->back();
}
}
const uint16_t* begin() const {
if ((v.s.flag & 0x1) != 0) {
return v.s.arr;
} else {
return v.l->data();
}
}
const uint16_t* end() const {
if ((v.s.flag & 0x1) != 0) {
return v.s.arr + v.s.size;
} else {
return v.l->data() + v.l->size();
}
}
};