Skip to content

Instantly share code, notes, and snippets.

@vituscze
Created May 21, 2024 01:31
Show Gist options
  • Save vituscze/ea93693a79bce5a05beaac9f9da5ae98 to your computer and use it in GitHub Desktop.
Save vituscze/ea93693a79bce5a05beaac9f9da5ae98 to your computer and use it in GitHub Desktop.
firstFit(Start, Len, [], Start, [Start-Len]).
firstFit(Start, Len, [Alloc-AllocLen|Rest], Pos, NewTaken) :-
Start + Len =< Alloc -> Pos = Start, NewTaken = [Start-Len,Alloc-AllocLen|Rest];
NewStart is Alloc + AllocLen, firstFit(NewStart, Len, Rest, Pos, RestTaken), NewTaken = [Alloc-AllocLen|RestTaken].
merge([], []).
merge([X], [X]).
merge([A-AL, B-BL|Rest], New) :-
B is A + AL -> L is AL + BL, merge([A-L|Rest], New);
merge([B-BL|Rest], RestNew), New = [A-AL|RestNew].
alokace([], Taken, [], Taken).
alokace([A|As], Taken, [A-Pos|PosRest], NewTaken) :-
firstFit(0, A, Taken, Pos, Unmerged),
merge(Unmerged, Merged),
alokace(As, Merged, PosRest, NewTaken).
attr(X-_, X).
getAttr(Attr, DBLine, Val) :- member(Attr-Val, DBLine) -> true; Val = undef.
getAttrAll(DB, Attr, Attr-Vals) :- maplist(getAttr(Attr), DB, ValsDup), sort(ValsDup, Vals).
extrakce(Database, Final) :-
append(Database, Flat),
maplist(attr, Flat, AttrsDup),
sort(AttrsDup, Attrs),
maplist(getAttrAll(Database), Attrs, Final).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment