-/*\r
- Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
- Permission is hereby granted, free of charge, to any person obtaining a copy\r
- of this software and associated documentation files (the "Software"), to deal\r
- in the Software without restriction, including without limitation the rights\r
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
- copies of the Software, and to permit persons to whom the Software is\r
- furnished to do so, subject to the following conditions:\r
- \r
- The above copyright notice and this permission notice shall be included in\r
- all copies or substantial portions of the Software.\r
- \r
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
- SOFTWARE.\r
-*/\r
-\r
-// C5 example: job queue 2004-11-22\r
-\r
-// Compile with \r
-// csc /r:C5.dll Anagrams.cs \r
-\r
-using System;\r
-using C5;\r
-using SCG = System.Collections.Generic;\r
-\r
-class MyJobQueueTest {\r
- public static void Main(String[] args) {\r
- JobQueue jq = new JobQueue();\r
- // One user submits three jobs at time=27\r
- Rid rid1 = jq.Submit(new Ip("62.150.83.11"), 27),\r
- rid2 = jq.Submit(new Ip("62.150.83.11"), 27),\r
- rid3 = jq.Submit(new Ip("62.150.83.11"), 27);\r
- // One job is executed\r
- jq.ExecuteOne();\r
- // Another user submits two jobs at time=55\r
- Rid rid4 = jq.Submit(new Ip("130.225.17.5"), 55),\r
- rid5 = jq.Submit(new Ip("130.225.17.5"), 55);\r
- // One more job is executed\r
- jq.ExecuteOne();\r
- // The first user tries to cancel his first and last job\r
- jq.Cancel(rid1);\r
- jq.Cancel(rid3);\r
- // The remaining jobs are executed\r
- while (jq.ExecuteOne() != null) { } \r
- }\r
-}\r
-\r
-class JobQueue {\r
- private readonly IPriorityQueue<Job> jobQueue;\r
- private readonly IDictionary<Rid,IPriorityQueueHandle<Job>> jobs;\r
- private readonly HashBag<Ip> userJobs;\r
-\r
- public JobQueue() {\r
- this.jobQueue = new IntervalHeap<Job>();\r
- this.jobs = new HashDictionary<Rid,IPriorityQueueHandle<Job>>();\r
- this.userJobs = new HashBag<Ip>();\r
- }\r
-\r
- public Rid Submit(Ip ip, int time) {\r
- int jobCount = userJobs.ContainsCount(ip);\r
- Rid rid = new Rid();\r
- Job job = new Job(rid, ip, time + 60 * jobCount);\r
- IPriorityQueueHandle<Job> h = null;\r
- jobQueue.Add(ref h, job);\r
- userJobs.Add(ip);\r
- jobs.Add(rid, h);\r
- Console.WriteLine("Submitted {0}", job); \r
- return rid;\r
- }\r
-\r
- public Job ExecuteOne() {\r
- if (!jobQueue.IsEmpty) {\r
- Job job = jobQueue.DeleteMin();\r
- userJobs.Remove(job.ip);\r
- jobs.Remove(job.rid);\r
- Console.WriteLine("Executed {0}", job); \r
- return job;\r
- } else\r
- return null;\r
- }\r
-\r
- public void Cancel(Rid rid) {\r
- IPriorityQueueHandle<Job> h;\r
- if (jobs.Remove(rid, out h)) {\r
- Job job = jobQueue.Delete(h);\r
- userJobs.Remove(job.ip);\r
- Console.WriteLine("Cancelled {0}", job);\r
- }\r
- }\r
-}\r
-\r
-class Job : IComparable<Job> {\r
- public readonly Rid rid;\r
- public readonly Ip ip;\r
- public readonly int time;\r
-\r
- public Job(Rid rid, Ip ip, int time) {\r
- this.rid = rid;\r
- this.ip = ip;\r
- this.time = time;\r
- }\r
-\r
- public int CompareTo(Job that) {\r
- return this.time - that.time;\r
- }\r
-\r
- public override String ToString() {\r
- return rid.ToString();\r
- }\r
-}\r
-\r
-class Rid { \r
- private readonly int ridNumber;\r
- private static int nextRid = 1;\r
- public Rid() {\r
- ridNumber = nextRid++;\r
- }\r
- public override String ToString() {\r
- return "rid=" + ridNumber;\r
- }\r
-}\r
-\r
-class Ip { \r
- public readonly String ipString;\r
-\r
- public Ip(String ipString) {\r
- this.ipString = ipString;\r
- }\r
-\r
- public override int GetHashCode() {\r
- return ipString.GetHashCode();\r
- }\r
-\r
- public override bool Equals(Object that) {\r
- return \r
- that != null \r
- && that is Ip \r
- && this.ipString.Equals(((Ip)that).ipString);\r
- }\r
-}\r
- \r
-\r
+/*
+ Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+// C5 example: job queue 2004-11-22
+
+// Compile with
+// csc /r:C5.dll Anagrams.cs
+
+using System;
+using C5;
+using SCG = System.Collections.Generic;
+
+class MyJobQueueTest {
+ public static void Main(String[] args) {
+ JobQueue jq = new JobQueue();
+ // One user submits three jobs at time=27
+ Rid rid1 = jq.Submit(new Ip("62.150.83.11"), 27),
+ rid2 = jq.Submit(new Ip("62.150.83.11"), 27),
+ rid3 = jq.Submit(new Ip("62.150.83.11"), 27);
+ // One job is executed
+ jq.ExecuteOne();
+ // Another user submits two jobs at time=55
+ Rid rid4 = jq.Submit(new Ip("130.225.17.5"), 55),
+ rid5 = jq.Submit(new Ip("130.225.17.5"), 55);
+ // One more job is executed
+ jq.ExecuteOne();
+ // The first user tries to cancel his first and last job
+ jq.Cancel(rid1);
+ jq.Cancel(rid3);
+ // The remaining jobs are executed
+ while (jq.ExecuteOne() != null) { }
+ }
+}
+
+class JobQueue {
+ private readonly IPriorityQueue<Job> jobQueue;
+ private readonly IDictionary<Rid,IPriorityQueueHandle<Job>> jobs;
+ private readonly HashBag<Ip> userJobs;
+
+ public JobQueue() {
+ this.jobQueue = new IntervalHeap<Job>();
+ this.jobs = new HashDictionary<Rid,IPriorityQueueHandle<Job>>();
+ this.userJobs = new HashBag<Ip>();
+ }
+
+ public Rid Submit(Ip ip, int time) {
+ int jobCount = userJobs.ContainsCount(ip);
+ Rid rid = new Rid();
+ Job job = new Job(rid, ip, time + 60 * jobCount);
+ IPriorityQueueHandle<Job> h = null;
+ jobQueue.Add(ref h, job);
+ userJobs.Add(ip);
+ jobs.Add(rid, h);
+ Console.WriteLine("Submitted {0}", job);
+ return rid;
+ }
+
+ public Job ExecuteOne() {
+ if (!jobQueue.IsEmpty) {
+ Job job = jobQueue.DeleteMin();
+ userJobs.Remove(job.ip);
+ jobs.Remove(job.rid);
+ Console.WriteLine("Executed {0}", job);
+ return job;
+ } else
+ return null;
+ }
+
+ public void Cancel(Rid rid) {
+ IPriorityQueueHandle<Job> h;
+ if (jobs.Remove(rid, out h)) {
+ Job job = jobQueue.Delete(h);
+ userJobs.Remove(job.ip);
+ Console.WriteLine("Cancelled {0}", job);
+ }
+ }
+}
+
+class Job : IComparable<Job> {
+ public readonly Rid rid;
+ public readonly Ip ip;
+ public readonly int time;
+
+ public Job(Rid rid, Ip ip, int time) {
+ this.rid = rid;
+ this.ip = ip;
+ this.time = time;
+ }
+
+ public int CompareTo(Job that) {
+ return this.time - that.time;
+ }
+
+ public override String ToString() {
+ return rid.ToString();
+ }
+}
+
+class Rid {
+ private readonly int ridNumber;
+ private static int nextRid = 1;
+ public Rid() {
+ ridNumber = nextRid++;
+ }
+ public override String ToString() {
+ return "rid=" + ridNumber;
+ }
+}
+
+class Ip {
+ public readonly String ipString;
+
+ public Ip(String ipString) {
+ this.ipString = ipString;
+ }
+
+ public override int GetHashCode() {
+ return ipString.GetHashCode();
+ }
+
+ public override bool Equals(Object that) {
+ return
+ that != null
+ && that is Ip
+ && this.ipString.Equals(((Ip)that).ipString);
+ }
+}
+
+