Artifact e047a4cc81258d2d41e92382a080188cfcad45b32653a5f8074110719b1e9bcf:
- File
polyline.erl
— part of check-in
[301914a6a1]
at
2014-12-05 20:43:53
on branch origin/master
— Housekeeping: Formatting, variable naming consistency, etc
- Get rid of the mixed up mess of CamelCase, underscores, prefixes,
suffixes, abbreviations, full names, etc.
- Make indenting of lists:map functions consistent
- Remove spurious whitespace (user: base@atomicules.co.uk size: 6134)
0000: 2d 6d 6f 64 75 6c 65 28 70 6f 6c 79 6c 69 6e 65 -module(polyline
0010: 29 2e 0a 2d 65 78 70 6f 72 74 28 5b 64 65 63 6f )..-export([deco
0020: 64 65 2f 31 2c 20 65 69 67 68 74 5f 62 69 74 5f de/1, eight_bit_
0030: 63 68 75 6e 6b 73 2f 31 2c 20 73 69 78 5f 62 69 chunks/1, six_bi
0040: 74 5f 63 68 75 6e 6b 73 2f 31 2c 20 73 69 78 5f t_chunks/1, six_
0050: 62 69 74 5f 63 68 75 6e 6b 2f 31 2c 20 73 70 6c bit_chunk/1, spl
0060: 69 74 5f 75 70 5f 73 69 78 5f 62 69 74 73 2f 31 it_up_six_bits/1
0070: 2c 20 66 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b , five_bit_chunk
0080: 73 2f 31 2c 20 62 69 6e 5f 66 6c 69 70 2f 31 5d s/1, bin_flip/1]
0090: 29 2e 0a 0a 25 53 65 65 20 68 74 74 70 73 3a 2f )...%See https:/
00a0: 2f 64 65 76 65 6c 6f 70 65 72 73 2e 67 6f 6f 67 /developers.goog
00b0: 6c 65 2e 63 6f 6d 2f 6d 61 70 73 2f 64 6f 63 75 le.com/maps/docu
00c0: 6d 65 6e 74 61 74 69 6f 6e 2f 75 74 69 6c 69 74 mentation/utilit
00d0: 69 65 73 2f 70 6f 6c 79 6c 69 6e 65 61 6c 67 6f ies/polylinealgo
00e0: 72 69 74 68 6d 0a 0a 0a 64 65 63 6f 64 65 28 45 rithm...decode(E
00f0: 6e 63 6f 64 65 64 5f 70 6f 6c 79 6c 69 6e 65 29 ncoded_polyline)
0100: 20 2d 3e 0a 09 25 53 74 65 70 73 20 31 31 20 62 ->..%Steps 11 b
0110: 61 63 6b 20 74 6f 20 38 0a 09 53 69 78 5f 62 69 ack to 8..Six_bi
0120: 74 5f 63 68 75 6e 6b 73 20 3d 20 73 69 78 5f 62 t_chunks = six_b
0130: 69 74 5f 63 68 75 6e 6b 73 28 45 6e 63 6f 64 65 it_chunks(Encode
0140: 64 5f 70 6f 6c 79 6c 69 6e 65 29 2c 0a 09 25 53 d_polyline),..%S
0150: 74 65 70 20 38 0a 09 47 72 6f 75 70 73 5f 6f 66 tep 8..Groups_of
0160: 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 20 3d 20 73 _chunks_list = s
0170: 70 6c 69 74 5f 75 70 5f 73 69 78 5f 62 69 74 73 plit_up_six_bits
0180: 28 53 69 78 5f 62 69 74 5f 63 68 75 6e 6b 73 29 (Six_bit_chunks)
0190: 2c 0a 09 25 53 74 65 70 20 38 20 62 61 63 6b 20 ,..%Step 8 back
01a0: 74 6f 20 36 0a 09 46 69 76 65 5f 62 69 74 5f 63 to 6..Five_bit_c
01b0: 68 75 6e 6b 73 20 3d 20 66 69 76 65 5f 62 69 74 hunks = five_bit
01c0: 5f 63 68 75 6e 6b 73 28 47 72 6f 75 70 73 5f 6f _chunks(Groups_o
01d0: 66 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 29 2c 0a f_chunks_list),.
01e0: 09 25 2d 2d 2d 54 4f 44 4f 0a 09 25 4d 61 79 62 .%---TODO..%Mayb
01f0: 65 20 73 6f 6d 65 20 6d 6f 72 65 20 6f 66 20 74 e some more of t
0200: 68 65 20 62 65 6c 6f 77 20 6e 65 65 64 20 73 70 he below need sp
0210: 6c 69 74 74 69 6e 67 20 6f 75 74 20 69 6e 74 6f litting out into
0220: 20 64 69 66 66 65 72 65 6e 74 20 66 75 6e 63 74 different funct
0230: 69 6f 6e 73 20 6f 72 20 6e 65 73 74 69 6e 67 20 ions or nesting
0240: 69 6e 20 61 20 6d 61 70 3f 0a 09 25 57 68 69 63 in a map?..%Whic
0250: 68 20 6f 70 74 69 6f 6e 20 74 6f 20 67 6f 20 66 h option to go f
0260: 6f 72 2c 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 or, a function t
0270: 68 61 74 20 6d 61 70 73 20 61 73 20 70 65 72 20 hat maps as per
0280: 66 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b 73 2c five_bit_chunks,
0290: 20 6f 72 20 6d 61 70 70 69 6e 67 20 66 75 6e 63 or mapping func
02a0: 74 69 6f 6e 73 20 61 73 20 70 65 72 20 62 65 6c tions as per bel
02b0: 6f 77 3f 0a 09 25 49 20 64 6f 6e 27 74 20 74 68 ow?..%I don't th
02c0: 69 6e 6b 20 49 20 63 61 6e 20 6d 61 70 20 61 6c ink I can map al
02d0: 6c 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 6f l functions in o
02e0: 6e 65 20 67 6f 20 62 65 63 61 75 73 65 20 75 6c ne go because ul
02f0: 74 69 6d 61 74 65 6c 79 20 6e 65 65 64 20 74 6f timately need to
0300: 20 63 68 61 6e 67 65 20 6e 75 6d 62 65 72 20 6f change number o
0310: 66 20 6d 65 6d 62 65 72 73 20 69 6e 20 67 72 6f f members in gro
0320: 75 70 73 2e 0a 09 25 49 2e 65 2e 20 66 6f 6c 6c ups...%I.e. foll
0330: 6f 77 69 6e 67 20 77 69 6c 6c 20 67 6f 20 66 72 owing will go fr
0340: 6f 6d 20 67 72 6f 75 70 73 20 6f 66 20 66 69 76 om groups of fiv
0350: 65 20 74 6f 20 65 69 67 68 74 2e 0a 09 25 2d 2d e to eight...%--
0360: 2d 0a 09 25 53 74 65 70 20 35 0a 09 45 69 67 68 -..%Step 5..Eigh
0370: 74 5f 62 69 74 5f 63 68 75 6e 6b 73 20 3d 20 6c t_bit_chunks = l
0380: 69 73 74 73 3a 6d 61 70 28 0a 09 09 66 75 6e 28 ists:map(...fun(
0390: 47 72 6f 75 70 5f 6f 66 5f 63 68 75 6e 6b 73 29 Group_of_chunks)
03a0: 20 2d 3e 0a 09 09 09 65 69 67 68 74 5f 62 69 74 ->....eight_bit
03b0: 5f 63 68 75 6e 6b 73 28 47 72 6f 75 70 5f 6f 66 _chunks(Group_of
03c0: 5f 63 68 75 6e 6b 73 29 0a 09 09 65 6e 64 2c 0a _chunks)...end,.
03d0: 09 09 46 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b ..Five_bit_chunk
03e0: 73 29 2c 0a 09 52 65 73 75 6c 74 73 20 3d 20 6c s),..Results = l
03f0: 69 73 74 73 3a 6d 61 70 28 0a 09 09 66 75 6e 28 ists:map(...fun(
0400: 47 72 6f 75 70 5f 6f 66 5f 63 68 75 6e 6b 73 29 Group_of_chunks)
0410: 20 2d 3e 0a 09 09 09 25 54 4f 44 4f 20 54 68 65 ->....%TODO The
0420: 73 65 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 se should probab
0430: 6c 79 20 62 65 20 73 65 70 61 72 61 74 65 20 66 ly be separate f
0440: 75 6e 63 74 69 6f 6e 73 2c 20 72 61 74 68 65 72 unctions, rather
0450: 20 74 68 61 6e 20 6f 6e 65 20 6c 6f 6e 67 20 6d than one long m
0460: 65 73 73 20 69 6e 73 69 64 65 20 61 20 6d 61 70 ess inside a map
0470: 0a 09 09 09 4c 61 73 74 5f 62 69 74 20 3d 20 5b ....Last_bit = [
0480: 68 64 28 6c 69 73 74 73 3a 72 65 76 65 72 73 65 hd(lists:reverse
0490: 28 68 64 28 6c 69 73 74 73 3a 72 65 76 65 72 73 (hd(lists:revers
04a0: 65 28 47 72 6f 75 70 5f 6f 66 5f 63 68 75 6e 6b e(Group_of_chunk
04b0: 73 29 29 29 29 5d 2c 0a 09 09 09 46 6c 69 70 70 s))))],....Flipp
04c0: 65 64 5f 63 68 75 6e 6b 73 20 3d 20 6c 69 73 74 ed_chunks = list
04d0: 73 3a 6d 61 70 28 0a 09 09 09 09 66 75 6e 28 43 s:map(.....fun(C
04e0: 68 75 6e 6b 29 20 2d 3e 0a 09 09 09 09 09 62 69 hunk) ->......bi
04f0: 6e 5f 66 6c 69 70 5f 28 43 68 75 6e 6b 2c 20 5b n_flip_(Chunk, [
0500: 5d 29 0a 09 09 09 09 65 6e 64 2c 0a 09 09 09 09 ]).....end,.....
0510: 47 72 6f 75 70 5f 6f 66 5f 63 68 75 6e 6b 73 29 Group_of_chunks)
0520: 2c 0a 09 09 09 25 53 74 65 70 20 35 0a 09 09 09 ,....%Step 5....
0530: 43 68 75 6e 6b 73 20 3d 20 69 66 20 4c 61 73 74 Chunks = if Last
0540: 5f 62 69 74 20 3d 3a 3d 20 22 31 22 20 2d 3e 0a _bit =:= "1" ->.
0550: 09 09 09 09 46 6c 69 70 70 65 64 5f 63 68 75 6e ....Flipped_chun
0560: 6b 73 3b 0a 09 09 09 74 72 75 65 20 2d 3e 0a 09 ks;....true ->..
0570: 09 09 09 47 72 6f 75 70 5f 6f 66 5f 63 68 75 6e ...Group_of_chun
0580: 6b 73 0a 09 09 09 65 6e 64 2c 0a 09 09 09 7b 6f ks....end,....{o
0590: 6b 2c 20 5b 46 6c 61 74 74 65 6e 65 64 5f 62 69 k, [Flattened_bi
05a0: 6e 61 72 79 5d 2c 20 5b 5d 7d 20 3d 20 69 6f 5f nary], []} = io_
05b0: 6c 69 62 3a 66 72 65 61 64 28 22 7e 32 75 22 2c lib:fread("~2u",
05c0: 20 6c 69 73 74 73 3a 66 6c 61 74 74 65 6e 28 43 lists:flatten(C
05d0: 68 75 6e 6b 73 29 29 2c 0a 09 09 09 25 53 74 65 hunks)),....%Ste
05e0: 70 20 34 0a 09 09 09 53 68 69 66 74 65 64 5f 62 p 4....Shifted_b
05f0: 69 6e 61 72 79 20 3d 20 46 6c 61 74 74 65 6e 65 inary = Flattene
0600: 64 5f 62 69 6e 61 72 79 20 62 73 72 20 31 2c 0a d_binary bsr 1,.
0610: 09 09 09 25 53 69 6e 63 65 20 62 69 6e 5f 66 6c ...%Since bin_fl
0620: 69 70 20 72 65 74 75 72 6e 73 20 61 20 73 74 72 ip returns a str
0630: 69 6e 67 20 6e 65 65 64 20 74 6f 20 74 68 65 6e ing need to then
0640: 20 63 68 61 6e 67 65 20 62 61 63 6b 20 74 6f 20 change back to
0650: 61 20 6e 75 6d 62 65 72 0a 09 09 09 7b 6f 6b 2c a number....{ok,
0660: 20 5b 53 68 69 66 74 65 64 5f 62 69 6e 61 72 79 [Shifted_binary
0670: 5f 5d 2c 20 5b 5d 7d 20 3d 20 69 6f 5f 6c 69 62 _], []} = io_lib
0680: 3a 66 72 65 61 64 28 22 7e 32 75 22 2c 20 62 69 :fread("~2u", bi
0690: 6e 5f 66 6c 69 70 28 53 68 69 66 74 65 64 5f 62 n_flip(Shifted_b
06a0: 69 6e 61 72 79 20 2d 20 31 29 29 2c 0a 09 09 09 inary - 1)),....
06b0: 25 53 74 65 70 20 33 0a 09 09 09 46 69 6e 61 6c %Step 3....Final
06c0: 5f 62 69 6e 61 72 79 20 3d 20 69 66 20 4c 61 73 _binary = if Las
06d0: 74 5f 62 69 74 20 3d 3a 3d 20 22 31 22 20 2d 3e t_bit =:= "1" ->
06e0: 0a 09 09 09 09 53 68 69 66 74 65 64 5f 62 69 6e .....Shifted_bin
06f0: 61 72 79 5f 3b 0a 09 09 09 74 72 75 65 20 2d 3e ary_;....true ->
0700: 0a 09 09 09 09 53 68 69 66 74 65 64 5f 62 69 6e .....Shifted_bin
0710: 61 72 79 0a 09 09 09 65 6e 64 2c 0a 09 09 09 25 ary....end,....%
0720: 53 74 65 70 20 32 20 62 61 63 6b 20 74 6f 20 31 Step 2 back to 1
0730: 0a 09 09 09 44 65 63 6f 64 65 64 20 3d 20 69 66 ....Decoded = if
0740: 20 4c 61 73 74 5f 62 69 74 20 3d 3a 3d 20 22 31 Last_bit =:= "1
0750: 22 20 2d 3e 0a 09 09 09 09 2d 31 20 2a 20 46 69 " ->.....-1 * Fi
0760: 6e 61 6c 5f 62 69 6e 61 72 79 2f 31 30 30 30 30 nal_binary/10000
0770: 30 3b 0a 09 09 09 74 72 75 65 20 2d 3e 0a 09 09 0;....true ->...
0780: 09 09 46 69 6e 61 6c 5f 62 69 6e 61 72 79 2f 31 ..Final_binary/1
0790: 30 30 30 30 30 0a 09 09 09 65 6e 64 2c 0a 09 09 00000....end,...
07a0: 09 44 65 63 6f 64 65 64 0a 09 09 65 6e 64 2c 0a .Decoded...end,.
07b0: 09 09 45 69 67 68 74 5f 62 69 74 5f 63 68 75 6e ..Eight_bit_chun
07c0: 6b 73 29 2c 0a 09 52 65 73 75 6c 74 73 2e 0a 09 ks),..Results...
07d0: 0a 0a 25 53 74 65 70 20 38 20 2d 20 53 70 6c 69 ..%Step 8 - Spli
07e0: 74 20 75 70 20 73 69 78 20 62 69 74 20 63 68 75 t up six bit chu
07f0: 6e 6b 73 2c 20 70 65 72 20 74 68 65 20 30 78 32 nks, per the 0x2
0800: 30 20 62 69 74 0a 73 70 6c 69 74 5f 75 70 5f 73 0 bit.split_up_s
0810: 69 78 5f 62 69 74 73 28 42 69 74 5f 63 68 75 6e ix_bits(Bit_chun
0820: 6b 73 5f 6c 69 73 74 29 20 2d 3e 0a 09 73 70 6c ks_list) ->..spl
0830: 69 74 5f 75 70 5f 73 69 78 5f 62 69 74 73 5f 28 it_up_six_bits_(
0840: 42 69 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 2c Bit_chunks_list,
0850: 20 5b 5d 2c 20 5b 5d 29 2e 0a 73 70 6c 69 74 5f [], [])..split_
0860: 75 70 5f 73 69 78 5f 62 69 74 73 5f 28 5b 48 65 up_six_bits_([He
0870: 61 64 20 7c 20 54 61 69 6c 5d 2c 20 47 72 6f 75 ad | Tail], Grou
0880: 70 5f 6f 66 5f 62 69 74 5f 63 68 75 6e 6b 73 2c p_of_bit_chunks,
0890: 20 47 72 6f 75 70 73 5f 6f 66 5f 62 69 74 5f 63 Groups_of_bit_c
08a0: 68 75 6e 6b 73 5f 6c 69 73 74 29 20 77 68 65 6e hunks_list) when
08b0: 20 5b 68 64 28 48 65 61 64 29 5d 20 3d 3d 20 22 [hd(Head)] == "
08c0: 31 22 20 2d 3e 0a 09 73 70 6c 69 74 5f 75 70 5f 1" ->..split_up_
08d0: 73 69 78 5f 62 69 74 73 5f 28 54 61 69 6c 2c 20 six_bits_(Tail,
08e0: 5b 48 65 61 64 5d 2b 2b 47 72 6f 75 70 5f 6f 66 [Head]++Group_of
08f0: 5f 62 69 74 5f 63 68 75 6e 6b 73 2c 20 47 72 6f _bit_chunks, Gro
0900: 75 70 73 5f 6f 66 5f 62 69 74 5f 63 68 75 6e 6b ups_of_bit_chunk
0910: 73 5f 6c 69 73 74 29 3b 0a 73 70 6c 69 74 5f 75 s_list);.split_u
0920: 70 5f 73 69 78 5f 62 69 74 73 5f 28 5b 48 65 61 p_six_bits_([Hea
0930: 64 20 7c 20 54 61 69 6c 5d 2c 20 47 72 6f 75 70 d | Tail], Group
0940: 5f 6f 66 5f 62 69 74 5f 63 68 75 6e 6b 73 2c 20 _of_bit_chunks,
0950: 47 72 6f 75 70 73 5f 6f 66 5f 62 69 74 5f 63 68 Groups_of_bit_ch
0960: 75 6e 6b 73 5f 6c 69 73 74 29 20 77 68 65 6e 20 unks_list) when
0970: 5b 68 64 28 48 65 61 64 29 5d 20 3d 3d 20 22 30 [hd(Head)] == "0
0980: 22 20 2d 3e 0a 09 25 54 68 65 6e 20 6e 65 65 64 " ->..%Then need
0990: 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20 to start a new
09a0: 6c 69 73 74 2c 20 62 75 74 20 61 66 74 65 72 20 list, but after
09b0: 74 68 69 73 20 30 20 6f 6e 65 21 0a 09 73 70 6c this 0 one!..spl
09c0: 69 74 5f 75 70 5f 73 69 78 5f 62 69 74 73 5f 28 it_up_six_bits_(
09d0: 54 61 69 6c 2c 20 5b 5d 2c 20 5b 6c 69 73 74 73 Tail, [], [lists
09e0: 3a 72 65 76 65 72 73 65 28 5b 48 65 61 64 5d 2b :reverse([Head]+
09f0: 2b 47 72 6f 75 70 5f 6f 66 5f 62 69 74 5f 63 68 +Group_of_bit_ch
0a00: 75 6e 6b 73 29 5d 2b 2b 47 72 6f 75 70 73 5f 6f unks)]++Groups_o
0a10: 66 5f 62 69 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 f_bit_chunks_lis
0a20: 74 29 3b 0a 73 70 6c 69 74 5f 75 70 5f 73 69 78 t);.split_up_six
0a30: 5f 62 69 74 73 5f 28 5b 5d 2c 20 47 72 6f 75 70 _bits_([], Group
0a40: 5f 6f 66 5f 62 69 74 5f 63 68 75 6e 6b 73 2c 20 _of_bit_chunks,
0a50: 47 72 6f 75 70 73 5f 6f 66 5f 62 69 74 5f 63 68 Groups_of_bit_ch
0a60: 75 6e 6b 73 5f 6c 69 73 74 29 20 77 68 65 6e 20 unks_list) when
0a70: 6c 65 6e 67 74 68 28 47 72 6f 75 70 5f 6f 66 5f length(Group_of_
0a80: 62 69 74 5f 63 68 75 6e 6b 73 29 20 3e 20 30 20 bit_chunks) > 0
0a90: 2d 3e 0a 09 73 70 6c 69 74 5f 75 70 5f 73 69 78 ->..split_up_six
0aa0: 5f 62 69 74 73 5f 28 5b 5d 2c 20 5b 5d 2c 20 5b _bits_([], [], [
0ab0: 6c 69 73 74 73 3a 72 65 76 65 72 73 65 28 47 72 lists:reverse(Gr
0ac0: 6f 75 70 5f 6f 66 5f 62 69 74 5f 63 68 75 6e 6b oup_of_bit_chunk
0ad0: 73 29 5d 2b 2b 47 72 6f 75 70 73 5f 6f 66 5f 62 s)]++Groups_of_b
0ae0: 69 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 29 3b it_chunks_list);
0af0: 0a 73 70 6c 69 74 5f 75 70 5f 73 69 78 5f 62 69 .split_up_six_bi
0b00: 74 73 5f 28 5b 5d 2c 20 5b 5d 2c 20 47 72 6f 75 ts_([], [], Grou
0b10: 70 73 5f 6f 66 5f 62 69 74 5f 63 68 75 6e 6b 73 ps_of_bit_chunks
0b20: 5f 6c 69 73 74 29 20 2d 3e 0a 09 25 54 4f 44 4f _list) ->..%TODO
0b30: 20 4d 69 67 68 74 20 62 65 20 6e 65 61 74 65 72 Might be neater
0b40: 20 74 6f 20 6d 61 70 20 6c 69 73 74 73 3a 72 65 to map lists:re
0b50: 76 65 72 73 65 20 6f 76 65 72 20 74 68 65 20 6c verse over the l
0b60: 69 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 64 ist instead of d
0b70: 6f 69 6e 67 20 61 62 6f 76 65 20 61 6e 64 20 68 oing above and h
0b80: 65 72 65 2e 0a 09 6c 69 73 74 73 3a 72 65 76 65 ere...lists:reve
0b90: 72 73 65 28 47 72 6f 75 70 73 5f 6f 66 5f 62 69 rse(Groups_of_bi
0ba0: 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 29 2e 0a t_chunks_list)..
0bb0: 0a 0a 25 53 74 65 70 20 35 0a 25 54 4f 44 4f 20 ..%Step 5.%TODO
0bc0: 53 65 65 20 69 66 20 62 65 74 74 65 72 20 77 61 See if better wa
0bd0: 79 20 6f 66 20 64 6f 69 6e 67 20 74 68 69 73 0a y of doing this.
0be0: 65 69 67 68 74 5f 62 69 74 5f 63 68 75 6e 6b 73 eight_bit_chunks
0bf0: 28 46 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b 73 (Five_bit_chunks
0c00: 5f 6c 69 73 74 29 20 2d 3e 0a 09 46 69 76 65 5f _list) ->..Five_
0c10: 62 69 74 5f 63 68 75 6e 6b 5f 73 74 72 69 6e 67 bit_chunk_string
0c20: 20 3d 20 6c 69 73 74 73 3a 72 65 76 65 72 73 65 = lists:reverse
0c30: 28 6c 69 73 74 73 3a 66 6c 61 74 74 65 6e 28 46 (lists:flatten(F
0c40: 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b 73 5f 6c ive_bit_chunks_l
0c50: 69 73 74 29 29 2c 0a 09 65 69 67 68 74 5f 62 69 ist)),..eight_bi
0c60: 74 5f 63 68 75 6e 6b 73 5f 28 46 69 76 65 5f 62 t_chunks_(Five_b
0c70: 69 74 5f 63 68 75 6e 6b 5f 73 74 72 69 6e 67 2c it_chunk_string,
0c80: 20 5b 5d 29 2e 0a 65 69 67 68 74 5f 62 69 74 5f [])..eight_bit_
0c90: 63 68 75 6e 6b 73 5f 28 46 69 76 65 5f 62 69 74 chunks_(Five_bit
0ca0: 5f 63 68 75 6e 6b 5f 73 74 72 69 6e 67 2c 20 45 _chunk_string, E
0cb0: 69 67 68 74 5f 62 69 74 5f 63 68 75 6e 6b 73 5f ight_bit_chunks_
0cc0: 6c 69 73 74 29 20 77 68 65 6e 20 6c 65 6e 67 74 list) when lengt
0cd0: 68 28 46 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b h(Five_bit_chunk
0ce0: 5f 73 74 72 69 6e 67 29 20 3e 20 38 20 2d 3e 0a _string) > 8 ->.
0cf0: 09 45 69 67 68 74 5f 62 69 74 5f 63 68 75 6e 6b .Eight_bit_chunk
0d00: 20 3d 20 6c 69 73 74 73 3a 72 65 76 65 72 73 65 = lists:reverse
0d10: 28 6c 69 73 74 73 3a 73 75 62 6c 69 73 74 28 46 (lists:sublist(F
0d20: 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b 5f 73 74 ive_bit_chunk_st
0d30: 72 69 6e 67 2c 31 2c 38 29 29 2c 0a 09 52 65 73 ring,1,8)),..Res
0d40: 74 5f 6f 66 5f 66 69 76 65 5f 62 69 74 5f 63 68 t_of_five_bit_ch
0d50: 75 6e 6b 5f 73 74 72 69 6e 67 20 3d 20 6c 69 73 unk_string = lis
0d60: 74 73 3a 6e 74 68 74 61 69 6c 28 38 2c 46 69 76 ts:nthtail(8,Fiv
0d70: 65 5f 62 69 74 5f 63 68 75 6e 6b 5f 73 74 72 69 e_bit_chunk_stri
0d80: 6e 67 29 2c 0a 09 65 69 67 68 74 5f 62 69 74 5f ng),..eight_bit_
0d90: 63 68 75 6e 6b 73 5f 28 52 65 73 74 5f 6f 66 5f chunks_(Rest_of_
0da0: 66 69 76 65 5f 62 69 74 5f 63 68 75 6e 6b 5f 73 five_bit_chunk_s
0db0: 74 72 69 6e 67 2c 20 5b 45 69 67 68 74 5f 62 69 tring, [Eight_bi
0dc0: 74 5f 63 68 75 6e 6b 5d 2b 2b 45 69 67 68 74 5f t_chunk]++Eight_
0dd0: 62 69 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 29 bit_chunks_list)
0de0: 3b 0a 65 69 67 68 74 5f 62 69 74 5f 63 68 75 6e ;.eight_bit_chun
0df0: 6b 73 5f 28 46 69 76 65 5f 62 69 74 5f 63 68 75 ks_(Five_bit_chu
0e00: 6e 6b 5f 73 74 72 69 6e 67 2c 20 45 69 67 68 74 nk_string, Eight
0e10: 5f 62 69 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 _bit_chunks_list
0e20: 29 20 77 68 65 6e 20 6c 65 6e 67 74 68 28 46 69 ) when length(Fi
0e30: 76 65 5f 62 69 74 5f 63 68 75 6e 6b 5f 73 74 72 ve_bit_chunk_str
0e40: 69 6e 67 29 20 3d 3c 20 38 2c 20 46 69 76 65 5f ing) =< 8, Five_
0e50: 62 69 74 5f 63 68 75 6e 6b 5f 73 74 72 69 6e 67 bit_chunk_string
0e60: 20 2f 3d 20 5b 5d 20 2d 3e 0a 09 50 61 64 64 65 /= [] ->..Padde
0e70: 64 5f 62 69 74 5f 73 74 72 69 6e 67 20 3d 20 70 d_bit_string = p
0e80: 61 64 5f 74 6f 28 38 2c 20 6c 69 73 74 73 3a 72 ad_to(8, lists:r
0e90: 65 76 65 72 73 65 28 46 69 76 65 5f 62 69 74 5f everse(Five_bit_
0ea0: 63 68 75 6e 6b 5f 73 74 72 69 6e 67 29 29 2c 0a chunk_string)),.
0eb0: 09 65 69 67 68 74 5f 62 69 74 5f 63 68 75 6e 6b .eight_bit_chunk
0ec0: 73 5f 28 5b 5d 2c 20 5b 50 61 64 64 65 64 5f 62 s_([], [Padded_b
0ed0: 69 74 5f 73 74 72 69 6e 67 5d 2b 2b 45 69 67 68 it_string]++Eigh
0ee0: 74 5f 62 69 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 t_bit_chunks_lis
0ef0: 74 29 3b 0a 65 69 67 68 74 5f 62 69 74 5f 63 68 t);.eight_bit_ch
0f00: 75 6e 6b 73 5f 28 5b 5d 2c 20 45 69 67 68 74 5f unks_([], Eight_
0f10: 62 69 74 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 29 bit_chunks_list)
0f20: 20 2d 3e 0a 09 45 69 67 68 74 5f 62 69 74 5f 63 ->..Eight_bit_c
0f30: 68 75 6e 6b 73 5f 6c 69 73 74 2e 0a 0a 0a 73 69 hunks_list....si
0f40: 78 5f 62 69 74 5f 63 68 75 6e 6b 73 28 45 6e 63 x_bit_chunks(Enc
0f50: 6f 64 65 64 5f 70 6f 6c 79 6c 69 6e 65 29 20 2d oded_polyline) -
0f60: 3e 0a 09 73 69 78 5f 62 69 74 5f 63 68 75 6e 6b >..six_bit_chunk
0f70: 73 5f 28 45 6e 63 6f 64 65 64 5f 70 6f 6c 79 6c s_(Encoded_polyl
0f80: 69 6e 65 2c 20 5b 5d 29 2e 0a 73 69 78 5f 62 69 ine, [])..six_bi
0f90: 74 5f 63 68 75 6e 6b 73 5f 28 5b 48 65 61 64 20 t_chunks_([Head
0fa0: 7c 20 52 65 73 74 5d 2c 20 43 68 75 6e 6b 73 5f | Rest], Chunks_
0fb0: 6c 69 73 74 29 20 2d 3e 0a 09 53 69 78 5f 62 69 list) ->..Six_bi
0fc0: 74 5f 63 68 75 6e 6b 20 3d 20 73 69 78 5f 62 69 t_chunk = six_bi
0fd0: 74 5f 63 68 75 6e 6b 28 48 65 61 64 29 2c 0a 09 t_chunk(Head),..
0fe0: 25 41 64 64 20 74 6f 20 52 65 76 65 72 73 65 64 %Add to Reversed
0ff0: 5f 63 68 75 6e 6b 73 0a 09 73 69 78 5f 62 69 74 _chunks..six_bit
1000: 5f 63 68 75 6e 6b 73 5f 28 52 65 73 74 2c 20 5b _chunks_(Rest, [
1010: 53 69 78 5f 62 69 74 5f 63 68 75 6e 6b 5d 2b 2b Six_bit_chunk]++
1020: 43 68 75 6e 6b 73 5f 6c 69 73 74 29 3b 0a 73 69 Chunks_list);.si
1030: 78 5f 62 69 74 5f 63 68 75 6e 6b 73 5f 28 5b 5d x_bit_chunks_([]
1040: 2c 20 43 68 75 6e 6b 73 5f 6c 69 73 74 29 20 2d , Chunks_list) -
1050: 3e 0a 09 6c 69 73 74 73 3a 72 65 76 65 72 73 65 >..lists:reverse
1060: 28 43 68 75 6e 6b 73 5f 6c 69 73 74 29 2e 0a 0a (Chunks_list)...
1070: 0a 73 69 78 5f 62 69 74 5f 63 68 75 6e 6b 28 41 .six_bit_chunk(A
1080: 73 63 69 69 5f 62 69 74 29 20 2d 3e 0a 09 25 53 scii_bit) ->..%S
1090: 74 65 70 20 31 30 0a 09 53 68 69 66 74 65 64 5f tep 10..Shifted_
10a0: 62 69 74 20 3d 20 41 73 63 69 69 5f 62 69 74 20 bit = Ascii_bit
10b0: 2d 20 36 33 2c 0a 09 25 53 74 65 70 20 39 0a 09 - 63,..%Step 9..
10c0: 25 46 72 6f 6d 20 68 74 74 70 3a 2f 2f 65 72 6c %From http://erl
10d0: 61 6e 67 63 65 6e 74 72 61 6c 2e 6f 72 67 2f 77 angcentral.org/w
10e0: 69 6b 69 2f 69 6e 64 65 78 2e 70 68 70 2f 43 6f iki/index.php/Co
10f0: 6e 76 65 72 74 69 6e 67 5f 42 65 74 77 65 65 6e nverting_Between
1100: 5f 42 69 6e 61 72 79 5f 61 6e 64 5f 44 65 63 69 _Binary_and_Deci
1110: 6d 61 6c 0a 09 42 69 6e 61 72 79 5f 63 68 75 6e mal..Binary_chun
1120: 6b 20 3d 20 68 64 28 69 6f 5f 6c 69 62 3a 66 6f k = hd(io_lib:fo
1130: 72 6d 61 74 28 22 7e 2e 32 42 22 2c 20 5b 53 68 rmat("~.2B", [Sh
1140: 69 66 74 65 64 5f 62 69 74 5d 29 29 2c 0a 09 25 ifted_bit])),..%
1150: 2d 2d 2d 54 4f 44 4f 0a 09 25 57 68 61 74 20 69 ---TODO..%What i
1160: 66 20 42 69 6e 61 72 79 5f 63 68 75 6e 6b 20 69 f Binary_chunk i
1170: 73 20 73 68 6f 72 74 65 72 20 74 68 61 6e 20 36 s shorter than 6
1180: 3f 0a 09 25 57 65 6c 6c 20 69 6e 20 74 68 61 74 ?..%Well in that
1190: 20 63 61 73 65 2c 20 49 20 67 75 65 73 73 20 74 case, I guess t
11a0: 68 61 74 20 6d 65 61 6e 73 20 77 65 27 64 20 77 hat means we'd w
11b0: 61 6e 74 20 74 6f 20 73 70 6c 69 74 2c 20 62 75 ant to split, bu
11c0: 74 20 66 6f 72 20 6e 6f 77 20 70 61 64 20 74 6f t for now pad to
11d0: 20 73 69 78 20 61 6e 64 20 63 68 65 63 6b 20 66 six and check f
11e0: 6f 72 20 30 78 32 30 20 65 6c 73 65 77 68 65 72 or 0x20 elsewher
11f0: 65 2e 0a 09 25 2d 2d 2d 0a 09 70 61 64 5f 74 6f e...%---..pad_to
1200: 28 36 2c 20 42 69 6e 61 72 79 5f 63 68 75 6e 6b (6, Binary_chunk
1210: 29 2e 0a 0a 0a 66 69 76 65 5f 62 69 74 5f 63 68 )....five_bit_ch
1220: 75 6e 6b 73 28 47 72 6f 75 70 73 5f 6f 66 5f 63 unks(Groups_of_c
1230: 68 75 6e 6b 73 5f 6c 69 73 74 29 20 2d 3e 0a 09 hunks_list) ->..
1240: 6c 69 73 74 73 3a 6d 61 70 28 0a 09 09 66 75 6e lists:map(...fun
1250: 28 47 72 6f 75 70 5f 6f 66 5f 63 68 75 6e 6b 73 (Group_of_chunks
1260: 29 20 2d 3e 0a 09 09 09 25 53 74 65 70 20 37 20 ) ->....%Step 7
1270: 2d 20 55 6e 2d 72 65 76 65 72 73 65 20 74 68 65 - Un-reverse the
1280: 20 66 69 76 65 20 62 69 74 20 63 68 75 6e 6b 73 five bit chunks
1290: 0a 09 09 09 6c 69 73 74 73 3a 72 65 76 65 72 73 ....lists:revers
12a0: 65 28 6c 69 73 74 73 3a 6d 61 70 28 0a 09 09 09 e(lists:map(....
12b0: 09 66 75 6e 28 43 68 75 6e 6b 29 20 2d 3e 0a 09 .fun(Chunk) ->..
12c0: 09 09 09 09 25 53 74 65 70 20 38 20 2d 20 22 55 ....%Step 8 - "U
12d0: 6e 2d 6f 72 22 20 74 68 65 20 30 78 32 30 20 62 n-or" the 0x20 b
12e0: 69 74 0a 09 09 09 09 09 6c 69 73 74 73 3a 73 75 it......lists:su
12f0: 62 6c 69 73 74 28 43 68 75 6e 6b 2c 32 2c 36 29 blist(Chunk,2,6)
1300: 0a 09 09 09 09 65 6e 64 2c 0a 09 09 09 09 47 72 .....end,.....Gr
1310: 6f 75 70 5f 6f 66 5f 63 68 75 6e 6b 73 29 29 0a oup_of_chunks)).
1320: 09 09 65 6e 64 2c 0a 09 09 47 72 6f 75 70 73 5f ..end,...Groups_
1330: 6f 66 5f 63 68 75 6e 6b 73 5f 6c 69 73 74 29 2e of_chunks_list).
1340: 0a 0a 0a 25 49 20 63 61 6e 27 74 20 66 69 67 75 ...%I can't figu
1350: 72 65 20 6f 75 74 20 70 61 64 64 69 6e 67 20 77 re out padding w
1360: 69 74 68 20 69 6f 3a 66 6f 72 6d 61 74 20 65 74 ith io:format et
1370: 63 20 77 68 65 6e 20 70 72 69 6e 74 69 6e 67 20 c when printing
1380: 62 69 6e 61 72 79 20 6e 75 6d 62 65 72 73 0a 70 binary numbers.p
1390: 61 64 5f 74 6f 28 4c 65 6e 67 74 68 2c 20 42 69 ad_to(Length, Bi
13a0: 6e 61 72 79 5f 73 74 72 69 6e 67 29 20 77 68 65 nary_string) whe
13b0: 6e 20 6c 65 6e 67 74 68 28 42 69 6e 61 72 79 5f n length(Binary_
13c0: 73 74 72 69 6e 67 29 20 3c 20 4c 65 6e 67 74 68 string) < Length
13d0: 20 2d 3e 0a 09 50 61 64 64 65 64 5f 62 69 6e 61 ->..Padded_bina
13e0: 72 79 5f 73 74 72 69 6e 67 20 3d 20 22 30 22 2b ry_string = "0"+
13f0: 2b 42 69 6e 61 72 79 5f 73 74 72 69 6e 67 2c 0a +Binary_string,.
1400: 09 70 61 64 5f 74 6f 28 4c 65 6e 67 74 68 2c 20 .pad_to(Length,
1410: 50 61 64 64 65 64 5f 62 69 6e 61 72 79 5f 73 74 Padded_binary_st
1420: 72 69 6e 67 29 3b 0a 70 61 64 5f 74 6f 28 4c 65 ring);.pad_to(Le
1430: 6e 67 74 68 2c 20 42 69 6e 61 72 79 5f 73 74 72 ngth, Binary_str
1440: 69 6e 67 29 20 77 68 65 6e 20 6c 65 6e 67 74 68 ing) when length
1450: 28 42 69 6e 61 72 79 5f 73 74 72 69 6e 67 29 20 (Binary_string)
1460: 3d 3d 20 4c 65 6e 67 74 68 20 2d 3e 0a 09 42 69 == Length ->..Bi
1470: 6e 61 72 79 5f 73 74 72 69 6e 67 2e 0a 0a 0a 25 nary_string....%
1480: 62 6e 6f 74 20 64 6f 65 73 6e 27 74 20 73 65 65 bnot doesn't see
1490: 6d 20 74 6f 20 77 6f 72 6b 20 61 73 20 49 20 74 m to work as I t
14a0: 68 6f 75 67 68 74 20 69 74 20 77 6f 75 6c 64 20 hought it would
14b0: 73 6f 20 64 6f 20 69 74 20 76 65 72 79 20 69 6e so do it very in
14c0: 65 6c 65 67 61 6e 74 6c 79 20 62 79 20 73 77 69 elegantly by swi
14d0: 74 63 68 69 6e 67 20 65 61 63 68 20 22 62 69 74 tching each "bit
14e0: 22 20 69 6e 20 61 20 73 74 72 69 6e 67 2e 0a 62 " in a string..b
14f0: 69 6e 5f 66 6c 69 70 28 42 69 6e 61 72 79 5f 6e in_flip(Binary_n
1500: 75 6d 62 65 72 29 20 2d 3e 0a 09 42 69 6e 61 72 umber) ->..Binar
1510: 79 5f 73 74 72 69 6e 67 20 3d 20 68 64 28 69 6f y_string = hd(io
1520: 5f 6c 69 62 3a 66 6f 72 6d 61 74 28 22 7e 2e 32 _lib:format("~.2
1530: 42 22 2c 20 5b 42 69 6e 61 72 79 5f 6e 75 6d 62 B", [Binary_numb
1540: 65 72 5d 29 29 2c 0a 09 62 69 6e 5f 66 6c 69 70 er])),..bin_flip
1550: 5f 28 42 69 6e 61 72 79 5f 73 74 72 69 6e 67 2c _(Binary_string,
1560: 20 5b 5d 29 2e 0a 62 69 6e 5f 66 6c 69 70 5f 28 [])..bin_flip_(
1570: 5b 48 65 61 64 20 7c 20 52 65 73 74 5d 2c 20 46 [Head | Rest], F
1580: 6c 69 70 70 65 64 5f 73 74 72 69 6e 67 29 20 2d lipped_string) -
1590: 3e 0a 09 48 65 61 64 5f 62 69 74 20 3d 20 68 64 >..Head_bit = hd
15a0: 28 69 6f 5f 6c 69 62 3a 66 6f 72 6d 61 74 28 22 (io_lib:format("
15b0: 7e 63 22 2c 5b 48 65 61 64 5d 29 29 2c 0a 09 46 ~c",[Head])),..F
15c0: 6c 69 70 70 65 64 5f 62 69 74 20 3d 20 69 66 20 lipped_bit = if
15d0: 48 65 61 64 5f 62 69 74 20 3d 3a 3d 20 22 30 22 Head_bit =:= "0"
15e0: 20 2d 3e 0a 09 09 22 31 22 3b 0a 09 74 72 75 65 ->..."1";..true
15f0: 20 2d 3e 0a 09 09 22 30 22 0a 09 65 6e 64 2c 0a ->..."0"..end,.
1600: 09 62 69 6e 5f 66 6c 69 70 5f 28 52 65 73 74 2c .bin_flip_(Rest,
1610: 20 46 6c 69 70 70 65 64 5f 62 69 74 2b 2b 46 6c Flipped_bit++Fl
1620: 69 70 70 65 64 5f 73 74 72 69 6e 67 29 3b 0a 62 ipped_string);.b
1630: 69 6e 5f 66 6c 69 70 5f 28 5b 5d 2c 20 46 6c 69 in_flip_([], Fli
1640: 70 70 65 64 5f 73 74 72 69 6e 67 29 20 2d 3e 0a pped_string) ->.
1650: 09 6c 69 73 74 73 3a 72 65 76 65 72 73 65 28 46 .lists:reverse(F
1660: 6c 69 70 70 65 64 5f 73 74 72 69 6e 67 29 2e 0a lipped_string)..
1670: 0a 0a 25 4e 6f 74 65 73 0a 09 25 54 6f 20 65 6e ..%Notes..%To en
1680: 74 65 72 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 ter numbers of a
1690: 20 64 69 66 66 65 72 65 6e 74 20 62 61 73 65 20 different base
16a0: 32 23 31 30 31 30 31 30 0a 09 25 30 78 31 66 20 2#101010..%0x1f
16b0: 69 73 20 33 31 2c 20 30 78 32 30 20 69 73 20 33 is 31, 0x20 is 3
16c0: 32 0a 09 25 4e 65 65 64 20 74 6f 20 65 6e 73 75 2..%Need to ensu
16d0: 72 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 re functions are
16e0: 20 74 61 69 6c 20 72 65 63 75 72 73 69 76 65 0a tail recursive.
16f0: 09 25 54 6f 20 63 6f 6e 76 65 72 74 20 74 6f 20 .%To convert to
1700: 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 6e 65 integer, just ne
1710: 65 64 20 74 6f 20 64 6f 20 24 3c 63 68 61 72 3e ed to do $<char>
1720: 0a 09 25 6a 75 73 74 20 64 6f 20 68 64 28 53 74 ..%just do hd(St
1730: 72 69 6e 67 29 20 77 69 6c 6c 20 63 6f 6d 65 20 ring) will come
1740: 61 73 20 61 20 6e 75 6d 62 65 72 0a 09 25 4c 61 as a number..%La
1750: 73 74 20 3d 20 68 64 28 69 6f 5f 6c 69 62 3a 66 st = hd(io_lib:f
1760: 6f 72 6d 61 74 28 22 7e 63 22 2c 20 5b 68 64 28 ormat("~c", [hd(
1770: 6c 69 73 74 73 3a 72 65 76 65 72 73 65 28 68 64 lists:reverse(hd
1780: 28 69 6f 5f 6c 69 62 3a 66 6f 72 6d 61 74 28 22 (io_lib:format("
1790: 7e 2e 32 42 22 2c 20 5b 42 69 6e 61 72 79 5f 6e ~.2B", [Binary_n
17a0: 75 6d 62 65 72 5d 29 29 29 29 5d 29 29 2c 0a 09 umber]))))])),..
17b0: 25 7b 6f 6b 2c 20 41 6c 6d 6f 73 74 5f 66 6c 69 %{ok, Almost_fli
17c0: 70 70 65 64 2c 20 5f 7d 20 3d 20 69 6f 5f 6c 69 pped, _} = io_li
17d0: 62 3a 66 72 65 61 64 28 22 7e 32 75 22 2c 41 6c b:fread("~2u",Al
17e0: 6d 6f 73 74 5f 66 6c 69 70 70 65 64 5f 73 74 72 most_flipped_str
17f0: 69 6e 67 29 2c 0a ing),.