From 670b311fa2c60c10878d7ca7984392b0d2b0ef03 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sun, 19 Apr 2020 20:36:19 +1000 Subject: Misc --- src/packrat-graphs.adb | 99 +++++++++++++++++++++++++++++++------------------- src/packrat-graphs.ads | 70 +++++++++++++++++++++-------------- 2 files changed, 104 insertions(+), 65 deletions(-) diff --git a/src/packrat-graphs.adb b/src/packrat-graphs.adb index f32c628..10e130f 100644 --- a/src/packrat-graphs.adb +++ b/src/packrat-graphs.adb @@ -1012,55 +1012,61 @@ package body Packrat.Graphs is - function Iterate - (This : in Graph) - return Graph_Iterators.Reversible_Iterator'Class is - begin - return Result : Reversible_Iterator do - Result.Position := No_Position; - end return; - end Iterate; - - - function Iterate_Subtree - (This : in Graph; - Position : in Cursor) - return Graph_Iterators.Reversible_Iterator'Class is + function Default_Choices + (Container : in Graph; + Position : in Cursor) + return Natural is begin - return Result : Reversible_Iterator do - Result.Position := No_Position; - end return; - end Iterate_Subtree; + if Is_Nothing (Position) then + return Container.Root_Count; + else + return Choices (Position); + end if; + end Default_Choices; - function Iterate_Choice - (This : in Graph; - Func : in Choosing_Function) - return Graph_Iterators.Forward_Iterator'Class is + function Accept_All + (Position : in Cursor) + return Boolean is begin - return Result : Forward_Iterator do - Result.Position := No_Position; - end return; - end Iterate_Choice; + return not Is_Nothing (Position); + end Accept_All; - function First - (Object : in Forward_Iterator) - return Cursor is + function Iterate + (Container : in Graph; + Start_At : in Cursor := No_Position; + Choose : in Choosing_Function := Default_Choices'Access; + Filter : in Filter_Function := Accept_All'Access) + return Graph_Iterators.Reversible_Iterator'Class is begin - return No_Position; - end First; + return This : Reversible_Iterator do + This.My_Graph := Container'Unrestricted_Access; + This.Start_Pos := Start_At; + This.Rule := Specific_Branch; + This.Choose_Func := Choose; + This.Filter_Func := Filter; + end return; + end Iterate; - function Next - (Object : in Forward_Iterator; - Place : in Cursor) - return Cursor is + + function Iterate_All + (Container : in Graph; + Start_At : in Cursor := No_Position; + Filter : in Filter_Function := Accept_All'Access) + return Graph_Iterators.Reversible_Iterator'Class is begin - return No_Position; - end Next; + return This : Reversible_Iterator do + This.My_Graph := Container'Unrestricted_Access; + This.Start_Pos := Start_At; + This.Rule := All_Nodes; + This.Choose_Func := null; + This.Filter_Func := Filter; + end return; + end Iterate_All; @@ -1070,7 +1076,18 @@ package body Packrat.Graphs is (Object : in Reversible_Iterator) return Cursor is begin - return No_Position; + if Object.My_Graph = null or else Object.My_Graph.all.Is_Empty then + return No_Position; + elsif Is_Nothing (Object.Start_Pos) then + if Object.Rule = All_Nodes then + return Object.My_Graph.all.Root (1); + else + return Object.My_Graph.all.Root + (Object.Choose_Func (Object.My_Graph.all, No_Position)); + end if; + else + return Object.Start_Pos; + end if; end First; @@ -1079,6 +1096,12 @@ package body Packrat.Graphs is Place : in Cursor) return Cursor is begin + if Object.My_Graph = null or else + Object.My_Graph.all.Is_Empty or else + Is_Nothing (Place) + then + return No_Position; + end if; -- elsif return No_Position; end Next; diff --git a/src/packrat-graphs.ads b/src/packrat-graphs.ads index 5074485..6bceaaf 100644 --- a/src/packrat-graphs.ads +++ b/src/packrat-graphs.ads @@ -387,26 +387,52 @@ package Packrat.Graphs is package Graph_Iterators is new Ada.Iterator_Interfaces (Cursor, Is_Node); + -- Note that if the given Cursor doesn't point to any position, + -- then the function should assume that the choice is of what root to + -- select and return that value. type Choosing_Function is access function + (Container : in Graph; + Position : in Cursor) + return Natural; + + -- This function returns True if a Node pointed to by a Cursor should + -- be returned, and False if it should be ignored. An example of a + -- filter that will probably see a decent amount of use would be Is_Leaf. + type Filter_Function is access function (Position : in Cursor) + return Boolean; + + + + + function Default_Choices + (Container : in Graph; + Position : in Cursor) return Natural; + function Accept_All + (Position : in Cursor) + return Boolean; - function Iterate - (This : in Graph) - return Graph_Iterators.Reversible_Iterator'Class; - function Iterate_Subtree - (This : in Graph; - Position : in Cursor) - return Graph_Iterators.Reversible_Iterator'Class; + function Iterate + (Container : in Graph; + Start_At : in Cursor := No_Position; + Choose : in Choosing_Function := Default_Choices'Access; + Filter : in Filter_Function := Accept_All'Access) + return Graph_Iterators.Reversible_Iterator'Class + with Pre => + Container.Contains (Start_At) or Is_Nothing (Start_At); - function Iterate_Choice - (This : in Graph; - Func : in Choosing_Function) - return Graph_Iterators.Forward_Iterator'Class; + function Iterate_All + (Container : in Graph; + Start_At : in Cursor := No_Position; + Filter : in Filter_Function := Accept_All'Access) + return Graph_Iterators.Reversible_Iterator'Class + with Pre => + Container.Contains (Start_At) or Is_Nothing (Start_At); @@ -597,24 +623,14 @@ private - type Forward_Iterator is new Graph_Iterators.Forward_Iterator with record - Position : Cursor; - end record; - - overriding function First - (Object : in Forward_Iterator) - return Cursor; - - overriding function Next - (Object : in Forward_Iterator; - Place : in Cursor) - return Cursor; - - - + type Iterate_Kind is (Specific_Branch, All_Nodes); type Reversible_Iterator is new Graph_Iterators.Reversible_Iterator with record - Position : Cursor; + My_Graph : access Graph; + Start_Pos : Cursor; + Rule : Iterate_Kind; + Choose_Func : Choosing_Function; + Filter_Func : Filter_Function; end record; overriding function First -- cgit