-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_function_way.cpp
More file actions
186 lines (141 loc) · 3.71 KB
/
trie_function_way.cpp
File metadata and controls
186 lines (141 loc) · 3.71 KB
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
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define ll long long
#define way_size 256
fstream file,file_link;
vector <string> prefix;
class way_link{
public:
char id[15];
char longitude[25], latitude[25];
ll next;
int delimiter;
way_link()
{
next=-1;
delimiter=-1;
}
};
class way{
public :
ll ptr[256];
ll wayPtr,wayPtr_end;
char admin_level[3];
char way_id[15];
way()
{
for(int i=0;i<256;i++)
{ptr[i]=-1;}
wayPtr=-1;
wayPtr_end=-1;
}
};
vector <string> id_list;
vector <string> long_list;
vector <string> lat_list;
vector <int> delimiter_list;
void node_with_wayid(ll curr_root){
id_list.clear();
lat_list.clear();
file_link.seekp(curr_root);file_link.seekg(curr_root);
way_link link_list;
//cout<<root.nodePtr<<" "<<root.nodePtr_end<<endl;
while(1){
file_link.read((char*)&link_list,sizeof(way_link));
string s(link_list.id);
id_list.push_back(s);
string lo(link_list.longitude);
string la(link_list.latitude);
lat_list.push_back(la);
lat_list.push_back(lo);
delimiter_list.push_back(link_list.delimiter);
if( link_list.next==-1){break;}
//cout<<link_list.id<<endl;
file_link.seekp(link_list.next);file_link.seekg(link_list.next);
}
return ;
}
void display_prefix(string s,int ind,ll curr_root){
if( ind>0 ){
ll curr_child=0;
for(int i=0;i<s.length();i++){
file.seekp(curr_root);file.seekg(curr_root);
way root;
file.read((char*)&root,sizeof(way));
if(root.ptr[s[i]-0]==-1 ){
return ;
}
else{
curr_child=root.ptr[s[i]-0];
}
curr_root=curr_child;
ind--;
//cout<<curr_root<<" ";
}
}
file.seekp(curr_root);file.seekg(curr_root);
way root;
file.read((char*)&root,sizeof(way));
if(root.wayPtr!=-1){
prefix.push_back(s);
}
for(int i=0;i<way_size;i++){
if(root.ptr[i]==-1 ){
//return ;
}
else{
string si=s;
si+=(0+i);
display_prefix(si,0,root.ptr[i] );
}
}
return;
}
way search_trie(string s){
ll curr_child=0,curr_root=0;
for(int i=0;i<s.length();i++){
file.seekp(curr_root);file.seekg(curr_root);
way root;
file.read((char*)&root,sizeof(way));
if(root.ptr[s[i]-0]==-1 ){
way x;
return x;
}
else{
curr_child=root.ptr[s[i]-0];
if(i==s.length()-1){
way child;
file.seekp(curr_child);file.seekg(curr_child);
file.read((char*)&child,sizeof(way));
return child;
}
}
curr_root=curr_child;
//cout<<curr_root<<endl;
}
}
int main(){
long long curr_child=0,curr_root=0,m=0;
file.open("trie_way.txt",ios::app|ios::in|ios::out);
file_link.open("trie_link_way.txt",ios::app|ios::in|ios::out);
curr_root=0;
file.seekp(0,ios::end);
curr_child=file.tellp();
file.seekp(curr_root);file.seekg(curr_root);
// search
cout<<endl<<endl;
way x= search_trie("Hapur Road");
cout<<x.wayPtr<<" "<<x.wayPtr_end<<endl;
string temp="B"; //prefix search
display_prefix(temp,temp.length(),0);
for(int i=0;i<prefix.size();i++){
// cout<<prefix[i]<<" "<<prefix.size()<<endl;
}
file.close();
node_with_wayid(x.wayPtr);
for(int i=0;i<id_list.size();i++){
cout<<id_list[i]<<" "<<delimiter_list[i]<<endl;
}
return 0;
}