-
Notifications
You must be signed in to change notification settings - Fork 12
/
osmaputils.pas
726 lines (609 loc) · 17.2 KB
/
osmaputils.pas
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
(*
OsMap components for offline rendering and routing functionalities
based on OpenStreetMap data
Copyright (C) 2019 Sergey Bodrov
This source is ported from libosmscout library
Copyright (C) 2009 Tim Teulings
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
(*
util\Number (...)
Breaker:
Breaker -> TBreakerObject
util/Cache:
Cache
*)
unit OsMapUtils;
{$ifdef FPC}
{$mode objfpc}{$H+}
{$else}
{$ZEROBASEDSTRINGS OFF}
{$endif}
interface
uses
Classes, SysUtils;
type
{ TBreakerObject }
TBreakerObject = object
IsAborted: Boolean;
procedure Init();
procedure Break();
end;
{ TStopClock }
TStopClock = object
StartTime: TDateTime;
StopTime: TDateTime;
procedure Init();
procedure Stop();
function IsSignificant(): Boolean;
function ResultString(): string;
function GetMilliseconds(): Int64;
end;
//PPHashItem = ^PHashItem;
PHashItem = ^THashItem;
THashItem = record
Next: PHashItem;
Key: string;
Value: Integer;
end;
{ TSimpleStringHash }
{ Simple string-to-integer hashtable }
TSimpleStringHash = object
private
Buckets: array of PHashItem;
protected
function Find(const Key: string): PHashItem;
function HashOf(const Key: string): Cardinal;
public
procedure Init(Size: Cardinal = 256);
procedure Add(const Key: string; Value: Integer);
procedure Clear;
function Modify(const Key: string; Value: Integer): Boolean;
{ return -1 if no value for key, suitable for list index }
function ValueOf(const Key: string): Integer;
function FindValue(const Key: string; out Value: Integer): Boolean;
end;
TCompareFunc = function (const elem1, elem2): Integer;
PSimpleCacheItem = ^TSimpleCacheItem;
TSimpleCacheItem = record
Next: Integer;
Prev: Integer;
Key: string;
//Value: Integer;
end;
{ Simple string-to-index cache with FIFO semantics and limited count }
{ TSimpleCache }
TSimpleCache = object
private
Hash: TSimpleStringHash;
Items: array of TSimpleCacheItem;
Head: Integer;
Tail: Integer;
Count: Integer;
{ Move item to head of queue }
procedure Promote(AIndex: Integer);
public
procedure Init(AMaxSize: Integer);
{ Return True if item AKey is found, AIndex is set to found item's index }
function GetEntry(const AKey: string; out AIndex: Integer): Boolean;
{ Put item AKey to cache, AIndex is set to found/added item index.
If cache grows to MaxSize, added item replaces less used item }
procedure SetEntry(const AKey: string; out AIndex: Integer);
function GetMaxSize(): Integer;
function GetCount(): Integer;
procedure Clear();
end;
{ Sort any array }
procedure AnySort(const Arr; Count: Integer; ItemSize: Integer; CompareFunc: TCompareFunc);
{ Encode a signed number into the given buffer using some variable length encoding.
The methods returns the number of bytes written.
The first bit (if set) signals a negative numer. The highest bit in a byte
is set, if there is an additional byte following. So we use one bit
for signaling signess and use 7 of 8 bytes per byte for data.
The current implementation thus requires the buffer to have at least space
for sizeof(N)*8/7 + 1/8 bytes:
This are 5 bytes for a 32bit value and 10 bytes for a 64bit value. }
function EncodeNumberSigned(ANum: Int64; var ABuffer): Integer;
{ Encode an unsigned number into the given buffer using some variable length encoding.
The highest bit in a byte is set, if there is an additional byte following.
So we use 7 of 8 bytes per byte for data.
The methods returns the number of bytes written.
The current implementation requires the buffer to have at least space
for sizeof(N)*8/7 bytes:
This are 5 bytes for a 32bit value and 10 bytes for a 64bit value. }
function EncodeNumberUnsigned(ANum: UInt64; var ABuffer): Integer;
{ Encode a number into the given buffer using some variable length encoding.
The methods returns the number of bytes written.
The current implementation requires the buffer to have at least space
for sizeof(N)*8/7 bytes for an unsigned number
and sizeof(N)*8/7 + 1/8 bytes for a signed number
This are 5 bytes for a 32bit value and 10 bytes for a64bit value. }
function EncodeNumber(ANum: Int64; var ABuffer): Integer; overload;
function EncodeNumber(ANum: UInt64; var ABuffer): Integer; overload;
{ Decode a signed variable length encoded number from the buffer back to
the variable.
The methods returns the number of bytes read. }
function DecodeNumberSigned(const ABuffer; out ANum: Int64): Integer;
{ Decode a unsigned variable length encoded number from the buffer back to
the variable.
The methods returns the number of bytes read. }
function DecodeNumberUnsigned(const ABuffer; out ANum: UInt64): Integer;
{ Decode a variable length encoded number from the buffer back to
the variable.
The methods returns the number of bytes read. }
function DecodeNumber(const ABuffer; out ANum: Int64): Integer; overload;
function DecodeNumber(const ABuffer; out ANum: UInt64): Integer; overload;
{ Returns the number of bytes needed to encode the number. The function calculates
the number of bytes that contain information, dropping leading bytes that only
contain zero. }
function BytesNeededToEncodeNumber(ANum: Int64): Byte;
{ Returns the number of bytes needed to encode the number. The function calculates
the number of bytes that contain information, dropping leading bytes that only
contain zero. }
function BitsNeededToEncodeNumber(ANum: Int64): Byte;
{ Encodes the given numbers into a new number of twice the size that has
the bits of each nunber alternating interleaved.
This can be used to convert two dimensional coordinates into
one number, where coordinates close in 2D are close in the
one dimensional projection, too. }
function InterleaveNumbers(A, B: LongWord): UInt64;
{ Returns the number of bytes needed to encode the given number of bits. }
function BitsToBytes(ABits: Integer): Integer;
{:Fetch string from left of Value string.}
function Fetch(var Value: string; const Delimiter: string): string;
{ Return True if bit with BitIndex is set in Value }
function GetBit(const Value; BitIndex: Integer): Boolean;
{ Set bit with BitIndex (0..BitCount) in Value }
procedure SetBit(var Value; BitIndex: Integer; BitValue: Boolean);
procedure AppendToArray(var AArray; const AValue);
var
LogFile: TextFile;
implementation
uses Math; // eliminate "end of source not found"
function EncodeNumberSigned(ANum: Int64; var ABuffer): Integer;
var
pBuf: PByte;
begin
pBuf := @ABuffer;
if ANum < 0 then
pBuf^ := 1
else
pBuf^ := 0;
pBuf^ := pBuf^ or Byte((ANum and $3F) shl 1);
ANum := ANum shr 6;
Result := 1;
while ANum > 0 do
begin
pBuf^ := pBuf^ or $80;
Inc(pBuf);
pBuf^ := Byte(ANum and $7F);
ANum := ANum shr 7;
Inc(Result);
end;
end;
function EncodeNumberUnsigned(ANum: UInt64; var ABuffer): Integer;
var
pBuf: PByte;
begin
pBuf := @ABuffer;
Result := 0;
while ANum > $7F do
begin
pBuf^ := Byte(ANum and $7F) or $80;
Inc(pBuf);
ANum := ANum shr 7;
Inc(Result);
end;
pBuf^ := Byte(ANum and $7F);
Inc(Result);
end;
function EncodeNumber(ANum: Int64; var ABuffer): Integer;
begin
Result := EncodeNumberSigned(ANum, ABuffer);
end;
function EncodeNumber(ANum: UInt64; var ABuffer): Integer;
begin
Result := EncodeNumberUnsigned(ANum, ABuffer);
end;
function DecodeNumberSigned(const ABuffer; out ANum: Int64): Integer;
var
pBuf: PByte;
Val: Int64;
Shift, NextShift: Integer;
begin
pBuf := @ABuffer;
Shift := 0;
Result := 1;
if (pBuf^ and $01) <> 0 then
ANum := -1
else
ANum := 0;
Val := (pBuf^ and $7E) shr 1;
NextShift := 6;
while (pBuf^ and $80) <> 0 do
begin
ANum := ANum or (Val shl Shift);
Inc(pBuf);
Val := (pBuf^ and $7F);
Shift := NextShift;
Inc(NextShift, 7);
Inc(Result);
end;
ANum := ANum or (Val shl Shift);
end;
function DecodeNumberUnsigned(const ABuffer; out ANum: UInt64): Integer;
var
pBuf: PByte;
Shift: Integer;
begin
pBuf := @ABuffer;
Shift := 0;
Result := 1;
ANum := 0;
while True and (Shift < 48) do
begin
ANum := ANum or (UInt64(pBuf^ and $7F) shl Shift);
if (pBuf^ and $80) = 0 then
Break;
Inc(pBuf);
Inc(Shift, 7);
Inc(Result);
end;
end;
function DecodeNumber(const ABuffer; out ANum: Int64): Integer;
begin
Result := DecodeNumberSigned(ABuffer, ANum);
end;
function DecodeNumber(const ABuffer; out ANum: UInt64): Integer;
begin
Result := DecodeNumberUnsigned(ABuffer, ANum);
end;
function BytesNeededToEncodeNumber(ANum: Int64): Byte;
begin
Result := 0;
while (ANum <> 0) do
begin
ANum := ANum div 256;
Inc(Result);
end;
if Result = 0 then
Result := 1;
end;
function BitsNeededToEncodeNumber(ANum: Int64): Byte;
begin
Result := 0;
while (ANum <> 0) do
begin
ANum := ANum div 2;
Inc(Result);
end;
if Result = 0 then
Result := 1;
end;
function InterleaveNumbers(A, B: LongWord): UInt64;
var
i, nBit: Integer;
begin
Result := 0;
for i := 0 to 31 do
begin
nBit := 31-i;
Result := Result shl 1;
Result := Result + ((A shr nBit) and $01);
Result := Result shl 1;
Result := Result + ((B shr nBit) and $01);
end;
end;
function BitsToBytes(ABits: Integer): Integer;
begin
if (ABits mod 8) = 0 then
Result := (ABits div 8)
else
Result := (ABits div 8) + 1;
end;
function Fetch(var Value: string; const Delimiter: string): string;
var
n: Integer;
begin
n := Pos(Delimiter, Value);
if n = 0 then n := MaxInt-1;
Result := Copy(Value, 1, n-1);
Value := Copy(Value, n+1, MaxInt);
end;
function GetBit(const Value; BitIndex: Integer): Boolean;
var
pb: PByte;
begin
pb := @Value;
Inc(pb, BitIndex div 8); // point to n-th byte of Value
Result := ((pb^ shr (BitIndex mod 8)) and 1) = 1;
end;
procedure SetBit(var Value; BitIndex: Integer; BitValue: Boolean);
var
pb: PByte;
BitOffset: Integer;
begin
pb := @Value;
BitOffset := BitIndex mod 8;
Inc(pb, BitIndex div 8); // point to n-th byte of Value
if BitValue then
pb^ := pb^ or (1 shl BitOffset)
else
pb^ := pb^ and (not (1 shl BitOffset));
end;
procedure AppendToArray(var AArray; const AValue);
begin
end;
{ TBreakerObject }
procedure TBreakerObject.Init();
begin
IsAborted := False;
end;
procedure TBreakerObject.Break();
begin
IsAborted := True;
end;
{ TStopClock }
procedure TStopClock.Init();
begin
StartTime := Now();
StopTime := 0;
end;
procedure TStopClock.Stop();
begin
StopTime := Now();
end;
function TStopClock.IsSignificant(): Boolean;
begin
Result := (StopTime > StartTime);
end;
function TStopClock.ResultString(): string;
begin
Result := FormatDateTime('S.Z', (StopTime - StartTime));
end;
function TStopClock.GetMilliseconds(): Int64;
var
diff: TDateTime;
begin
diff := StopTime - StartTime;
if (StopTime > 0) and (StartTime < 0) then
diff := diff - 0.5
else if (StopTime < -1.0) and (StartTime > -1.0) then
diff := diff + 0.5;
Result := Trunc((Abs(diff) + ((TDateTime(1) / MSecsPerDay) / 2)) * MSecsPerDay);
end;
{ TSimpleStringHash }
function TSimpleStringHash.Find(const Key: string): PHashItem;
var
Hash: Cardinal;
begin
Assert(Length(Buckets) > 0);
Hash := HashOf(Key) mod Cardinal(Length(Buckets));
Result := Buckets[Hash];
while Result <> nil do
begin
if Result^.Key = Key then
Exit
else
begin
Result := Result^.Next;
end;
end;
end;
function TSimpleStringHash.HashOf(const Key: string): Cardinal;
var
I: Integer;
begin
Result := 0;
for I := 1 to Length(Key) do
Result := ((Result shl 2) or (Result shr (SizeOf(Result) * 8 - 2))) xor Ord(Key[I]);
end;
procedure TSimpleStringHash.Init(Size: Cardinal);
begin
SetLength(Buckets, Size);
end;
procedure TSimpleStringHash.Add(const Key: string; Value: Integer);
var
Hash: Cardinal;
Bucket: PHashItem;
begin
Assert(Length(Buckets) > 0);
Hash := HashOf(Key) mod Cardinal(Length(Buckets));
New(Bucket);
Bucket^.Key := Key;
Bucket^.Value := Value;
Bucket^.Next := Buckets[Hash];
Buckets[Hash] := Bucket;
end;
procedure TSimpleStringHash.Clear;
var
I: Integer;
P, N: PHashItem;
begin
for I := 0 to Length(Buckets) - 1 do
begin
P := Buckets[I];
while P <> nil do
begin
N := P^.Next;
Dispose(P);
P := N;
end;
Buckets[I] := nil;
end;
end;
function TSimpleStringHash.Modify(const Key: string; Value: Integer): Boolean;
var
P: PHashItem;
begin
P := Find(Key);
if P <> nil then
begin
Result := True;
P^.Value := Value;
end
else
Result := False;
end;
function TSimpleStringHash.ValueOf(const Key: string): Integer;
var
P: PHashItem;
begin
P := Find(Key);
if P <> nil then
Result := P^.Value
else
Result := -1;
end;
function TSimpleStringHash.FindValue(const Key: string; out Value: Integer): Boolean;
var
P: PHashItem;
begin
P := Find(Key);
Result := (P <> nil);
if Result then
Value := P^.Value;
end;
procedure AnyQuickSort(const Arr; idxL, idxH: Integer;
ItemSize: Integer; CompareFunc: TCompareFunc; var SwapBuf);
var
ls, hs: Integer;
li, hi: Integer;
mi : Integer;
ms : Integer;
pb : PByteArray;
begin
pb := @Arr;
li := idxL;
hi := idxH;
mi := (li+hi) div 2;
ls := li * ItemSize;
hs := hi * ItemSize;
ms := mi * ItemSize;
repeat
while CompareFunc( pb[ls], pb[ms] ) < 0 do
begin
Inc(ls, ItemSize);
Inc(li);
end;
while CompareFunc( pb[ms], pb[hs] ) < 0 do
begin
Dec(hs, ItemSize);
Dec(hi);
end;
if ls <= hs then
begin
Move(pb[ls], SwapBuf, ItemSize);
Move(pb[hs], pb[ls], ItemSize);
Move(SwapBuf, pb[hs], ItemSize);
Inc(ls, ItemSize); Inc(li);
Dec(hs, ItemSize); Dec(hi);
end;
until ls > hs;
if hi > idxL then AnyQuickSort(Arr, idxL, hi, ItemSize, CompareFunc, SwapBuf);
if li < idxH then AnyQuickSort(Arr, li, idxH, ItemSize, CompareFunc, SwapBuf);
end;
procedure AnySort(const Arr; Count: Integer; ItemSize: Integer; CompareFunc: TCompareFunc);
var
buf: array of byte;
begin
SetLength(buf, ItemSize);
AnyQuickSort(Arr, 0, Count-1, ItemSize, compareFunc, buf[0]);
end;
{ TSimpleCache }
procedure TSimpleCache.Promote(AIndex: Integer);
var
iNext, iPrev: Integer;
begin
Assert(AIndex < Length(Items));
Assert(Items[AIndex].Key <> '');
iNext := Items[AIndex].Next;
iPrev := Items[AIndex].Prev;
if iPrev <> -1 then
begin
// link prev and next items
Items[iPrev].Next := iNext;
if iNext <> -1 then
begin
Items[iNext].Prev := iPrev;
end;
if (Tail = AIndex) then
Tail := iPrev;
// update old head item
if Head <> -1 then
begin
Items[Head].Prev := AIndex;
Items[AIndex].Next := Head;
end;
// set new head item
Head := AIndex;
Items[Head].Prev := -1;
end;
end;
procedure TSimpleCache.Init(AMaxSize: Integer);
begin
Assert(AMaxSize > GetMaxSize());
SetLength(Items, AMaxSize);
Hash.Init(AMaxSize);
Clear();
end;
function TSimpleCache.GetEntry(const AKey: string; out AIndex: Integer): Boolean;
begin
Result := Hash.FindValue(AKey, AIndex);
if Result then
Promote(AIndex);
end;
procedure TSimpleCache.SetEntry(const AKey: string; out AIndex: Integer);
begin
if not Hash.FindValue(AKey, AIndex) then
begin
AIndex := -1;
if Count < Length(Items) then
begin
// find empty item
AIndex := Count;
Inc(Count);
if (Tail = -1) then
Tail := AIndex;
if (Head = -1) then
Head := AIndex;
end
else
AIndex := Tail;
Assert(AIndex <> -1);
Items[AIndex].Key := AKey;
end;
Promote(AIndex);
end;
function TSimpleCache.GetMaxSize(): Integer;
begin
Result := Length(Items);
end;
function TSimpleCache.GetCount(): Integer;
begin
Result := Count;
end;
procedure TSimpleCache.Clear();
var
i: Integer;
begin
for i := Low(Items) to High(Items) do
begin
Items[i].Next := -1;
Items[i].Prev := -1;
Items[i].Key := '';
end;
Hash.Clear();
Count := 0;
end;
end.