Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.

Commit 48237e2

Browse files
committed
initialize queue with default null element
1 parent 98ecf3a commit 48237e2

File tree

4 files changed

+30
-42
lines changed

4 files changed

+30
-42
lines changed

src/basalt-queue.adb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,9 @@ is
2020
function Put_Index (C : Context) return Positive is
2121
(Positive ((C.Index + C.Length - 1) mod C.List'Length + Long_Positive (C.List'First)));
2222

23-
procedure Initialize (C : out Context;
24-
Null_Element : T)
23+
procedure Initialize (C : in out Context)
2524
is
2625
begin
27-
-- This would be the correct way to initialize S.List:
28-
-- C.List := (others => Null_Element);
29-
-- As this creates a (potentially large) object on the stack, we initialize in a loop. The resulting flow
30-
-- error is justified in the spec.
31-
for E of C.List loop
32-
E := Null_Element;
33-
end loop;
3426
C.Length := Long_Natural'First;
3527
C.Index := Long_Natural'First;
3628
end Initialize;
@@ -39,28 +31,28 @@ is
3931
Element : T)
4032
is
4133
begin
42-
C.Length := C.Length + 1;
43-
C.List (Put_Index (C)) := Element;
34+
C.Length := C.Length + 1;
35+
C.List (Put_Index (C)).Value := Element;
4436
end Put;
4537

4638
procedure Generic_Put (C : in out Context)
4739
is
4840
begin
49-
C.Length := C.Length + 1;
50-
Put (C.List (Put_Index (C)));
41+
C.Length := C.Length + 1;
42+
Put (C.List (Put_Index (C)).Value);
5143
end Generic_Put;
5244

5345
procedure Peek (C : Context;
5446
Element : out T)
5547
is
5648
begin
57-
Element := C.List (Positive (C.Index + Long_Positive (C.List'First)));
49+
Element := C.List (Positive (C.Index + Long_Positive (C.List'First))).Value;
5850
end Peek;
5951

6052
procedure Generic_Peek (C : Context)
6153
is
6254
begin
63-
Peek (C.List (Positive (C.Index + Long_Positive (C.List'First))));
55+
Peek (C.List (Positive (C.Index + Long_Positive (C.List'First))).Value);
6456
end Generic_Peek;
6557

6658
procedure Drop (C : in out Context)

src/basalt-queue.ads

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
generic
1616
type T is private;
17+
Null_Element : T;
1718
package Basalt.Queue with
1819
SPARK_Mode,
1920
Pure,
@@ -41,20 +42,11 @@ is
4142
-- @return Number of elements currently in the queue
4243
function Count (C : Context) return Natural;
4344

44-
-- Initializes the queue with a default element value
45+
-- Initializes the queue
4546
--
46-
-- This procedure is only needed to proof the data flow. It is
47-
-- the only way to assign a Queue object. The object will automatically
48-
-- be initialized with the given element. The initialized queue will be empty.
49-
--
50-
-- @param C Queue context
51-
-- @param Null_Element Default element to initialize the queue with
52-
procedure Initialize (C : out Context;
53-
Null_Element : T) with
47+
-- @param C Queue context
48+
procedure Initialize (C : in out Context) with
5449
Post => Count (C) = 0;
55-
pragma Annotate (GNATprove, False_Positive,
56-
"""C.List"" might not be initialized*",
57-
"Initialized in complete loop");
5850

5951
-- Puts an element in the queue.
6052
--
@@ -115,7 +107,11 @@ is
115107

116108
private
117109

118-
type Simple_List is array (Natural range <>) of T;
110+
type List_Element is record
111+
Value : T := Null_Element;
112+
end record;
113+
114+
type Simple_List is array (Natural range <>) of List_Element;
119115
subtype Long_Natural is Long_Integer range 0 .. Long_Integer'Last;
120116
subtype Long_Positive is Long_Integer range 1 .. Long_Integer'Last;
121117

test/aunit/queue_tests.adb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ with Basalt.Queue;
44

55
package body Queue_Tests
66
is
7-
package F is new Basalt.Queue (Integer);
7+
package F is new Basalt.Queue (Integer, 0);
88

99
procedure Test_Fifo (T : in out Aunit.Test_Cases.Test_Case'Class)
1010
is
1111
pragma Unreferenced (T);
1212
Q : F.Context (100);
1313
J : Integer;
1414
begin
15-
F.Initialize (Q, 0);
15+
F.Initialize (Q);
1616
for I in 70 .. 120 loop
1717
F.Put (Q, I);
1818
end loop;
@@ -27,7 +27,7 @@ is
2727
pragma Unreferenced (T);
2828
Q : F.Context (2);
2929
begin
30-
F.Initialize (Q, 0);
30+
F.Initialize (Q);
3131
Aunit.Assertions.Assert (F.Count (Q) = 0, "Count not 0");
3232
F.Put (Q, 1);
3333
Aunit.Assertions.Assert (F.Count (Q) = 1, "Count not 1 after Put");
@@ -45,7 +45,7 @@ is
4545
Q : F.Context (1);
4646
J : Integer;
4747
begin
48-
F.Initialize (Q, 0);
48+
F.Initialize (Q);
4949
Aunit.Assertions.Assert (F.Count (Q) = 0, "Queue not empty");
5050
F.Put (Q, 1);
5151
Aunit.Assertions.Assert (F.Count (Q) = 1, "Queue not full");
@@ -60,7 +60,7 @@ is
6060
pragma Unreferenced (T);
6161
Q : F.Context (100);
6262
begin
63-
F.Initialize (Q, 0);
63+
F.Initialize (Q);
6464
Aunit.Assertions.Assert (F.Count (Q) = 0, "Count should be 0");
6565
for I in Integer range 1 .. 20 loop
6666
F.Put (Q, I);
@@ -90,7 +90,7 @@ is
9090
Q : F.Context (1);
9191
Unused : Integer;
9292
begin
93-
F.Initialize (Q, 0);
93+
F.Initialize (Q);
9494
Aunit.Assertions.Assert (F.Count (Q) = 0, "Count should be 0");
9595
F.Put (Q, 1);
9696
Aunit.Assertions.Assert (F.Count (Q) = 1, "Count should be 1");
@@ -118,7 +118,7 @@ is
118118
Aunit.Assertions.Assert (I = 42, "I should be 42");
119119
end Peek;
120120
begin
121-
F.Initialize (Q, 0);
121+
F.Initialize (Q);
122122
Aunit.Assertions.Assert (F.Count (Q) = 0, "Count should be 0");
123123
Put (Q);
124124
Aunit.Assertions.Assert (F.Count (Q) = 1, "Count should be 1");
@@ -136,10 +136,10 @@ is
136136
Q3 : F.Context (200);
137137
Q4 : F.Context (13000);
138138
begin
139-
F.Initialize (Q1, 0);
140-
F.Initialize (Q2, 0);
141-
F.Initialize (Q3, 0);
142-
F.Initialize (Q4, 0);
139+
F.Initialize (Q1);
140+
F.Initialize (Q2);
141+
F.Initialize (Q3);
142+
F.Initialize (Q4);
143143
Aunit.Assertions.Assert (F.Size (Q1) = 1, "Size of Q1 should be 1");
144144
Aunit.Assertions.Assert (F.Size (Q2) = 50, "Size of Q2 should be 50");
145145
Aunit.Assertions.Assert (F.Size (Q3) = 200, "Size of Q3 should be 200");
@@ -152,7 +152,7 @@ is
152152
Q : F.Context (10);
153153
J : Integer;
154154
begin
155-
F.Initialize (Q, 0);
155+
F.Initialize (Q);
156156
for I in Integer range 1 .. 7 loop
157157
F.Put (Q, I);
158158
end loop;

test/proof/proof_queue.adb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package body Proof_Queue with
55
SPARK_Mode
66
is
77

8-
package Fifo is new Basalt.Queue (Integer);
8+
package Fifo is new Basalt.Queue (Integer, 0);
99
Queue : Fifo.Context (10);
1010

1111
procedure Prove
@@ -27,7 +27,7 @@ is
2727
J_Ignore := I;
2828
end Peek;
2929
begin
30-
Fifo.Initialize (Queue, 0);
30+
Fifo.Initialize (Queue);
3131
for I in Integer range 7 .. 13 loop
3232
Fifo.Put (Queue, I);
3333
exit when Fifo.Count (Queue) >= Fifo.Size (Queue);

0 commit comments

Comments
 (0)