Priority queue: Difference between revisions
m
→{{header|Zig}}
imported>Pjfarley3 (Moved to correct location by language name) |
Nima.trueway (talk | contribs) m (→{{header|Zig}}) |
||
(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
Note also that the subroutines call each other using "PIC X(8)"
<syntaxhighlight lang="COBOL">
PROCESS NOSEQ,DS(S),AR(E),TEST(SO),CP(1047)
IDENTIFICATION DIVISION.
PROGRAM-ID. PTYQTEST
Line 2,444:
10 TASK-DOWN POINTER.
05 TASK-NAME PIC X(40).
PROCEDURE DIVISION.
Line 2,502 ⟶ 2,492:
GOBACK.
END PROGRAM PTYQTEST.
PROCESS NOSEQ,DS(S),AR(E),TEST(SO),CP(1047)
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)
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)
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)
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)
return std.math.order(a.priority
}
fn minCompare(_: void, a: Task, b: Task)
return std.math.order(a.priority
}
};
Line 8,997 ⟶ 8,987:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var pq = PriorityQueue(Task, void, Comparator.maxCompare).init(allocator,
▲ 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:
}
std.debug.print("\n", .{});
Line 9,022 ⟶ 9,011:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var pq = PriorityQueue(Task, void, Comparator.minCompare).init(allocator,
▲ 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:
}
std.debug.print("\n", .{});
}
</syntaxhighlight>
|