Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting to Alire projects #1

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .gitignore

This file was deleted.

32 changes: 0 additions & 32 deletions adalog_comps.gpr

This file was deleted.

4 changes: 4 additions & 0 deletions binary_map/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/obj/
/lib/
/alire/
/config/
10 changes: 10 additions & 0 deletions binary_map/alire.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name = "binary_map"
description = "A map data structure stored in binary tree format"
version = "0.1.0-dev"

authors = ["Jean-Pierre Rosen"]
maintainers = ["Jean-Pierre Rosen <[email protected]>"]
maintainers-logins = ["jprosen"]
licenses = "GPL-2.0-or-later"
website = "https://adalog.fr/en/components.html"
tags = ["map", "binary-tree", "key-value", "dictionary", "data-structure"]
29 changes: 29 additions & 0 deletions binary_map/binary_map.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
with "config/binary_map_config.gpr";
project Binary_Map is

for Library_Name use "Binary_Map";
for Library_Version use Project'Library_Name & ".so." & Binary_Map_Config.Crate_Version;

for Source_Dirs use ("src/", "config/");
for Object_Dir use "obj/" & Binary_Map_Config.Build_Profile;
for Create_Missing_Dirs use "True";
for Library_Dir use "lib";

type Library_Type_Type is ("relocatable", "static", "static-pic");
Library_Type : Library_Type_Type :=
external ("BINARY_MAP_LIBRARY_TYPE", external ("LIBRARY_TYPE", "static"));
for Library_Kind use Library_Type;

package Compiler is
for Default_Switches ("Ada") use Binary_Map_Config.Ada_Compiler_Switches;
end Compiler;

package Binder is
for Switches ("Ada") use ("-Es"); -- Symbolic traceback
end Binder;

package Install is
for Artifacts (".") use ("share");
end Install;

end Binary_Map;
40 changes: 20 additions & 20 deletions binary_map/binary_map-put.adb → binary_map/src/binary_map-put.adb
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
procedure Binary_Map.Put (The_Map : Map; Indent : Text_IO.Count := 1) is
use Text_IO;
Pos : Count;
begin
Set_Col (Indent);
if The_Map = null then
Put ("()");
return;
end if;
Put ("(");
Put (Image (The_Map.Key));
Pos := Col+1;
Put (The_Map.Children (Before), Pos);
Put (The_Map.Children (After), Pos);
Set_Col (Indent);
Put (')');
New_Line;
end Binary_Map.Put;
procedure Binary_Map.Put (The_Map : Map; Indent : Text_IO.Count := 1) is
use Text_IO;
Pos : Count;
begin
Set_Col (Indent);
if The_Map = null then
Put ("()");
return;
end if;

Put ("(");
Put (Image (The_Map.Key));
Pos := Col + 1;
Put (The_Map.Children (Before), Pos);
Put (The_Map.Children (After), Pos);
Set_Col (Indent);
Put (')');
New_Line;

end Binary_Map.Put;
8 changes: 4 additions & 4 deletions binary_map/binary_map-put.ads → binary_map/src/binary_map-put.ads
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with Text_IO;
generic
with function Image (X : Key_Type) return String;
procedure Binary_Map.Put (The_Map : Map; Indent : Text_IO.Count := 1);
with Text_IO;
generic
with function Image (X : Key_Type) return String;
procedure Binary_Map.Put (The_Map : Map; Indent : Text_IO.Count := 1);
82 changes: 46 additions & 36 deletions binary_map/binary_map.adb → binary_map/src/binary_map.adb
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ package body Binary_Map is
procedure Free is new Ada.Unchecked_Deallocation (Node, Map);

--
-- Internal utilities
-- Internal utilities
--

--------------
Expand All @@ -49,7 +49,7 @@ package body Binary_Map is
begin
loop
if Current = null then
-- Not found
-- Not found
return null;

elsif Key < Current.Key then
Expand All @@ -59,7 +59,7 @@ package body Binary_Map is
Current := Current.Children (After);

else
-- Found
-- Found
return Current;
end if;
end loop;
Expand All @@ -70,10 +70,11 @@ package body Binary_Map is
---------------

procedure Linearize (M : Map; First, Last : out Map; Count : out Natural) is
-- Precondition: M is not null
-- Postconditions: First is the first element of a linear tree (all Left pointers are null)
-- Last is the last element
-- Count is the number of elements in the tree
-- Precondition: M is not null
-- Postconditions: First is the first element of a linear tree (all Left
-- pointers are null)
-- Last is the last element
-- Count is the number of elements in the tree

Temp_Map : Map;
Temp_Count : Natural;
Expand All @@ -97,14 +98,15 @@ package body Binary_Map is
end if;
end Linearize;

--------------
--Rebalance --
--------------
---------------
-- Rebalance --
---------------

procedure Rebalance (M : in out Map; Size : Natural; Rest : out Map) is
-- Precondition: M is a linear tree (all Before pointers are null)
-- Postcondions: M is a balanced tree containing the first Size elements
-- Rest is the first of the remaining elements from the linear tree
-- Precondition: M is a linear tree (all Before pointers are null)
-- Postcondions: M is a balanced tree containing the first Size elements
-- Rest is the first of the remaining elements from the
-- linear tree
Top : Map;
Left : Map;
begin
Expand All @@ -119,25 +121,24 @@ package body Binary_Map is

when others =>
Left := M;
Rebalance (Left, (Size-1) / 2, Top);
Rebalance (Left, (Size - 1) / 2, Top);
Top.Children (Before) := Left;
Rebalance (Top.Children (After), Size - (Size-1)/2 - 1, Rest);
Rebalance (Top.Children (After), Size - (Size - 1) / 2 - 1, Rest);
M := Top;
end case;
end Rebalance;


--
-- Exported subprograms
-- Exported subprograms
--

---------
-- Add --
---------

procedure Add (To : in out Map;
Key : in Key_Type;
Value : in Value_Type)
Key : Key_Type;
Value : Value_Type)
is
begin
if To = null then
Expand All @@ -156,7 +157,7 @@ package body Binary_Map is

-------------
-- Balance --
------------
-------------

procedure Balance (The_Map : in out Map) is
First, Last : Map;
Expand All @@ -176,7 +177,7 @@ package body Binary_Map is
------------

procedure Delete (From : in out Map; Key : Key_Type) is
Count1, Count2: Natural;
Count1, Count2 : Natural;
Last : Map;
Parent : Map := null;
Slot : Slots;
Expand All @@ -185,7 +186,7 @@ package body Binary_Map is
begin
loop
if Cur_Node = null then
-- Not found
-- Not found
raise Not_Present;

elsif Key > Cur_Node.Key then
Expand All @@ -195,7 +196,7 @@ package body Binary_Map is
Slot := Before;

else
-- Found
-- Found
exit;
end if;
Parent := Cur_Node;
Expand All @@ -213,10 +214,17 @@ package body Binary_Map is
Result := Cur_Node.Children (Before);

else
-- At this point, deleting the node involves walking down the tree.
-- it is not much more effort to rebalance (and actually simpler to program)
Linearize (Cur_Node.Children (Before), Result, Last, Count1);
Linearize (Cur_Node.Children (After), Last.Children (After), Last, Count2);
-- At this point, deleting the node involves walking down the tree.
-- it is not much more effort to rebalance (and actually simpler to
-- program)
Linearize (Cur_Node.Children (Before),
Result,
Last,
Count1);
Linearize (Cur_Node.Children (After),
Last.Children (After),
Last,
Count2);
Rebalance (Result, Count1 + Count2, Last);
end if;

Expand Down Expand Up @@ -246,7 +254,10 @@ package body Binary_Map is
-- Fetch --
-----------

function Fetch (From : Map; Key : Key_Type; Default_Value : Value_Type) return Value_Type is
function Fetch (From : Map;
Key : Key_Type;
Default_Value : Value_Type)
return Value_Type is
Cur_Node : constant Map := Get_Node (From, Key);
begin
if Cur_Node = null then
Expand Down Expand Up @@ -276,18 +287,18 @@ package body Binary_Map is
return;
end if;

Iterate(On.Children (Before));
Iterate (On.Children (Before));
begin
Action(On.Key, On.Value);
Action (On.Key, On.Value);
exception
when Delete_Current =>
Delete_Node := True;
end;
Iterate(On.Children (After));
Iterate (On.Children (After));

-- Deleting the node *after* traversing On.Children (After)
-- makes sure that there is no problem with the tree being
-- rearranged due to delete.
-- Deleting the node *after* traversing On.Children (After)
-- makes sure that there is no problem with the tree being
-- rearranged due to delete.
if Delete_Node then
Delete (On, On.Key);
end if;
Expand Down Expand Up @@ -328,10 +339,9 @@ package body Binary_Map is
-- Is_Empty --
--------------

function Is_Empty (The_Map : in Map) return Boolean is
function Is_Empty (The_Map : Map) return Boolean is
begin
return The_Map = Empty_Map;
end Is_Empty;

end Binary_Map;

Loading