1 namespace SpikingNeuronNetwork.Lib
4 using System.Collections;
5 using System.Collections.Generic;
16 private readonly SortedDictionary<double, Queue<int>> _baseQueue;
26 var inputSpikeTimesList = inputSpikes.ToList();
28 if (spikeQueue.Count == 0)
34 while (currentTime > spikeQueue.PeekTime())
50 foreach (var inputSpike
in inputSpikes)
61 _baseQueue =
new SortedDictionary<double, Queue<int>>();
64 #region Priority queue operations
71 if (_baseQueue.ContainsKey(inputSpike.
Time))
73 _baseQueue[inputSpike.Time].Enqueue(inputSpike.NeuronIndex);
77 _baseQueue.Add(inputSpike.Time,
new Queue<int>(
new[] {inputSpike.NeuronIndex}));
96 throw new InvalidOperationException(
"Priority queue is empty");
108 return Dequeue().NeuronIndex;
122 var first = _baseQueue.First();
123 return new Spike { Time = first.Key, NeuronIndex = first.Value.Peek()};
125 throw new InvalidOperationException(
"Priority queue is empty");
145 get {
return _baseQueue.Count == 0; }
156 if (!Remove(oldSpike))
158 if (!Double.IsInfinity(newFiringTime))
160 Add(
new Spike { Time = newFiringTime, NeuronIndex = oldSpike.NeuronIndex });
171 foreach (var spike
in spikes)
179 #region ICollection<Spike> implementation
205 if (!_baseQueue.ContainsKey(spike.
Time))
209 return _baseQueue[spike.Time].Contains(spike.NeuronIndex);
217 get {
return _baseQueue.Sum(x => x.Value.Count); }
230 var arrayList =
new List<Spike>();
231 foreach (var kvp
in _baseQueue)
233 var queueList = kvp.Value.ToList();
234 arrayList.AddRange(queueList.Select(item =>
new Spike { Time = kvp.Key, NeuronIndex = item}));
236 arrayList.CopyTo(array, arrayIndex);
245 public bool IsReadOnly
247 get {
return false; }
259 if (!_baseQueue.ContainsKey(spike.
Time))
263 if (_baseQueue[spike.
Time].Count == 1)
265 _baseQueue.Remove(spike.Time);
269 var queueList = _baseQueue[spike.Time].ToList();
270 queueList.Remove(spike.NeuronIndex);
271 _baseQueue[spike.Time] =
new Queue<int>(queueList);
283 return (from kvp in _baseQueue
284 from item in kvp.Value.ToList()
285 select
new Spike{ Time = kvp.Key, NeuronIndex = item}).GetEnumerator();
294 IEnumerator IEnumerable.GetEnumerator()
296 return GetEnumerator();
int DequeueValue()
Dequeues spike with next firing time and returns its index
SpikePriorityQueue()
Initializes a new instance of the spike priority queue
Spike Peek()
Returns the spike with next firing time, without removing it from the queue
static SpikePriorityQueue CreateSpikeQueue(IEnumerable< Spike > inputSpikes, double currentTime)
Creates a new spike priority queue
void Enqueue(Spike inputSpike)
Enqueues spike into the priority queue
double Time
Gets or sets the time.
void Add(Spike spike)
Enqueus spike into priority queue
bool Update(Spike oldSpike, double newFiringTime)
Updates a spike in the queue with a new firing time
bool Contains(Spike spike)
Determines whether the priority queue contains a specific spike
void AddRange(ICollection< Spike > spikes)
Adds a range of spikes to the priority queue
Spike Priority Queue used for determining what spike to process next based on spike timing ...
SpikePriorityQueue(IEnumerable< Spike > inputSpikes)
Initializes a new instance of priority queue with specified input spikes
void Clear()
Clears the collection
Spike Dequeue()
Dequeues spike with next firing time and returns the spike/>
void CopyTo(Spike[] array, int arrayIndex)
Copies the spikes of the priority queue to an Array, starting at a particular Array index...
IEnumerator< Spike > GetEnumerator()
Returns an enumerator that iterates through the collection.
double PeekTime()
Returns the next firing time of a spike, without removing the spike from the queue ...
bool Remove(Spike spike)
Removes the first occurrence of a specific spike from the priority queue.