Priority queue: Difference between revisions

m
imported>Pjfarley3
(Moved to correct location by language name)
 
(5 intermediate revisions by one other user not shown)
Line 2,401:
Note also that each subroutine is declared RECURSIVE though they do not all need it.
 
The subroutines each pass back a return value in their last parameter. The most recent release of the IBM Enterprise COBOL compiler (V6.4 as of the date of this contribution) does, in fact, support user-defined functions, which would make some of this implementation a little easier to write and read, but since many IBM shops are not yet up to the most recent level, this version is offered as one that will work with down-level compiler versions.
 
In the "two pass merge" subroutine (PTYQ2PMG), the final three lines are needed because withoutthe usingCOBOL "userCALL definedstatement functions"does not allow for expressions as arguments, so the arguments to the outer call to the "merge" subroutine must be executed first, and the results of those two calls become the arguments to the final "merge" call.
 
Note also that the subroutines call each other using "PIC X(8)" pseudonym'spseudonyms because using the actually recursive subroutines cannot ustuse the "same name" as both the PROGRAM-ID and as a variable name. This could be resolved by simply using "constant" calls (like ```<code>CALL "PTYQ2PMG" usingUSING . . . ```</code> but using the pseudonym'spseudonyms allows each of the subroutines to also be separately compiled into an executable module and then dynamically loaded at run time. Many IBM shops will prefer that method to this purely "static" solution.
 
<syntaxhighlight lang="COBOL">
PROCESS NOSEQ,DS(S),AR(E),TEST(SO),CP(1047),DYNAM
IDENTIFICATION DIVISION.
PROGRAM-ID. PTYQTEST
Line 2,444:
10 TASK-DOWN POINTER.
05 TASK-NAME PIC X(40).
 
01 HEAP.
05 HEAP-KEY PIC S9(8) COMP-5.
05 HEAP-NEXT POINTER.
05 HEAP-DOWN POINTER.
 
01 NODE.
05 NODE-KEY PIC S9(8) COMP-5.
05 NODE-NEXT POINTER.
05 NODE-DOWN POINTER.
 
PROCEDURE DIVISION.
Line 2,502 ⟶ 2,492:
GOBACK.
END PROGRAM PTYQTEST.
PROCESS NOSEQ,DS(S),AR(E),TEST(SO),CP(1047),DYNAM
IDENTIFICATION DIVISION.
PROGRAM-ID. PTYQMERG RECURSIVE.
Line 2,557 ⟶ 2,547:
GOBACK.
END PROGRAM PTYQMERG.
PROCESS NOSEQ,DS(S),AR(E),TEST(SO),CP(1047),DYNAM
IDENTIFICATION DIVISION.
PROGRAM-ID. PTYQ2PMG RECURSIVE.
Line 2,631 ⟶ 2,621:
GOBACK.
END PROGRAM PTYQ2PMG.
PROCESS NOSEQ,DS(S),AR(E),TEST(SO),CP(1047),DYNAM
IDENTIFICATION DIVISION.
PROGRAM-ID. PTYQPUSH RECURSIVE.
Line 2,674 ⟶ 2,664:
GOBACK.
END PROGRAM PTY2PUSH.
PROCESS NOSEQ,DS(S),AR(E),TEST(SO),CP(1047),DYNAM
IDENTIFICATION DIVISION.
PROGRAM-ID. PTYQPOP RECURSIVE.
Line 8,985 ⟶ 8,975:
/// fn(T, T) bool
const Comparator = struct {
fn maxCompare(_: void, a: Task, b: Task) boolstd.math.Order {
return std.math.order(a.priority >, b.priority);
}
 
fn minCompare(_: void, a: Task, b: Task) boolstd.math.Order {
return std.math.order(a.priority <, b.priority);
}
};
Line 8,997 ⟶ 8,987:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
varconst allocator = &arena.allocator();
var pq = PriorityQueue(Task, void, Comparator.maxCompare).init(allocator, Comparator.maxCompare{});
 
var pq = PriorityQueue(Task).init(allocator, Comparator.maxCompare);
defer pq.deinit();
 
Line 9,007 ⟶ 8,996:
try pq.add(Task.init(1, "Solve RC tasks"));
try pq.add(Task.init(2, "Tax returns"));
try testing.expectEqual(pq.count(), 5);
 
std.debug.print("\n", .{});
Line 9,014 ⟶ 9,003:
while (pq.count() != 0) {
const task = pq.remove();
std.debug.print("Executing: {s} (priority {d})\n", .{ task.name, task.priority });
}
std.debug.print("\n", .{});
Line 9,022 ⟶ 9,011:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
varconst allocator = &arena.allocator();
var pq = PriorityQueue(Task, void, Comparator.minCompare).init(allocator, Comparator.minCompare{});
 
var pq = PriorityQueue(Task).init(allocator, Comparator.minCompare);
defer pq.deinit();
 
Line 9,032 ⟶ 9,020:
try pq.add(Task.init(1, "Solve RC tasks"));
try pq.add(Task.init(2, "Tax returns"));
try testing.expectEqual(pq.count(), 5);
 
std.debug.print("\n", .{});
Line 9,039 ⟶ 9,027:
while (pq.count() != 0) {
const task = pq.remove();
std.debug.print("Executing: {s} (priority {d})\n", .{ task.name, task.priority });
}
std.debug.print("\n", .{});
}
 
</syntaxhighlight>