File: histogram_writer.h

package info (click to toggle)
kmc 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,716 kB
  • sloc: cpp: 38,308; python: 664; makefile: 216; perl: 179; sh: 34
file content (109 lines) | stat: -rw-r--r-- 2,014 bytes parent folder | download
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
/*
  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: 3.2.4
  Date   : 2024-02-09
*/

#ifndef _HISTOGRAM_WRITER_H
#define _HISTOGRAM_WRITER_H

#include "defs.h"
#include "config.h"
#include <vector>
#include <fstream>


class CHistogramWriterBase
{
private:
	std::string& file_src;
	uint32 cutoff_max;
	uint32 cutoff_min;
	std::vector<uint64> counters;
protected:
	void Init()
	{
		counters.resize(cutoff_max + 1);
	}
	void ProcessCounter(uint32 counter)
	{
		if (counter >= cutoff_min && counter <= cutoff_max)
			counters[counter]++;
	}
	void Finish()
	{
		std::ofstream file(file_src);
		if (!file)
		{
			std::cerr << "Error: cannot open file: " << file_src << "\n";
			exit(1);
		}
		for (uint32 i = cutoff_min; i <= cutoff_max; ++i)
		{
			file << i << "\t" << counters[i] << "\n";
		}
		file.close();
	}
	CHistogramWriterBase(std::string& file_src, uint32 cutoff_max, uint32 cutoff_min):
		file_src(file_src),
		cutoff_max(cutoff_max),
		cutoff_min(cutoff_min)
	{
	}
};

template<typename KMCDB> class CHistogramWriter : public CHistogramWriterBase
{
	KMCDB& kmcdb;
		
public:
	CHistogramWriter(KMCDB& kmcdb) :
		CHistogramWriterBase(CConfig::GetInstance().output_desc.file_src, CConfig::GetInstance().output_desc.cutoff_max, CConfig::GetInstance().output_desc.cutoff_min),
		kmcdb(kmcdb)
	{

	}
	bool Process()
	{
		Init();
		uint32 counter;
		while (kmcdb.NextCounter(counter))
		{
			ProcessCounter(counter);
		}
		Finish();
		return true;
	}
};


class CHistogramWriterForTransform : public CHistogramWriterBase
{
public:
	CHistogramWriterForTransform(CTransformOutputDesc& output_desc) :
		CHistogramWriterBase(output_desc.file_src, output_desc.cutoff_max, output_desc.cutoff_min)
	{

	}

	void Init()
	{
		CHistogramWriterBase::Init();
	}

	void PutCounter(uint32 counter)
	{
		ProcessCounter(counter);
	}

	void Finish()
	{
		CHistogramWriterBase::Finish();
	}
};

#endif