forked from marbl/Mash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashSet.cpp
136 lines (120 loc) · 2.85 KB
/
HashSet.cpp
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
// Copyright © 2015, Battelle National Biodefense Institute (BNBI);
// all rights reserved. Authored by: Brian Ondov, Todd Treangen,
// Sergey Koren, and Adam Phillippy
//
// See the LICENSE.txt file included with this software for license information.
#include "HashSet.h"
#include <utility>
uint32_t HashSet::count(hash_u hash) const
{
if ( use64 )
{
if ( hashes64.count(hash.hash64) )
{
return hashes64.at(hash.hash64);
}
else
{
return 0;
}
}
else
{
if ( hashes32.count(hash.hash32) )
{
return hashes32.at(hash.hash32);
}
else
{
return 0;
}
}
}
void HashSet::erase(hash_u hash)
{
if ( use64 )
{
hashes64.erase(hash.hash64);
}
else
{
hashes32.erase(hash.hash32);
}
}
void HashSet::insert(hash_u hash, uint32_t count)
{
if ( use64 )
{
hash64_t hash64 = hash.hash64;
if ( hashes64.count(hash64) )
{
hashes64[hash64] = hashes64.at(hash64) + count;
}
else
{
hashes64[hash64] = count;
}
}
else
{
hash32_t hash32 = hash.hash32;
if ( hashes32.count(hash32) )
{
hashes32[hash32] = hashes32.at(hash32) + count;
}
else
{
hashes32[hash32] = count;
}
}
}
void HashSet::toHashList(HashList & hashList, std::vector<uint32_t> & counts) const
{
if ( use64 )
{
typedef std::pair<hash64_t, uint32_t> hashCount64;
std::vector <hashCount64> sortList;
for ( auto i = hashes64.begin(); i != hashes64.end(); i++ )
{
sortList.push_back(hashCount64(i->first, i->second));
}
std::sort(sortList.begin(), sortList.end(), [](hashCount64 a, hashCount64 b){return a.first < b.first;});
for ( auto i = sortList.begin(); i != sortList.end(); i++ )
{
hashList.push_back64(i->first);
counts.push_back(i->second);
}
}
else
{
typedef std::pair<hash32_t, uint32_t> hashCount32;
std::vector <hashCount32> sortList;
for ( auto i = hashes32.begin(); i != hashes32.end(); i++ )
{
sortList.push_back(hashCount32(i->first, i->second));
}
std::sort(sortList.begin(), sortList.end(), [](hashCount32 a, hashCount32 b){return a.first < b.first;});
for ( auto i = sortList.begin(); i != sortList.end(); i++ )
{
hashList.push_back32(i->first);
counts.push_back(i->second);
}
}
}
void HashSet::toHashList(HashList & hashList) const
{
if ( use64 )
{
for ( auto i = hashes64.begin(); i != hashes64.end(); i++ )
{
hashList.push_back64(i->first);
}
}
else
{
for ( auto i = hashes32.begin(); i != hashes32.end(); i++ )
{
hashList.push_back32(i->first);
}
}
}