-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrqts.cpp
631 lines (584 loc) · 21.6 KB
/
rqts.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/**
`libRQTS` is an implementation of Restricted QuadTreeS (https://github.com/cfmmoc/libRQTS/)
Licensed under The GNU General Public License v3.0 (GPLv3)
Any modification, re-utilization or copy of the source or binary format in other software or publications should mention a CITATION of this library.
Copyright (c) 2016-2018 by Authors (Jin Yan, Guanghong Gong, Ni Li and Luhao Xiao)
**/
/**@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%%%%%%%%&@@@@*,,,,,,*%&@@@@@
@@@&%#*,* @@@@@@@@@@@@@&%#*,*@@@@@@@@@@@@@@@@@* @@@* .@@
@@@. ,@@@@@@@@&@@@@@, .@@@@@@@@@@@@@@@@* @@@@. .@@@@#. @@@@@@@ @@
@@@@@ .@@@@@@* ,@@@@@@. .@@@@@@@@@@@@@@@, .@@@@@@. .@@@@. .@@@@@@@@. @@
@@@@@, .@@@@@@ ,@@@@@@. @@@@@@@@@@@@@@@@@. *@@@@@. .@@@@. &@@@@@@@. .@
@@@@@. @@@@@@ @@@@@@, @@@@@@@@@@@@@@@@@@ .,,, .@@@@. ,@@@@@@@@@. .@
@@@@@. .@@@@@@@@@@@@@@@@@. @@@@@@@@@@@@@@@@@@* .@@@@. ,@@@@@@@@@. .@
@@@@@, .@@@@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* @@@@@@@@@@@%%( .(%&@@@@@@@ *#@@&%#. @@
.@# .@@@@. @ ,, @@@@@@@@@@@@# .@@@@@@@@ ., @@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@**/
/**
@brief Core class of libRQTS, mainly for split/merge checking, invoking splitting/merging and maintaining neighbors of tiles.
**/
#include "rqts.h"
#include <sys/shm.h>
#include <string>
#include <string.h>
#include <iostream>
/**
@remarks
allocate shared memory and initialize neighbors of initial tiles
**/
libRQTS::libRQTS(void)
{
std::string copyrightinfo("libRQTS is an implementation of Restricted QuadTreeS.\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%%%%%%%%&@@@@*,,,,,,*%&@@@@@\n@@@&%#*,* @@@@@@@@@@@@@&%#*,*@@@@@@@@@@@@@@@@@* @@@* .@@\n@@@. ,@@@@@@@@&@@@@@, .@@@@@@@@@@@@@@@@* @@@@. .@@@@#. @@@@@@@ @@\n@@@@@ .@@@@@@* ,@@@@@@. .@@@@@@@@@@@@@@@, .@@@@@@. .@@@@. .@@@@@@@@. @@\n@@@@@, .@@@@@@ ,@@@@@@. @@@@@@@@@@@@@@@@@. *@@@@@. .@@@@. &@@@@@@@. .@\n@@@@@. @@@@@@ @@@@@@, @@@@@@@@@@@@@@@@@@ .,,, .@@@@. ,@@@@@@@@@. .@\n@@@@@. .@@@@@@@@@@@@@@@@@. @@@@@@@@@@@@@@@@@@* .@@@@. ,@@@@@@@@@. .@\n@@@@@, .@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@ @@@@. .@@@@. *@@@@@@@@. .@\n@@@@@ .@@@@@@@@@@@@@@@@@ .@@@@@@@@@@@@@@@@@@ .@@@@@. .@@@@. .@@&%%&@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\nAuthors:\nJin Yan ([email protected]) from Capital University of Economics and Business,\nGuanghong Gong ([email protected]) and Ni Li* ([email protected]) from Beihang University,\nLuhao Xiao ([email protected]) from Beijing ChuangQi Horizon Technology Co., Ltd.\nLicensed under The GNU General Public License v3.0 (GPLv3).\nAny modification, re-utilization or copy of the source or binary format in other software or publications should mention a CITATION (https://github.com/cfmmoc/) of this library.\n");
std::cout<<std::endl<<copyrightinfo;
initSharedM();
initRootNeighbor();
}
/**
@remarks
free shared memory
**/
libRQTS::~libRQTS(void)
{
closeSharedM();
}
/**
@remarks
initialize neighbors of six initial tiles (from u to z)
**/
void libRQTS::initRootNeighbor()
{
mHashTile2Neighbor = new std::tr1::unordered_map<std::string, CTBNeighbor*>;
std::string neighbor[8];
neighbor[0] = std::string("y"); neighbor[1] = neighbor[0];
neighbor[2] = std::string("v"); neighbor[3] = neighbor[2];
neighbor[4] = std::string("x"); neighbor[5] = neighbor[4];
neighbor[6] = std::string("w"); neighbor[7] = neighbor[6];
(*mHashTile2Neighbor)[std::string("u")] = new CTBNeighbor(neighbor);
neighbor[0] = std::string("u"); neighbor[1] = neighbor[0];
neighbor[2] = std::string("y"); neighbor[3] = neighbor[2];
neighbor[4] = std::string("w"); neighbor[5] = neighbor[4];
neighbor[6] = std::string("z"); neighbor[7] = neighbor[6];
(*mHashTile2Neighbor)[std::string("v")] = new CTBNeighbor(neighbor);
neighbor[0] = std::string("u"); neighbor[1] = neighbor[0];
neighbor[2] = std::string("v"); neighbor[3] = neighbor[2];
neighbor[4] = std::string("x"); neighbor[5] = neighbor[4];
neighbor[6] = std::string("z"); neighbor[7] = neighbor[6];
(*mHashTile2Neighbor)[std::string("w")] = new CTBNeighbor(neighbor);
neighbor[0] = std::string("u"); neighbor[1] = neighbor[0];
neighbor[2] = std::string("w"); neighbor[3] = neighbor[2];
neighbor[4] = std::string("y"); neighbor[5] = neighbor[4];
neighbor[6] = std::string("z"); neighbor[7] = neighbor[6];
(*mHashTile2Neighbor)[std::string("x")] = new CTBNeighbor(neighbor);
neighbor[0] = std::string("u"); neighbor[1] = neighbor[0];
neighbor[2] = std::string("x"); neighbor[3] = neighbor[2];
neighbor[4] = std::string("v"); neighbor[5] = neighbor[4];
neighbor[6] = std::string("z"); neighbor[7] = neighbor[6];
(*mHashTile2Neighbor)[std::string("y")] = new CTBNeighbor(neighbor);
neighbor[0] = std::string("w"); neighbor[1] = neighbor[0];
neighbor[2] = std::string("v"); neighbor[3] = neighbor[2];
neighbor[4] = std::string("x"); neighbor[5] = neighbor[4];
neighbor[6] = std::string("y"); neighbor[7] = neighbor[6];
(*mHashTile2Neighbor)[std::string("z")] = new CTBNeighbor(neighbor);
}
/**
@remarks
allocate shared memory
**/
void libRQTS::initSharedM()
{
mSharedIDLoad = shmget((key_t)1335, sizeof(struct SHARED_LOAD_RQ), 0666|IPC_CREAT);
mSharedMemLoad = shmat(mSharedIDLoad, (void*)0, 0);
mSharedSttLoad = (struct SHARED_LOAD_RQ*)mSharedMemLoad;
}
/**
@remarks
free shared memory
**/
void libRQTS::closeSharedM()
{
shmdt(mSharedMemLoad);
shmctl(mSharedIDLoad, IPC_RMID, 0);
}
/**
@remarks
send load requests to fore-end process
**/
void libRQTS::sendfile()
{
if(mSharedSttLoad->mWritable != 1)
{
for (std::vector<CTBLoadReq*>::iterator it = mLoadQueueReg.begin();
it != mLoadQueueReg.end(); it++)
{
mSharedSttLoad->mIsSplit[mSharedSttLoad->mLength] = (*it)->IsSplitReq();
strcpy(mSharedSttLoad->mFilename[mSharedSttLoad->mLength],
(*it)->GetReqName().c_str());
mSharedSttLoad->mLength++;
}
mLoadQueueReg.clear();
mSharedSttLoad->mWritable = 1;
}
}
/**
@remarks
receive load requests from back-end process
return true if success
**/
bool libRQTS::recvfile()
{
bool ret = false;
if(mSharedSttLoad->mWritable != 0)
{
if (mSharedSttLoad->mLength > 0)
{
for (unsigned int i = 0; i < mSharedSttLoad->mLength; i++)
{
CTBLoadReq *req = new CTBLoadReq(mSharedSttLoad->mIsSplit[i],
mSharedSttLoad->mFilename[i]);
mLoadQueueReg.push_back(req);
}
mSharedSttLoad->mLength = 0;
ret = true;
}
mSharedSttLoad->mWritable = 0;
}
if (mLoadQueue.empty())
{
mLoadQueue = mLoadQueueReg;
mLoadQueueReg.clear();
}
return ret;
}
/**
@remarks
check whether a tile could be split or not
@par
filename filename of a given tile
**/
bool libRQTS::checksplit(std::string filename)
{
bool can_split[4] = {false, false, false, false};
CTBNeighbor *neighbor = (*mHashTile2Neighbor)[filename];
/**
check the neighbors of the given tile
if a neighbor does not allow the given tile to be split (depending on the relationship of restricted quadtrees),
then recursively check the corresponding neighbor for splitting
**/
for (int i = 0; i < 4; i++)
{
if (neighbor->GetNeighbor(i * 2).length() == 0 || neighbor->GetNeighbor(i * 2).length() >= filename.length())
{
can_split[i] = true;
}
else
{
can_split[i] = checksplit(neighbor->GetNeighbor(i * 2));
}
}
/**
if the given tile could be split and it is not in load queue
then add it to the load queue
**/
if (can_split[0] && can_split[1] && can_split[2] && can_split[3])
{
bool found = false;
for (std::vector<CTBLoadReq*>::iterator it = mLoadQueue.begin();
it != mLoadQueue.end(); it++)
{
CTBLoadReq *req = *it;
if (req->GetReqName() == filename)
{
found = true;
break;
}
}
if (!found)
{
CTBLoadReq *req = new CTBLoadReq(true, filename);
mLoadQueue.push_back(req);
mLoadQueueReg.push_back(req);
}
}
return true;
}
/**
@remarks
check whether a group of tiles (represented by their parents) could be merged or not one by one
@par
filenames a group of parents of every four sibling tiles to be merged
**/
bool libRQTS::checkmerge(std::vector<std::string> filenames)
{
/**
iterate all tiles, check their mergability
**/
for (std::vector<std::string>::iterator it = filenames.begin();
it != filenames.end(); it++)
{
if (checkMerge(*it))
{
return true;
}
}
return false;
}
/**
@remarks
check whether four tiles could be merged or not
@par
filename filename of parent of given four tiles
**/
bool libRQTS::checkMerge(std::string filename)
{
std::string sub_name[4] = {"/q", "/r", "/s", "/t"};
unsigned int son_neighbor[4][4] = {0, 1, 2, 3,
0, 1, 4, 5,
2, 3, 6, 7,
4, 5, 6, 7};
/**
check the neighbors of the given tile
if it satisfies merging condition (depending on the relationship of restricted quadtrees),
then add it to the load queue
**/
for (int i = 0; i < 4; i++)
{
CTBNeighbor *neighbor = (*mHashTile2Neighbor)[filename + sub_name[i]];
if (neighbor == NULL)
{
int a = 0;
}
if (neighbor->GetNeighbor(son_neighbor[i][0]) != neighbor->GetNeighbor(son_neighbor[i][1]))
{
return false;
}
if (neighbor->GetNeighbor(son_neighbor[i][2]) != neighbor->GetNeighbor(son_neighbor[i][3]))
{
return false;
}
}
CTBLoadReq *req = new CTBLoadReq(false, filename);
mLoadQueue.push_back(req);
mLoadQueueReg.push_back(req);
return true;
}
/**
@remarks
actual operation of splitting a tile
@par
filename filename of a given tile
**/
void libRQTS::split(std::string filename)
{
std::string sub_name[4] = {"/q", "/r", "/s", "/t"};
unsigned int inter_map[4][8] = {0, 0, 0, 0, 2, 2, 3, 3,
0, 0, 1, 1, 0, 0, 4, 4,
1, 1, 0, 0, 4, 4, 0, 0,
2, 2, 3, 3, 0, 0, 0, 0};
unsigned int son_of_neighbor2[8] = {0, 1, 0, 2, 1, 3, 2, 3};
for (int i = 0; i < 4; i++)
{
(*mHashTile2Neighbor)[filename + sub_name[i]] = new CTBNeighbor();
}
/**
establish neighbor relationship between sibling children of given tile
**/
for (int i = 0; i < 4; i++)
{
CTBNeighbor *neighbor = (*mHashTile2Neighbor)[filename + sub_name[i]];
for (int j = 0; j < 8; j++)
{
if (inter_map[i][j])
{
neighbor->SetNeighbor(j, filename + sub_name[inter_map[i][j] - 1]);
}
}
}
/**
adjust neighbor relationship of neighbors of given tile
establish neighbor relationship of children of given tile
**/
CTBNeighbor *neighbor_of_father = (*mHashTile2Neighbor)[filename];
for (int i = 0; i < 8; i = i + 2)
{
if (neighbor_of_father->GetNeighbor(i) != ""|| neighbor_of_father->GetNeighbor(i + 1) != "")
{
if (neighbor_of_father->GetNeighbor(i) == neighbor_of_father->GetNeighbor(i + 1))
{
CTBNeighbor *neighbor_of_neighbor = (*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i)];
for (int j = 0; j < 8; j = j + 2)
{
if (neighbor_of_neighbor->GetNeighbor(j) == filename)
{
(*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i)]->SetNeighbor(j,
filename + sub_name[son_of_neighbor2[i]]);
(*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i)]->SetNeighbor(j + 1,
filename + sub_name[son_of_neighbor2[i + 1]]);
break;
}
}
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i]]]->SetNeighbor(i,
neighbor_of_father->GetNeighbor(i));
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i]]]->SetNeighbor(i + 1,
neighbor_of_father->GetNeighbor(i + 1));
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i + 1]]]->SetNeighbor(i,
neighbor_of_father->GetNeighbor(i));
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i + 1]]]->SetNeighbor(i + 1,
neighbor_of_father->GetNeighbor(i + 1));
}
else
{
CTBNeighbor *neighbor_of_neighbor;
if (neighbor_of_father->GetNeighbor(i) != "")
{
neighbor_of_neighbor = (*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i)];
}
else if (neighbor_of_father->GetNeighbor(i + 1) != "")
{
neighbor_of_neighbor = (*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i + 1)];
}
for (int j = 0; j < 8; j = j + 2)
{
if (neighbor_of_neighbor->GetNeighbor(j) == filename)
{
if (neighbor_of_father->GetNeighbor(i) != "")
{
(*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i)]->SetNeighbor(j,
filename + sub_name[son_of_neighbor2[i]]);
(*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i)]->SetNeighbor(j + 1,
filename + sub_name[son_of_neighbor2[i]]);
}
if (neighbor_of_father->GetNeighbor(i + 1) != "")
{
(*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i + 1)]->SetNeighbor(j,
filename + sub_name[son_of_neighbor2[i + 1]]);
(*mHashTile2Neighbor)[neighbor_of_father->GetNeighbor(i + 1)]->SetNeighbor(j + 1,
filename + sub_name[son_of_neighbor2[i + 1]]);
}
break;
}
}
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i]]]->SetNeighbor(i,
neighbor_of_father->GetNeighbor(i));
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i]]]->SetNeighbor(i + 1,
neighbor_of_father->GetNeighbor(i));
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i + 1]]]->SetNeighbor(i,
neighbor_of_father->GetNeighbor(i + 1));
(*mHashTile2Neighbor)[filename + sub_name[son_of_neighbor2[i + 1]]]->SetNeighbor(i + 1,
neighbor_of_father->GetNeighbor(i + 1));
}
}
}
/**
erase neighbor relationship of given tile
**/
std::tr1::unordered_map<std::string, CTBNeighbor*>::iterator it = (*mHashTile2Neighbor).find(filename);
(*mHashTile2Neighbor).erase(it);
}
/**
@remarks
actual operation of merging four tiles
@par
filename filename of the parent of four tiles
**/
void libRQTS::merge(std::string filename)
{
std::string sub_name[4] = {"/q", "/r", "/s", "/t"};
(*mHashTile2Neighbor)[filename] = new CTBNeighbor();
unsigned int son_to_neighbor[8][2] = {0, 0,
1, 0,
0, 2,
2, 2,
1, 4,
3, 4,
2, 6,
3, 6};
/**
establish neighbor relationship of parent tile
**/
CTBNeighbor *neighbor = (*mHashTile2Neighbor)[filename];
for (int i = 0; i < 8; i++)
{
CTBNeighbor *neighbor_of_son = (*mHashTile2Neighbor)[filename + sub_name[son_to_neighbor[i][0]]];
neighbor->SetNeighbor(i, neighbor_of_son->GetNeighbor(son_to_neighbor[i][1]));
}
/**
adjust neighbor relationship of neighbors of given four tiles
**/
for (int i = 0; i < 8; i = i + 2)
{
unsigned int s1 = son_to_neighbor[i][0];
unsigned int p1 = son_to_neighbor[i][1];
unsigned int s2 = son_to_neighbor[i + 1][0];
unsigned int p2 = son_to_neighbor[i + 1][1];
CTBNeighbor *neighbor_of_son1 = (*mHashTile2Neighbor)[filename + sub_name[s1]];
CTBNeighbor *neighbor_of_son2 = (*mHashTile2Neighbor)[filename + sub_name[s2]];
CTBNeighbor *neighbor_of_neighbor;
if (neighbor_of_son1->GetNeighbor(p1) != "")
{
neighbor_of_neighbor = (*mHashTile2Neighbor)[neighbor_of_son1->GetNeighbor(p1)];
for (int j = 0; j < 8; j++)
{
if (neighbor_of_neighbor->GetNeighbor(j) == filename + sub_name[s1])
{
neighbor_of_neighbor->SetNeighbor(j, filename);
}
}
}
if (neighbor_of_son2->GetNeighbor(p2) != "")
{
neighbor_of_neighbor = (*mHashTile2Neighbor)[neighbor_of_son2->GetNeighbor(p2)];
for (int j = 0; j < 8; j++)
{
if (neighbor_of_neighbor->GetNeighbor(j) == filename + sub_name[s2])
{
neighbor_of_neighbor->SetNeighbor(j, filename);
}
}
}
}
/**
erase neighbor relationship of given four tiles
**/
for (int i = 0; i < 4; i++)
{
std::tr1::unordered_map<std::string, CTBNeighbor*>::iterator it = (*mHashTile2Neighbor).find(filename + sub_name[i]);
(*mHashTile2Neighbor).erase(it);
}
}
/**
@remarks
check load queue, and return load request from load queue for merging request
for splitting request, increase its counter and return one of its child according to its counter
**/
std::string libRQTS::checkfile()
{
std::string name;
if (!mLoadQueue.empty())
{
CTBLoadReq *req = mLoadQueue[0];
if (req->IsSplitReq())
{
std::string reqName = req->GetReqName();
switch (req->GetReqCount())
{
case 0:
name = reqName + "/q";
break;
case 1:
name = reqName + "/r";
break;
case 2:
name = reqName + "/s";
break;
case 3:
name = reqName + "/t";
break;
}
req->IncReqCount();
}
else
{
std::string reqName = req->GetReqName();
if (req->GetReqCount() == 0)
{
name = reqName;
req->IncReqCount();
}
}
}
return name;
}
/**
@remarks
erase and return load request from load queue for merging request
for splitting request, increase its finished counter until four,
and then erase and return load request from load queue
**/
std::string libRQTS::getfile()
{
std::string name;
CTBLoadReq *req = mLoadQueue[0];
if (req->IsSplitReq())
{
req->IncReqOverCount();
}
else
{
name = "m";
mLoadQueue.erase(mLoadQueue.begin());
}
if (req->GetReqOverCount() == 4)
{
name = req->GetReqName();
mLoadQueue.erase(mLoadQueue.begin());
}
return name;
}
/**
@remarks
return whether load queue is empty, true if empty
**/
bool libRQTS::isdone()
{
return mLoadQueue.empty();
}
/**
@remarks
check neighbors of given tile for given side
@par
filename filename of a given tile
side indicates the give side (from 0 to 7)
*mask indicates the level of detail of four side of the corresponding neighbor (depends on side) for given tile
**/
std::string libRQTS::checkbound(std::string filename, unsigned char side, unsigned char* mask)
{
CTBNeighbor *neighbor = (*mHashTile2Neighbor)[filename];
std::string neighborname = neighbor->GetNeighbor(side * 2);
*mask = 0;
if (neighborname.length() == filename.length())
{
CTBNeighbor *neighborofneighbor = (*mHashTile2Neighbor)[neighborname];
for (unsigned int i = 0; i < 4; i++)
{
if (neighborofneighbor->GetNeighbor(i * 2).length() > neighborname.length() ||
neighborofneighbor->GetNeighbor(i * 2) == filename)
{
*mask |= (1 << i);
}
}
}
return neighborname;
}
/**
@remarks
replace the trailing letter of a tile's filename (from q,r,s,t to w,x,y,z, respectively)
@par
name filename of a given tile
**/
std::string replacefile(std::string name)
{
if (name.length() < 2)
{
return name;
}
char endname = *(name.end() - 1) - 'q' + 'w';
name.erase(name.end() - 1);
return name + endname;
}