Skip to content

Commit a7dfa83

Browse files
committed
Initial commit
0 parents  commit a7dfa83

11 files changed

Lines changed: 1271 additions & 0 deletions

File tree

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.dub
2+
docs.json
3+
__dummy.html
4+
*.o
5+
*.obj
6+
*.a
7+
bin
8+
/d-open-location-code
9+
dub.selections.json

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# DOpenLocationCode
2+
3+
Port of the [open-location-code](https://github.com/google/open-location-code) library (also known as [plus codes](https://plus.codes)) for identifying geo areas.
4+
5+
This ports commit (a6eb95b)[https://github.com/google/open-location-code/commit/a6eb95b4d2f934e94dae4f092260caf7f3db7967]
6+
7+
## Example
8+
9+
normal usage:
10+
11+
```d
12+
import openlocationcode;
13+
14+
// get geo coordinates from plus code
15+
OpenLocationCode plusCode = OpenLocationCode.fromString("8FVC2222+22GCCCC");
16+
writeln(plusCode.decode());
17+
// ->
18+
19+
// get plus code from geo coordinates
20+
OpenLocationCode generatedPlusCode = OpenLocationCode.encode(lat, lon);
21+
writeln(generatedPlusCode.code);
22+
```
23+
24+
---
25+
26+
`nothrow @nogc` usage:
27+
28+
```d
29+
import openlocationcode;
30+
31+
// get geo coordinates from plus code
32+
string input = "8FVC2222+22GCCCC";
33+
if (!input.isValidCode)
34+
return error;
35+
OpenLocationCode plusCode = OpenLocationCode.fromTrustedString(input);
36+
37+
if (!plusCode.isFull)
38+
return error;
39+
OpenLocationCodeArea area = plusCode.decodeTrustedFull();
40+
printf("area around %d, %d", area.centerLatitude, area.centerLongitude);
41+
42+
// get plus code from geo coordinates
43+
ubyte[maxOLCLength] buffer;
44+
scope ubyte[] generatedPlusCodeString = OpenLocationCode.encode(buffer, lat, lon);
45+
printf("%s", cast(char[])generatedPlusCodeString);
46+
47+
```

benchmark/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/benchmark

benchmark/dub.sdl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name "benchmark"
2+
dependency "d-open-location-code" path=".."

benchmark/source/app.d

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import openlocationcode;
2+
3+
import core.time : MonoTime;
4+
import std.random;
5+
import std.stdio;
6+
7+
void main()
8+
{
9+
enum loops = 1000000;
10+
11+
struct TestData
12+
{
13+
double lat, lon;
14+
int length;
15+
string code;
16+
17+
static TestData generate()
18+
{
19+
TestData ret;
20+
ret.lat = uniform01!double * 180 - 90;
21+
ret.lon = uniform01!double * 360 - 180;
22+
ret.length = uniform(4, 15);
23+
if (ret.length < 10 && ret.length % 2 == 1)
24+
ret.length++;
25+
ret.code = OpenLocationCode.encode(ret.lat, ret.lon, ret.length).code;
26+
return ret;
27+
}
28+
}
29+
30+
TestData[] list;
31+
list.length = loops;
32+
foreach (i; 0 .. loops)
33+
list[i] = TestData.generate();
34+
35+
writeln("Running encode benchmark");
36+
ubyte[maxOLCLength] buffer = void;
37+
// benchmark encode
38+
auto start = MonoTime.currTime();
39+
foreach (data; list)
40+
cast(void) OpenLocationCode.encode(buffer[], data.lat, data.lon, data.length);
41+
auto end = MonoTime.currTime();
42+
auto dur = end - start;
43+
writefln("Encode %s loops in %s;\n\t%s per call", loops, dur, dur / loops);
44+
45+
writeln("Running decode benchmark");
46+
// benchmark decode
47+
start = MonoTime.currTime();
48+
foreach (data; list)
49+
cast(void) OpenLocationCode.fromTrustedString(data.code).decodeTrustedFull();
50+
end = MonoTime.currTime();
51+
dur = end - start;
52+
writefln("Decode %s loops in %s;\n\t%s per call", loops, dur, dur / loops);
53+
}

dub.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "d-open-location-code",
3+
"license": "Apache-2.0",
4+
"targetType": "library",
5+
"configurations": [
6+
{
7+
"name": "library"
8+
},
9+
{
10+
"name": "unittest",
11+
"targetType": "executable",
12+
"preBuildCommands": [
13+
"$DUB run --compiler=$$DC unit-threaded -c gen_ut_main -- -f bin/ut.d"
14+
],
15+
"mainSourceFile": "bin/ut.d",
16+
"dependencies": {
17+
"unit-threaded": "*"
18+
}
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)