2 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
\r
3 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
4 of this software and associated documentation files (the "Software"), to deal
\r
5 in the Software without restriction, including without limitation the rights
\r
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
7 copies of the Software, and to permit persons to whom the Software is
\r
8 furnished to do so, subject to the following conditions:
\r
10 The above copyright notice and this permission notice shall be included in
\r
11 all copies or substantial portions of the Software.
\r
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
22 // C5 example: job queue 2004-11-22
\r
25 // csc /r:C5.dll Anagrams.cs
\r
29 using SCG = System.Collections.Generic;
\r
31 class MyJobQueueTest {
\r
32 public static void Main(String[] args) {
\r
33 JobQueue jq = new JobQueue();
\r
34 // One user submits three jobs at time=27
\r
35 Rid rid1 = jq.Submit(new Ip("62.150.83.11"), 27),
\r
36 rid2 = jq.Submit(new Ip("62.150.83.11"), 27),
\r
37 rid3 = jq.Submit(new Ip("62.150.83.11"), 27);
\r
38 // One job is executed
\r
40 // Another user submits two jobs at time=55
\r
41 Rid rid4 = jq.Submit(new Ip("130.225.17.5"), 55),
\r
42 rid5 = jq.Submit(new Ip("130.225.17.5"), 55);
\r
43 // One more job is executed
\r
45 // The first user tries to cancel his first and last job
\r
48 // The remaining jobs are executed
\r
49 while (jq.ExecuteOne() != null) { }
\r
54 private readonly IPriorityQueue<Job> jobQueue;
\r
55 private readonly IDictionary<Rid,IPriorityQueueHandle<Job>> jobs;
\r
56 private readonly HashBag<Ip> userJobs;
\r
59 this.jobQueue = new IntervalHeap<Job>();
\r
60 this.jobs = new HashDictionary<Rid,IPriorityQueueHandle<Job>>();
\r
61 this.userJobs = new HashBag<Ip>();
\r
64 public Rid Submit(Ip ip, int time) {
\r
65 int jobCount = userJobs.ContainsCount(ip);
\r
66 Rid rid = new Rid();
\r
67 Job job = new Job(rid, ip, time + 60 * jobCount);
\r
68 IPriorityQueueHandle<Job> h = null;
\r
69 jobQueue.Add(ref h, job);
\r
72 Console.WriteLine("Submitted {0}", job);
\r
76 public Job ExecuteOne() {
\r
77 if (!jobQueue.IsEmpty) {
\r
78 Job job = jobQueue.DeleteMin();
\r
79 userJobs.Remove(job.ip);
\r
80 jobs.Remove(job.rid);
\r
81 Console.WriteLine("Executed {0}", job);
\r
87 public void Cancel(Rid rid) {
\r
88 IPriorityQueueHandle<Job> h;
\r
89 if (jobs.Remove(rid, out h)) {
\r
90 Job job = jobQueue.Delete(h);
\r
91 userJobs.Remove(job.ip);
\r
92 Console.WriteLine("Cancelled {0}", job);
\r
97 class Job : IComparable<Job> {
\r
98 public readonly Rid rid;
\r
99 public readonly Ip ip;
\r
100 public readonly int time;
\r
102 public Job(Rid rid, Ip ip, int time) {
\r
108 public int CompareTo(Job that) {
\r
109 return this.time - that.time;
\r
112 public override String ToString() {
\r
113 return rid.ToString();
\r
118 private readonly int ridNumber;
\r
119 private static int nextRid = 1;
\r
121 ridNumber = nextRid++;
\r
123 public override String ToString() {
\r
124 return "rid=" + ridNumber;
\r
129 public readonly String ipString;
\r
131 public Ip(String ipString) {
\r
132 this.ipString = ipString;
\r
135 public override int GetHashCode() {
\r
136 return ipString.GetHashCode();
\r
139 public override bool Equals(Object that) {
\r
143 && this.ipString.Equals(((Ip)that).ipString);
\r