1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.SimulationCore {
|
---|
8 | [StorableClass]
|
---|
9 | public sealed class CompositeEventQueue<TModel> : Item where TModel : IModel {
|
---|
10 | [Storable]
|
---|
11 | private List<IEventQueue<TModel>> queues;
|
---|
12 |
|
---|
13 | public bool Empty {
|
---|
14 | get { return queues.All(x => x.Empty); }
|
---|
15 | }
|
---|
16 |
|
---|
17 | public int Count {
|
---|
18 | get { return queues.Sum(x => x.Count); }
|
---|
19 | }
|
---|
20 |
|
---|
21 | public IEvent<TModel> Peek() {
|
---|
22 | IEvent<TModel> nextEvent = null;
|
---|
23 | foreach (var queue in queues) {
|
---|
24 | if (!queue.Empty && (nextEvent == null || queue.Peek().Time < nextEvent.Time)) {
|
---|
25 | nextEvent = queue.Peek();
|
---|
26 | }
|
---|
27 | }
|
---|
28 | return nextEvent;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public IEvent<TModel> Pop() {
|
---|
32 | IEvent<TModel> nextEvent = null;
|
---|
33 | IEventQueue<TModel> minEventQueue = null;
|
---|
34 | foreach (var queue in queues) {
|
---|
35 | if (!queue.Empty && (nextEvent == null || queue.Peek().Time < nextEvent.Time)) {
|
---|
36 | nextEvent = queue.Peek();
|
---|
37 | minEventQueue = queue;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | return minEventQueue.Pop();
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void Clear() {
|
---|
44 | foreach (var queue in queues) {
|
---|
45 | queue.Clear();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | private CompositeEventQueue(bool deserializing) : base(deserializing) { }
|
---|
51 | private CompositeEventQueue(CompositeEventQueue<TModel> original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | this.queues = original.queues.Select(cloner.Clone).ToList();
|
---|
54 | }
|
---|
55 | public CompositeEventQueue(IEnumerable<IEventQueue<TModel>> queues) {
|
---|
56 | this.queues = new List<IEventQueue<TModel>>(queues);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new CompositeEventQueue<TModel>(this, cloner);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|