/
MatchHash.h
168 lines (130 loc) · 4.48 KB
/
MatchHash.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
* This file is part of SAP.
*
* SAP 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.
*
* SAP 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 SAP. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#ifndef _HASH_H
#define _HASH_H
#include "MatchStructures.h"
#define HASH_NODE_SIZE 5
/*
* Defines the size of each node of class MatchHash
*/
#define MAX_HASH_SIZE 100000000
/*
* Defines the maximum size of MatchHash
*/
/*
* A virtual class
*/
class Hash{
public:
struct Result{
int start;
int seq;
Result *next;
Result(const Result &res);
Result(int seq, int start);
};
public:
Hash();
~Hash();
static void deleteResult(Result *&res);
static unsigned long calcHashValue(const char *start, const char *end, unsigned long (*funcDnaToInt)(char c) = 0);
virtual Result* exactFind(const char *s, unsigned len) const = 0;
virtual Result* oneMismatchFind(const char *s, unsigned len) const = 0;
virtual void insert(Dna *seq, int start, int end) = 0;
virtual void insert(Dna *seq, int start, int end, unsigned long hashValue) = 0;
virtual void remove(Dna *seq, int start, int end) = 0;
virtual void remove(Dna *seq, int start, int end, unsigned long hashValue) = 0;
};
class MatchHash : public Hash{
protected:
struct HashElement{
friend class MatchHash;
unsigned long hashValue;
HashElement *next;
int seq;
int start;
HashElement(Dna *seq, int start, int end);
HashElement(Dna *seq, int start, int end, unsigned long hashValue);
~HashElement();
bool operator == (const HashElement &element) const;
};
public:
MatchHash();
~MatchHash();
Result* exactFind(const char *s, unsigned len) const;
Result* oneMismatchFind(const char *s, unsigned len) const;
void insert(Dna *seq, int start, int end);
void insert(Dna *seq, int start, int end, unsigned long hashValue);
virtual void remove(Dna *seq, int start, int end);
virtual void remove(Dna *seq, int start, int end, unsigned long hashValue);
private:
virtual void find_p(unsigned hashVal, Result *&ret) const = 0;
virtual bool insert_p(HashElement *element) = 0;
virtual void remove_p(Dna *seq, int start, unsigned long hashValue) = 0;
};
class BufferedMatchHash : public Hash{
protected:
struct HashElement{
friend class BufferedMatchHash;
unsigned long hashValue;
unsigned next;
int seq;
int start;
HashElement();
HashElement(Dna *seq, int start, int end);
HashElement(Dna *seq, int start, int end, unsigned long hashValue);
~HashElement();
bool operator == (const HashElement &element) const;
};
public:
BufferedMatchHash(unsigned hashSize);
~BufferedMatchHash();
Result* exactFind(const char *s, unsigned len) const;
Result* oneMismatchFind(const char *s, unsigned len) const;
void insert(Dna *seq, int start, int end);
void insert(Dna *seq, int start, int end, unsigned long hashValue);
void remove(Dna *seq, int start, int end);
void remove(Dna *seq, int start, int end, unsigned long hashValue);
protected:
HashElement *_elements;
private:
unsigned _hashSize, _currentHashElement;
virtual void find_p(unsigned long hashVal, Result *&ret) const = 0;
virtual void insert_p(unsigned elementId) = 0;
};
class BinaryHash : public MatchHash{
public:
BinaryHash(unsigned long binSize);
~BinaryHash();
private:
HashElement **_elements;
unsigned long _binSize, _binAnd, _size;
void find_p(unsigned hashVal, Result *&ret) const;
bool insert_p(HashElement *element);
void remove_p(Dna *seq, int start, unsigned long hashVal);
};
class BufferedBinaryHash : public BufferedMatchHash{
public:
BufferedBinaryHash(unsigned long hashSize, unsigned binSize);
~BufferedBinaryHash();
private:
unsigned *_elementBases;
unsigned long _binSize, _binAnd, _size;
void find_p(unsigned long hashVal, Result *&ret) const;
void insert_p(unsigned elementId);
};
#endif