1 | #region License Information
|
---|
2 | /* SimSharp - A .NET port of SimPy, discrete event simulation framework
|
---|
3 | Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 |
|
---|
5 | This program is free software: you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation, either version 3 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.*/
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | using System;
|
---|
20 | using System.Collections.Generic;
|
---|
21 | using System.Linq;
|
---|
22 |
|
---|
23 | namespace 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 | }
|
---|