Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/CompositeEventQueue.cs @ 13806

Last change on this file since 13806 was 10454, checked in by abeham, 11 years ago

#1610: updated core, implemented card game sample

File size: 1.9 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
7namespace 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}
Note: See TracBrowser for help on using the repository browser.