New test.
[mono.git] / mcs / class / Mono.C5 / UserGuideExamples / Jobqueue.cs
1 /*\r
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
9  \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
12  \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
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: job queue 2004-11-22\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll Anagrams.cs \r
26 \r
27 using System;\r
28 using C5;\r
29 using SCG = System.Collections.Generic;\r
30 \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
39     jq.ExecuteOne();\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
44     jq.ExecuteOne();\r
45     // The first user tries to cancel his first and last job\r
46     jq.Cancel(rid1);\r
47     jq.Cancel(rid3);\r
48     // The remaining jobs are executed\r
49     while (jq.ExecuteOne() != null) { } \r
50   }\r
51 }\r
52 \r
53 class JobQueue {\r
54   private readonly IPriorityQueue<Job> jobQueue;\r
55   private readonly IDictionary<Rid,IPriorityQueueHandle<Job>> jobs;\r
56   private readonly HashBag<Ip> userJobs;\r
57 \r
58   public JobQueue() {\r
59     this.jobQueue = new IntervalHeap<Job>();\r
60     this.jobs = new HashDictionary<Rid,IPriorityQueueHandle<Job>>();\r
61     this.userJobs = new HashBag<Ip>();\r
62   }\r
63 \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
70     userJobs.Add(ip);\r
71     jobs.Add(rid, h);\r
72     Console.WriteLine("Submitted {0}", job);    \r
73     return rid;\r
74   }\r
75 \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
82       return job;\r
83     } else\r
84       return null;\r
85   }\r
86 \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
93     }\r
94   }\r
95 }\r
96 \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
101 \r
102   public Job(Rid rid, Ip ip, int time) {\r
103     this.rid = rid;\r
104     this.ip = ip;\r
105     this.time = time;\r
106   }\r
107 \r
108   public int CompareTo(Job that) {\r
109     return this.time - that.time;\r
110   }\r
111 \r
112   public override String ToString() {\r
113     return rid.ToString();\r
114   }\r
115 }\r
116 \r
117 class Rid { \r
118   private readonly int ridNumber;\r
119   private static int nextRid = 1;\r
120   public Rid() {\r
121     ridNumber = nextRid++;\r
122   }\r
123   public override String ToString() {\r
124     return "rid=" + ridNumber;\r
125   }\r
126 }\r
127 \r
128 class Ip { \r
129   public readonly String ipString;\r
130 \r
131   public Ip(String ipString) {\r
132     this.ipString = ipString;\r
133   }\r
134 \r
135   public override int GetHashCode() {\r
136     return ipString.GetHashCode();\r
137   }\r
138 \r
139   public override bool Equals(Object that) {\r
140     return \r
141       that != null \r
142       && that is Ip \r
143       && this.ipString.Equals(((Ip)that).ipString);\r
144   }\r
145 }\r
146  \r
147 \r