From ac4c5fb2c562398a0953ea272b655c4c73d3c50e Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 14 Aug 2026 09:56:31 +0200 Subject: [PATCH] Improvements --- Runtime/Scripts/Core/Cluster.cs | 53 ++++++++++++++++++++++----------- Runtime/Scripts/Core/Neuron.cs | 7 +++-- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 0a92d1e..e516491 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -90,20 +90,36 @@ namespace NanoBrain { public long timestamp; public Action action; - public void AddTo(List actions) { - int ix = actions.FindIndex(f => f.neuronName == this.neuronName); + public static void AddTo(List actions, string neuronName, Action action, long timestamp) { + int ix = -1; + int insertIx = actions.Count; // default: add at end + + // Find existing item and the first item with timestamp > this.timestamp + for (int i = 0; i < actions.Count; i++) { + TimedAction item = actions[i]; + + if (ix < 0 && item.neuronName == neuronName) { + ix = i; + break; + } + } + if (ix >= 0) { - // update the reset time of the existing function call - actions[ix] = this; + actions[ix].action = action; + actions[ix].timestamp = timestamp; + return; } - else { - // Add the function call in a sorted manner - ix = actions.FindIndex(f => f.timestamp > this.timestamp); - if (ix < 0) - actions.Add(this); - else - actions.Insert(ix, this); + + // No existing item; compute insertion point (sorted by timestamp ascending) + for (int i = 0; i < actions.Count; i++) { + if (actions[i].timestamp > timestamp) { + insertIx = i; + break; + } } + + TimedAction newAction = new(neuronName, action, timestamp); + actions.Insert(insertIx, newAction); } } public readonly List actions = new(); @@ -516,17 +532,18 @@ namespace NanoBrain { /// /// The multi-cluster instance which not no longer be mapped private void RemoveThingCluster(Cluster cluster) { - List keysToRemove = new(); - foreach (KeyValuePair kvp in thingClusters) { - if (kvp.Value == cluster) - keysToRemove.Add(kvp.Key); + int keyToRemove = -1; + foreach (KeyValuePair kvp in this.thingClusters) { + if (kvp.Value == cluster) { + keyToRemove = kvp.Key; + break; + } } - foreach (int thingId in keysToRemove) - thingClusters.Remove(thingId); + if (keyToRemove >= 0) + this.thingClusters.Remove(keyToRemove); } - public List GetAllInstances(Neuron nucleus) { List allInstances = new(); diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index b1508b6..a528f4d 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -37,6 +37,7 @@ namespace NanoBrain { this.parent.nuclei ??= new(); this.parent.nuclei.Add(this); } + this.resetStimulus = ResetStimulus; } #region Serialization @@ -796,6 +797,7 @@ namespace NanoBrain { #endregion Back propagation private CancellationTokenSource _cts; + private Action resetStimulus; /// /// Process an external stimulus @@ -809,12 +811,11 @@ namespace NanoBrain { if (autoResetDelay > 0) { long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); long resetTime = now + (long)(autoResetDelay * 1000.0f); - Cluster.TimedAction action = new(this.name, ResetStimulus, resetTime); - action.AddTo(this.parent.actions); + Cluster.TimedAction.AddTo(this.parent.actions, this.name, this.resetStimulus, resetTime); } } - protected void ResetStimulus() { + private void ResetStimulus() { // Debug.Log("reset stimulus"); this.bias = Vector3.zero; this.parent?.UpdateFromNucleus(this);