-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathKmerIntPair.cpp
More file actions
54 lines (42 loc) · 1.46 KB
/
KmerIntPair.cpp
File metadata and controls
54 lines (42 loc) · 1.46 KB
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
#include <cstdlib>
#include "Kmer.hpp"
#include "KmerIntPair.hpp"
KmerIntPair::KmerIntPair(const Kmer &km, unsigned int val) {
SetKey(km);
SetVal(val);
}
void KmerIntPair::SetVal(unsigned int val) {
char val8 = (val > 0xFF) ? 0xFF : (char)val;
//memcpy(&this->v + KmerIntPair::IntOffset, &val8, sizeof(uint8_t));
this->v[KmerIntPair::IntOffset] = val8;
}
// use: b = p.ParallelIncrement()
// pre: 0 <= p.GetVal() <= 255
// post: if b is true then p has been atomically incremented
// and p.GetValue() < 255 prior to the increment
// if b is false, the value was 255
bool KmerIntPair::ParallelIncrement() {
unsigned int val = this->v[KmerIntPair::IntOffset];
unsigned int newval;
do {
val = this->v[KmerIntPair::IntOffset]; // read new value
if (val == 255) {
return false; // someone else beat us to it
}
newval = val + 1;
} while (!__sync_bool_compare_and_swap(this->v+KmerIntPair::IntOffset,val, newval));
return true;
}
unsigned int KmerIntPair::GetVal() const {
//uint8_t tmp = *reinterpret_cast<const uint8_t*>(this+KmerIntPair::IntOffset);
return (uint8_t)this->v[KmerIntPair::IntOffset];
}
const Kmer& KmerIntPair::GetKey() const {
return *reinterpret_cast<const Kmer*>(this + KmerIntPair::KmerOffset);
}
void KmerIntPair::SetKey(const Kmer& km) {
memcpy(this, &km, sizeof(Kmer));
}
void SetKmerKey::operator()(KmerIntPair *value, const Kmer& km) {
memcpy(value + KmerIntPair::KmerOffset, &km, sizeof(Kmer));
}