source: trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.0.11/SimSharp-3.0.11/Core/Resources/Store.cs @ 15972

Last change on this file since 15972 was 15972, checked in by abeham, 20 months ago

#2926: Replaced Sim# 3.0.9 with 3.0.11

File size: 2.8 KB
Line 
1#region License Information
2/* SimSharp - A .NET port of SimPy, discrete event simulation framework
3Copyright (C) 2016  Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22
23namespace SimSharp {
24  public class Store {
25    public int Count { get { return Items.Count; } }
26    public int Capacity { get; protected set; }
27    protected Environment Environment { get; private set; }
28
29    protected Queue<StorePut> PutQueue { get; private set; }
30    protected Queue<StoreGet> GetQueue { get; private set; }
31    protected List<object> Items { get; private set; }
32
33    public Store(Environment environment, int capacity = int.MaxValue) {
34      if (capacity <= 0) throw new ArgumentException("Capacity must be > 0", "capacity");
35      Environment = environment;
36      Capacity = capacity;
37      PutQueue = new Queue<StorePut>();
38      GetQueue = new Queue<StoreGet>();
39      Items = new List<object>();
40    }
41
42    public virtual StorePut Put(object item) {
43      var put = new StorePut(Environment, TriggerGet, item);
44      PutQueue.Enqueue(put);
45      TriggerPut();
46      return put;
47    }
48
49    public virtual StoreGet Get() {
50      var get = new StoreGet(Environment, TriggerPut);
51      GetQueue.Enqueue(get);
52      TriggerGet();
53      return get;
54    }
55
56    protected virtual void DoPut(StorePut put) {
57      if (Items.Count < Capacity) {
58        Items.Add(put.Value);
59        put.Succeed();
60      }
61    }
62
63    protected virtual void DoGet(StoreGet get) {
64      if (Items.Count > 0) {
65        var item = Items.First();
66        Items.RemoveAt(0);
67        get.Succeed(item);
68      }
69    }
70
71    protected virtual void TriggerPut(Event @event = null) {
72      while (PutQueue.Count > 0) {
73        var put = PutQueue.Peek();
74        DoPut(put);
75        if (put.IsTriggered) {
76          PutQueue.Dequeue();
77        } else break;
78      }
79    }
80
81    protected virtual void TriggerGet(Event @event = null) {
82      while (GetQueue.Count > 0) {
83        var get = GetQueue.Peek();
84        DoGet(get);
85        if (get.IsTriggered) {
86          GetQueue.Dequeue();
87        } else break;
88      }
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.