Created
January 15, 2016 11:39
-
-
Save noktoborus/6d4a18595af925643167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* vim: syntax=pgsql | |
*/ | |
DROP TABLE IF EXISTS x; | |
CREATE TABLE IF NOT EXISTS x ( | |
n integer NOT NULL, | |
"offset" integer NOT NULL, | |
"size" integer NOT NULL | |
); | |
COPY x (n, "offset", "size") FROM stdin; | |
1 0 10 | |
1 10 10 | |
1 20 10 | |
1 25 15 | |
1 40 10 | |
2 0 10 | |
2 10 10 | |
2 20 10 | |
2 0 40 | |
2 40 10 | |
3 0 10 | |
3 10 10 | |
3 20 10 | |
3 40 10 | |
\. | |
CREATE OR REPLACE FUNCTION is_full(_n integer, _fullsize integer) | |
RETURNS boolean AS $$ | |
DECLARE | |
_row record; | |
e_offset integer DEFAULT 0; | |
e_size integer DEFAULT 0; | |
BEGIN | |
FOR _row IN SELECT "offset", "size" FROM x WHERE x.n = _n ORDER BY "offset" | |
LOOP | |
-- выходим сразу, если обнаружили пропуск | |
IF _row."offset" > e_offset THEN | |
return false; | |
END IF; | |
-- наложение | |
IF _row."offset" < e_offset THEN | |
-- если не избыточное наложение | |
IF _row."offset" + _row."size" > e_offset THEN | |
e_offset := _row."offset" + _row."size"; | |
END IF; | |
ELSE | |
e_offset := e_offset + _row.size; | |
END IF; | |
--RAISE NOTICE '%, % -> %', _n ,_row, e_offset; | |
END LOOP; | |
return (e_offset = _fullsize); | |
END | |
$$ LANGUAGE plpgsql; | |
SELECT n, is_full(n, f), expect | |
FROM (VALUES(1, 50, True), (2, 50, True), (3, 50, False)) AS r(n, f, expect); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment