-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstring_helpers--1.0.sql
executable file
·79 lines (65 loc) · 2.45 KB
/
bitstring_helpers--1.0.sql
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
CREATE OR REPLACE FUNCTION shuffled_ints(bigint) RETURNS bigint[]
AS '$libdir/bitstring_helpers', 'shuffled_ints' LANGUAGE C STRICT;
COMMENT ON FUNCTION shuffled_ints(bigint) IS 'Create an array of user definable size ''n'' of shuffled integers (1 to n)';
CREATE OR REPLACE FUNCTION popcount(bit varying) RETURNS int
AS '$libdir/bitstring_helpers', 'popcount' LANGUAGE C STRICT IMMUTABLE;
COMMENT ON FUNCTION popcount(bit varying) IS 'Count the number of 1''s in a bitstring';
CREATE OR REPLACE FUNCTION hamming_distance(bit varying,bit varying) RETURNS int
AS $$
SELECT popcount($1 # $2);
$$ LANGUAGE 'SQL' STRICT IMMUTABLE;
COMMENT ON FUNCTION hamming_distance(bit varying, bit varying) IS 'Calculate the hamming distance between two bitstrings';
CREATE OR REPLACE FUNCTION neighbours(bit varying) RETURNS SETOF bit varying
AS $$
SELECT $1 # (pg_catalog.bit(1,length($1)) << generate_series(0,length($1)-1,1));
$$ LANGUAGE 'SQL' STRICT IMMUTABLE;
COMMENT ON FUNCTION neighbours(bit varying) IS 'Return all the 1-bit mutation neighbours of the given bitstring';
--casts
CREATE OR REPLACE FUNCTION text2bitstring(text) RETURNS bit varying
AS $$
SELECT bit_in(textout('B'::text||$1),67,length($1));
$$ LANGUAGE 'SQL';
COMMENT ON FUNCTION text2bitstring(text) IS 'Convert text (contianing 1''s and 0''s only) to a bit varying type.';
CREATE CAST (text AS bit varying) WITH FUNCTION text2bitstring (text);
CREATE OR REPLACE FUNCTION bit2text(bit varying) RETURNS text
AS $$
SELECT textin(bit_out($1));
$$ LANGUAGE 'SQL';
COMMENT ON FUNCTION bit2text(bit varying) IS 'Convert bits to text';
CREATE CAST (bit varying AS text) WITH FUNCTION bit2text(bit varying);
/*
CREATE OR REPLACE FUNCTION textarray2bitvaryingarray(text[]) RETURNS bit varying[]
AS $$
DECLARE
out bit varying[];
BEGIN
FOR i IN 1..array_upper($1, 1) LOOP
IF (i=1) THEN
out:=array[$1[i]::bit varying];
ELSE
out:=array_append(out,$1[i]::bit varying);
END IF;
END LOOP;
RETURN out;
END;
$$ LANGUAGE 'plpgsql';
--select textarray2bitvaryingarray(array['010011','0010101','010100101100']);
CREATE CAST (text[] AS bit varying[]) WITH FUNCTION textarray2bitvaryingarray(text[]);
*/
--aggregates
CREATE AGGREGATE aconcat (
BASETYPE = anyelement,
SFUNC = array_append,
STYPE = anyarray,
INITCOND = '{}'
);
CREATE AGGREGATE concat(
BASETYPE=text,
SFUNC=textcat,
STYPE=text
);
CREATE AGGREGATE concat(
BASETYPE=bit,
SFUNC=bitcat,
STYPE=bit varying
);