A library for working with phylogenetic and population genetic data.
v0.27.0
twobit_vector.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_MATH_TWOBIT_VECTOR_H_
2 #define GENESIS_UTILS_MATH_TWOBIT_VECTOR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <cstddef>
35 #include <cstdint>
36 #include <vector>
37 
38 namespace genesis {
39 namespace utils {
40 
42 {
43 public:
44 
45  // -------------------------------------------------------------------------
46  // Typedefs and Constants
47  // -------------------------------------------------------------------------
48 
55  using WordType = uint64_t;
56 
66  enum class ValueType : WordType {
67  A = 0,
68  C = 1,
69  G = 2,
70  T = 3
71  };
72 
79  static const size_t kValuesPerWord = sizeof( WordType ) * 8 / 2;
80 
81  // -------------------------------------------------------------------------
82  // Constructors and Rule of Five
83  // -------------------------------------------------------------------------
84 
85  TwobitVector() = default;
86  TwobitVector( size_t size );
87 
88  ~TwobitVector() = default;
89 
90  TwobitVector( TwobitVector const& ) = default;
91  TwobitVector( TwobitVector&& ) = default;
92 
93  TwobitVector& operator= ( TwobitVector const& ) = default;
94  TwobitVector& operator= ( TwobitVector&& ) = default;
95 
96  // -------------------------------------------------------------------------
97  // Accessors
98  // -------------------------------------------------------------------------
99 
100  size_t size() const;
101  size_t data_size() const;
102 
103  ValueType get( size_t index ) const;
104 
105  ValueType operator [] ( size_t index ) const;
106 
107  WordType const& data_at( size_t index ) const;
108  WordType& data_at( size_t index );
109 
110  WordType hash() const;
111 
112  // -------------------------------------------------------------------------
113  // Operators
114  // -------------------------------------------------------------------------
115 
116  bool operator == ( TwobitVector const& other ) const;
117  bool operator != ( TwobitVector const& other ) const;
118 
119  bool validate() const;
120 
121  // -------------------------------------------------------------------------
122  // Modifiers
123  // -------------------------------------------------------------------------
124 
125  void set( size_t index, ValueType value );
126 
127  void insert_at( size_t index, ValueType value );
128  void remove_at( size_t index );
129 
130  void clear();
131 
132  // -------------------------------------------------------------------------
133  // Data Members
134  // -------------------------------------------------------------------------
135 
136 private:
137 
138  static const WordType all_0_;
139  static const WordType all_1_;
140 
141  static const WordType bit_mask_[ kValuesPerWord ];
142  static const WordType ones_mask_[ kValuesPerWord ];
143 
144  size_t size_ = 0;
145  std::vector<WordType> data_;
146 
147 };
148 
149 } // namespace utils
150 } // namespace genesis
151 
152 #endif // include guard
genesis::utils::TwobitVector::data_at
WordType const & data_at(size_t index) const
Return a single word of the vector.
Definition: twobit_vector.cpp:176
genesis::utils::TwobitVector
Definition: twobit_vector.hpp:41
genesis::utils::TwobitVector::data_size
size_t data_size() const
Return the number of words (of type WordType) that are used to store the values in the vector.
Definition: twobit_vector.cpp:140
genesis::utils::TwobitVector::WordType
uint64_t WordType
Underlying word type for the bitvector.
Definition: twobit_vector.hpp:55
genesis::utils::TwobitVector::insert_at
void insert_at(size_t index, ValueType value)
Insert a value at a position.
Definition: twobit_vector.cpp:290
genesis::utils::TwobitVector::set
void set(size_t index, ValueType value)
Set a value at a position in the vector.
Definition: twobit_vector.cpp:269
genesis::utils::TwobitVector::~TwobitVector
~TwobitVector()=default
genesis::utils::TwobitVector::operator!=
bool operator!=(TwobitVector const &other) const
Inequality operator, opposite of operator==().
Definition: twobit_vector.cpp:230
genesis::utils::TwobitVector::validate
bool validate() const
Validation function that checks some basic invariants.
Definition: twobit_vector.cpp:241
genesis::utils::TwobitVector::ValueType::A
@ A
genesis::utils::TwobitVector::operator[]
ValueType operator[](size_t index) const
Alias for get().
Definition: twobit_vector.cpp:165
genesis::utils::TwobitVector::ValueType::C
@ C
genesis::utils::TwobitVector::clear
void clear()
Clear the vector, so that it contains no data.
Definition: twobit_vector.cpp:405
genesis::utils::TwobitVector::operator==
bool operator==(TwobitVector const &other) const
Equality operator.
Definition: twobit_vector.cpp:214
genesis::utils::TwobitVector::ValueType
ValueType
Value Type enumeration for the elements of a TwobitVector.
Definition: twobit_vector.hpp:66
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::TwobitVector::kValuesPerWord
static const size_t kValuesPerWord
Constant that holds the number of values (of tyoe ValueType) that are stored in a single word in the ...
Definition: twobit_vector.hpp:79
genesis::utils::TwobitVector::remove_at
void remove_at(size_t index)
Remove the value at a position.
Definition: twobit_vector.cpp:342
genesis::utils::TwobitVector::TwobitVector
TwobitVector()=default
genesis::utils::TwobitVector::operator=
TwobitVector & operator=(TwobitVector const &)=default
genesis::utils::TwobitVector::hash
WordType hash() const
Calculate a hash value of the vector, based on its size() and the xor of all its words.
Definition: twobit_vector.cpp:198
genesis::utils::TwobitVector::size
size_t size() const
Return the size of the vector, that is, how many values (of type ValueType) it currently holds.
Definition: twobit_vector.cpp:131
genesis::utils::TwobitVector::get
ValueType get(size_t index) const
Get the value at a position in the vector.
Definition: twobit_vector.cpp:149
genesis::utils::TwobitVector::ValueType::G
@ G
genesis::utils::TwobitVector::ValueType::T
@ T