-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceHashMap.cpp
79 lines (68 loc) · 2.17 KB
/
SourceHashMap.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
//
// SourceHashMap.cpp
// BitmapIndex
//
// Created on 09.02.13.
// Copyright (c) 2013 Christoph Schaefer. All rights reserved.
//
#include "SourceHashMap.h"
#include <stdexcept>
#include <fstream>
#include <sstream>
SourceHashMap::SourceHashMap() :
_sourceHashMapPtr(std::make_shared<std::unordered_map<std::string, RangePair>>()),
_currentSource(""),
_firstOccurrenceOfCurrentSource(0)
{
}
void SourceHashMap::add(const std::string& source, const uint32_t& rowNr)
{
if(source != _currentSource) {
if(rowNr != 0) {
RangePair fromToPair(_firstOccurrenceOfCurrentSource, rowNr);
std::pair<std::string, RangePair> sourceRange(_currentSource, fromToPair);
_sourceHashMapPtr->insert(sourceRange);
}
_currentSource = source;
_firstOccurrenceOfCurrentSource = rowNr;
}
}
void SourceHashMap::flush(const uint32_t& rowNr)
{
RangePair fromToPair(_firstOccurrenceOfCurrentSource, rowNr);
std::pair<const std::string, RangePair> sourceRange(_currentSource, fromToPair);
_sourceHashMapPtr->insert(sourceRange);
}
const RangePair SourceHashMap::getRangeOfSource(const std::string& source) const {
try {
return _sourceHashMapPtr->at(source);
}
catch (std::out_of_range& oor) {
return RangePair(0,0);
}
return RangePair(0,0);
}
void SourceHashMap::writeToDisc(const std::string& path) {
std::ofstream ofs;
ofs.open(path + "source_range", std::ios::out);
auto iterEnd = _sourceHashMapPtr->end();
auto iter = _sourceHashMapPtr->begin();
for (; iter != iterEnd; ++iter){
ofs << iter->first << '\t' << iter->second.first << '\t' << iter->second.second << std::endl;
}
ofs.close();
}
void SourceHashMap::readFromDisc(const std::string& path) {
std::ifstream ifs;
ifs.open(path + "source_range", std::ios::in);
std::string source;
uint32_t from, to;
for(std::string line; getline(ifs, line);) {
std::istringstream ss(line);
ss >> source >> from >> to;
RangePair fromToPair(from, to);
std::pair<std::string, RangePair> sourceRange(source, fromToPair);
_sourceHashMapPtr->insert(sourceRange);
}
ifs.close();
}