-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzPairsDB.cpp
129 lines (97 loc) · 2.87 KB
/
zPairsDB.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
#include "stdafx.h"
#include "zPairsDB.h"
//------------------------------------------------------------------------------------------
zPairsDB::zPairsDB()
{
m_Data.reserve(10000000);
}
//------------------------------------------------------------------------------------------
zPairsDB::~zPairsDB()
{
_clear_data();
}
//------------------------------------------------------------------------------------------
void zPairsDB::add_tripple(int word1, int word2, int word3, bool is_first)
{
zWordPair *p = 0;
//находим, либо создаем zWordPair
int id = get_pair_id(word1, word2);
if(id != INT_MAX)
p = m_Data[id];
else
{
p = new zWordPair();
p->set_words(word1, word2);
m_Data.push_back(p);
m_PairsMap.insert(std::make_pair(p, (int)m_Data.size() - 1));
}
//добавляем word3 и если надо - увеличиваем first_cnt
p->add_word3(word3);
if(is_first)
p->inc_first_cnt();
}
//------------------------------------------------------------------------------------------
//return INT_MAX if not exist
int zPairsDB::get_pair_id(int word1, int word2)
{
static zWordPair buf;
buf.set_words(word1, word2);
zPairsMap::iterator it = m_PairsMap.find(&buf);
if(it == m_PairsMap.end())
return INT_MAX;
else
return it->second;
}
//------------------------------------------------------------------------------------------
/*zWordPair *zPairsDB::get_pair_by_id(int word1, int word2)
{
int id = get_pair_id(word1, word2);
return m_Data[id];
}*/
//------------------------------------------------------------------------------------------
bool zPairsDB::load_from_file(const std::wstring &file_name)
{
return true;
}
//------------------------------------------------------------------------------------------
bool zPairsDB::save_to_file(const std::wstring &file_name)
{
return true;
}
//------------------------------------------------------------------------------------------
int zPairsDB::get_rand_word(int word1, int word2)
{
rel_assert(0); //todo
return INT_MAX;
}
//------------------------------------------------------------------------------------------
void zPairsDB::_clear_data()
{
for(size_t i = 0; i < m_Data.size(); i++)
{
delete m_Data[i];
}
}
//------------------------------------------------------------------------------------------
//статистика для отладки
std::string zPairsDB::get_stat_str()
{
std::string ret;
ret += zstr::fmt("size = %d. ", (int)m_Data.size());
int max = 0;
int total = 0;
int total_w3maps = 0;
for(zPairsMap::iterator i = m_PairsMap.begin(); i != m_PairsMap.end(); i++)
{
zWordPair *p = i->first;
int n = p->get_cnt_4_test();
total += n;
if(n > max)
max = n;
total_w3maps += p->get_word3_map_size_4_test();
}
ret += zstr::fmt("max = %d. ", max);
ret += zstr::fmt("total = %d. ", total);
ret += zstr::fmt("total_w3maps = %d. ", total_w3maps);
return ret;
}