File: bundle.h

package info (click to toggle)
kmc 2.3%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,412 kB
  • sloc: cpp: 17,316; perl: 178; makefile: 90; sh: 16
file content (233 lines) | stat: -rw-r--r-- 5,406 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
  This file is a part of KMC software distributed under GNU GPL 3 licence.
  The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
  
  Authors: Marek Kokot
  
  Version: 2.3.0
  Date   : 2015-08-21
*/

#ifndef _BUNDLE_H
#define _BUNDLE_H
#include "defs.h"
#include "kmer.h"


//************************************************************************************************************
// CBundle and CInput are CORE classes of this application. CInputs are nodes of binary tree which 
// represent operations. Leafs of this tree are kmc database (1 or 2) inputs (sets of k-mers).
// Each node represents an operation like intersection, subtraction, etc. Because this class is abstract 
// calling virtual method to get each single k-mer may be costly. To prevent high const, between tree nodes there
//are instances of CBundle which contains buffer of k-mers and its counters.
//
// The algorithm works as follow (conceptually):
// Build a tree with CBundles and CInputs, as a root take a some output writer (kmc database). 
// Root has its bundle and get from it k-mers, but at the beginning there is nothing in bundle. Each bundle 
// contains pointer to CInput below in tree. The CBundle is getting k-mers from its CInput. 
// This is repeated from top of tree to leafs
//************************************************************************************************************

//Forward declaration
template<unsigned SIZE> class CBundle;

//************************************************************************************************************
// CInput - Base abstract class representing data source for CBundle class
//************************************************************************************************************
template<unsigned SIZE> class CInput
{
public:
	virtual void NextBundle(CBundle<SIZE>& bundle) = 0;
	virtual void IgnoreRest() = 0;
	bool Finished(){ return finished; }
	virtual ~CInput(){}
protected:
	bool finished = false;

};



//************************************************************************************************************
// CBundleData - class containing a buffer of k-mers and its counters. 
//************************************************************************************************************
template<unsigned SIZE> class CBundleData
{
public:
	CBundleData() : insert_pos(0), get_pos(0), size(BUNDLE_CAPACITY)
	{
		kmers = new CKmer<SIZE>[size]; 
		counters = new uint32[size];
	}
	~CBundleData()
	{
		delete[] kmers;
		delete[] counters;
	}
	CBundleData(CBundleData<SIZE>&& rhs):
		insert_pos(rhs.insert_pos), get_pos(rhs.get_pos), size(rhs.size), kmers(rhs.kmers), counters(rhs.counters)
	{
		rhs.counters = nullptr;
		rhs.kmers = nullptr;
		rhs.get_pos = rhs.size = rhs.insert_pos = 0;
	}

	CBundleData<SIZE>& operator=(CBundleData<SIZE>&& rhs)
	{
		if (this != &rhs)
		{
			delete[] kmers;
			delete[] counters;

			kmers = rhs.kmers;
			counters = rhs.counters;
			get_pos = rhs.get_pos;
			size = rhs.size;
			insert_pos = rhs.insert_pos;

			rhs.counters = nullptr;
			rhs.kmers = nullptr;
			rhs.get_pos = rhs.size = rhs.insert_pos = 0;
		}
		return *this;
	}

	CBundleData(const CBundleData<SIZE>&) = delete;
	CBundle<SIZE>& operator=(const CBundleData<SIZE>&) = delete;

	CKmer<SIZE>& TopKmer() const
	{
		return kmers[get_pos];
	}

	uint32& TopCounter() const
	{
		return counters[get_pos];
	}

	bool Full()
	{
		return insert_pos >= size;
	}

	bool Empty()
	{
		return get_pos >= insert_pos;
	}

	void Insert(CKmer<SIZE>& kmer, uint32 counter)
	{
		kmers[insert_pos] = kmer;
		counters[insert_pos++] = counter;
	}
	void Pop()
	{
		++get_pos;
	}

	void Clear()
	{
		insert_pos = get_pos = 0;
	}

private:
	friend class CBundle<SIZE>;	
	uint32 insert_pos, get_pos, size;
	CKmer<SIZE>* kmers;
	uint32* counters;
};



//************************************************************************************************************
// CBundle - connector between CBundleData and CInput
//************************************************************************************************************
template<unsigned SIZE> class CBundle
{
public:
	CBundle(CInput<SIZE>* input) : input(input)
	{
		
	}

	CKmer<SIZE>& TopKmer() const
	{
		return data.TopKmer();
	}

	uint32& TopCounter() const
	{
		return data.TopCounter();
	}


	bool Full()
	{		
		return data.Full();
	}
	void Insert(CKmer<SIZE>& kmer, uint32 counter)
	{		
		data.Insert(kmer, counter);
	}
	void Pop()
	{		
		data.Pop();
	}
	~CBundle()
	{
		delete input;
	}

	bool Empty()
	{
		return data.Empty();
	}
	
	CBundleData<SIZE>& Data() {
		return data;
	}

	inline bool Finished();

	void IgnoreRest()
	{		
		input->IgnoreRest();
	}
	uint32 Size()
	{
		return data.insert_pos;
	}

private:
	CBundleData<SIZE> data;
	CInput<SIZE>* input;
	bool finished = false;
};

//************************************************************************************************************
template<unsigned SIZE> inline bool CBundle<SIZE>::Finished()
{	
	if (finished)
		return true;
	if (data.get_pos >= data.insert_pos)
	{
		if (input->Finished())
		{
			finished = true;
			return true;
		}
		data.get_pos = data.insert_pos = 0;
		input->NextBundle(*this); 
		if (data.insert_pos == 0)//Because maybe NextBundle did not add anything, which means there is nothing to take
		{
			finished = true;
			return true;
		}
	}
	return false;
}

#endif


// ***** EOF