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