-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconverter.cc
79 lines (61 loc) · 1.59 KB
/
converter.cc
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
#include "converter.h"
#include<utility>
#include<iostream>
#include<cstdlib>
converter::converter(){
m_curidx = 0;
m_idx2str.clear();
m_str2idx.clear();
}
converter::~converter(){
#ifdef DEBUG
std::cout << "max cout " << m_curidx << std::endl;
#endif
}
void converter::add_new( std::string str ){
m_curidx++;
//add to string to idx
std::pair<std::map<std::string, unsigned int>::iterator, bool> ret;
ret = m_str2idx.insert( std::pair<std::string, unsigned int>(str, m_curidx) );
if( ! ret.second ){
std::cerr << "converter insert Error"
<< std::endl;
exit(1);
}
std::pair<std::map<unsigned int, std::string>::iterator, bool> ret2;
ret2 = m_idx2str.insert( std::pair<unsigned int, std::string>( m_curidx, str ) );
if( ! ret2.second ){
std::cerr << "converter 2 insert errror"
<< std::endl;
exit(1);
}
#ifdef DEBUG
std::cout << "converter::add_new\tnew string : " << str << std::endl;
std::cout << "converter::add_new\t" << "string count\t" << m_curidx << std::endl;
#endif
}
unsigned int converter::str2idx( std::string pingying ){
if( !m_str2idx.count( pingying ) ){
add_new( pingying );
}
return m_str2idx[pingying];
}
unsigned int converter::str2idx_c( std::string pinyin ){
if( m_str2idx.count( pinyin ) ){
return m_str2idx[pinyin];
} else {
return 0;
}
}
std::string converter::idx2str( unsigned int idx){
return m_idx2str[idx];
}
unsigned int converter::count() const{
return m_curidx;
}
converter::iterator converter::begin() const{
return m_idx2str.begin();
}
converter::iterator converter::end() const{
return m_idx2str.end();
}