| 1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
| 2 |
|
%% |
| 3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
|
%% you may not use this file except in compliance with the License. |
| 5 |
|
%% You may obtain a copy of the License at |
| 6 |
|
%% |
| 7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
%% |
| 9 |
|
%% Unless required by applicable law or agreed to in writing, software |
| 10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
|
%% See the License for the specific language governing permissions and |
| 13 |
|
%% limitations under the License. |
| 14 |
|
|
| 15 |
|
|
| 16 |
|
-module(pgmp_tsvector). |
| 17 |
|
|
| 18 |
|
-export([decode/1]). |
| 19 |
|
-export([encode/1]). |
| 20 |
|
|
| 21 |
|
|
| 22 |
|
decode(<<N:32, Encoded/bytes>>) -> |
| 23 |
:-( |
{<<>>, Decoded} = pgmp_binary:repeat( |
| 24 |
|
N, |
| 25 |
|
Encoded, |
| 26 |
|
fun words/2, |
| 27 |
|
#{}), |
| 28 |
:-( |
Decoded. |
| 29 |
|
|
| 30 |
|
words(WordPositions, A) -> |
| 31 |
:-( |
position(binary:split(WordPositions, <<0>>), A). |
| 32 |
|
|
| 33 |
|
position([Word, <<0:16, Remainder/bytes>>], A) -> |
| 34 |
:-( |
{Remainder, A#{Word => #{}}}; |
| 35 |
|
|
| 36 |
|
position([Word, <<NumPos:16, Encoded/bytes>>], A) -> |
| 37 |
:-( |
{Remainder, Positions} = pgmp_binary:repeat( |
| 38 |
|
NumPos, |
| 39 |
|
Encoded, |
| 40 |
|
fun position_weight/2, |
| 41 |
|
#{}), |
| 42 |
:-( |
{Remainder, A#{Word => Positions}}. |
| 43 |
|
|
| 44 |
|
position_weight(<<Weight:2, Position:14, Remainder/bytes>>, A) -> |
| 45 |
:-( |
{Remainder, A#{Position => weight(Weight)}}. |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
weight(0) -> |
| 49 |
:-( |
d; |
| 50 |
|
weight(1) -> |
| 51 |
:-( |
c; |
| 52 |
|
weight(2) -> |
| 53 |
:-( |
b; |
| 54 |
|
weight(3) -> |
| 55 |
:-( |
a; |
| 56 |
|
weight(a) -> |
| 57 |
:-( |
3; |
| 58 |
|
weight(b) -> |
| 59 |
:-( |
2; |
| 60 |
|
weight(c) -> |
| 61 |
:-( |
1; |
| 62 |
|
weight(d) -> |
| 63 |
:-( |
0. |
| 64 |
|
|
| 65 |
|
encode(Value) -> |
| 66 |
:-( |
[<<(map_size(Value)):32>>, |
| 67 |
|
|
| 68 |
|
lists:map( |
| 69 |
|
fun |
| 70 |
|
({Word, Positions}) -> |
| 71 |
:-( |
[<<Word/bytes, |
| 72 |
|
0:8, |
| 73 |
|
(map_size(Positions)):16>>, |
| 74 |
|
|
| 75 |
|
lists:map( |
| 76 |
|
fun |
| 77 |
|
({Position, Weight}) -> |
| 78 |
:-( |
<<(weight(Weight)):2, Position:14>> |
| 79 |
|
end, |
| 80 |
|
as_sorted_list(Positions))] |
| 81 |
|
end, |
| 82 |
|
as_sorted_list(Value))]. |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
as_sorted_list(M) -> |
| 86 |
:-( |
lists:sort(maps:to_list(M)). |