A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2017 Lucas Czech
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 <stddef.h>
35 #include <stdint.h>
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
TwobitVector & operator=(TwobitVector const &)=default
void insert_at(size_t index, ValueType value)
Insert a value at a position.
bool operator==(TwobitVector const &other) const
Equality operator.
size_t data_size() const
Return the number of words (of type WordType) that are used to store the values in the vector...
static const size_t kValuesPerWord
Constant that holds the number of values (of tyoe ValueType) that are stored in a single word in the ...
size_t size() const
Return the size of the vector, that is, how many values (of type ValueType) it currently holds...
WordType const & data_at(size_t index) const
Return a single word of the vector.
void remove_at(size_t index)
Remove the value at a position.
uint64_t WordType
Underlying word type for the bitvector.
void set(size_t index, ValueType value)
Set a value at a position in the vector.
ValueType operator[](size_t index) const
Alias for get().
WordType hash() const
Calculate a hash value of the vector, based on its size() and the xor of all its words.
bool validate() const
Validation function that checks some basic invariants.
void clear()
Clear the vector, so that it contains no data.
ValueType
Value Type enumeration for the elements of a TwobitVector.
bool operator!=(TwobitVector const &other) const
Inequality operator, opposite of operator==().