<commit_before>using Uno;
using Uno.Collections;
using Uno.Threading;

namespace Fuse
{
	public enum UpdateStage
	{
		//prior to the update loop, handling the raw OS events
		None = -1,
		/** where frame-based actions are triggered by default (UpdateManager.AddAction) */
		Primary,
		/** Includes layout and responding triggers. */
		Layout,
		//tree structure and data should be static at this point
		Draw
	}
	
	/**
		Provides appropriate values for priority of actions within the Layout stage.
	*/
	public static class LayoutPriority
	{
		static public int Layout = 0;
		static public int Placement = 100;
		//should be resolved after other normal activities, but is not cleanup
		static public int Later = 500;
		//should be resolved last, such as cleanup
		static public int Post = 1000;
	}
	
	public interface IUpdateListener
	{
		void Update();
	}
	
	class UpdateCallback
	{
		//only one of udpate/action will be set
		public Action action;
		public IUpdateListener update;
		public bool removed;
		
		public void Invoke()
		{
			if (removed)
				return;
				
			if (action != null)
				action();
				
			if (update != null)
				update.Update();
		}
	}
	
	class UpdateListener : UpdateCallback
	{
		public int deferFrame;
		public int sequence;
	}

	class UpdateAction : UpdateCallback
	{
		public int priority;
	}
	
	class Stage
	{
		public UpdateStage UpdateStage;
		
		public List<UpdateListener> Listeners = new List<UpdateListener>();
		public List<UpdateListener> Onces = new List<UpdateListener>();
		public List<UpdateListener> OncesPending = new List<UpdateListener>();
		
		public List<UpdateAction> PhaseDeferredActions = new List<UpdateAction>();
		public int PhaseDeferredActionsAt = -1;
		
		public bool HasListenersRemoved;

		public Stage(UpdateStage _updateStage)
		{
			UpdateStage = _updateStage;
		}
		
		//insert them in sequenced order
		public void Insert( List<UpdateListener> list, UpdateListener us )
		{
			for (int i=list.Count; i > 0; --i)
			{
				if (list[i-1].sequence <= us.sequence)
				{
					list.Insert(i, us);
					return;
				}
			}
			
			list.Insert(0,us);
		}
		
		/*
			Keeps a sorted list of deferred actions. This list is processed in parallel, thus
			the add only scans back to PhaseDeferredActionsAt (the one currently being executed).
		*/
		public void AddDeferredAction( Action pu, IUpdateListener ul, int priority = 0 )
		{
			int at = PhaseDeferredActions.Count;
			while( at - 1 > PhaseDeferredActionsAt && 
				PhaseDeferredActions[at-1].priority > priority )
				at--;
				
			PhaseDeferredActions.Insert(at, new UpdateAction{
				action = pu, update = ul, priority = priority});
		}
		
		public void ResetDeferredActions()
		{
			PhaseDeferredActions.Clear();
			PhaseDeferredActionsAt = -1;
		}
	}

	class UpdateDispatcher: Uno.Threading.IDispatcher
	{
		public void Invoke(Action action)
		{
			UpdateManager.PostAction(action);
		}
	}
	
	public static class UpdateManager
	{
		static List<Stage> _stages = new List<Stage>();
		
		static List<Action> _postActions = new List<Action>();
		static List<Action> _postActionsSwap = new List<Action>();
		
		static UpdateManager()<commit_msg>object pooling for deferred actions go back to struct, classes had no gain<commit_after>using Uno;
using Uno.Collections;
using Uno.Threading;

namespace Fuse
{
	public enum UpdateStage
	{
		//prior to the update loop, handling the raw OS events
		None = -1,
		/** where frame-based actions are triggered by default (UpdateManager.AddAction) */
		Primary,
		/** Includes layout and responding triggers. */
		Layout,
		//tree structure and data should be static at this point
		Draw
	}
	
	/**
		Provides appropriate values for priority of actions within the Layout stage.
	*/
	public static class LayoutPriority
	{
		static public int Layout = 0;
		static public int Placement = 100;
		//should be resolved after other normal activities, but is not cleanup
		static public int Later = 500;
		//should be resolved last, such as cleanup
		static public int Post = 1000;
	}
	
	public interface IUpdateListener
	{
		void Update();
	}
	
	class UpdateListener
	{
		//only one of update/action will be set
		public Action action;
		public IUpdateListener update;
		public bool removed;
		public int deferFrame;
		public int sequence;
		
		public void Invoke()
		{
			if (removed)
				return;
				
			if (action != null)
				action();
				
			if (update != null)
				update.Update();
		}
	}

	struct UpdateAction
	{
		public Action action;
		public IUpdateListener update;
		public int priority;
		
		public void Invoke()
		{
			if (action != null)
				action();
				
			if (update != null)
				update.Update();
		}
	}
	
	class Stage
	{
		public UpdateStage UpdateStage;
		
		public List<UpdateListener> Listeners = new List<UpdateListener>();
		public List<UpdateListener> Onces = new List<UpdateListener>();
		public List<UpdateListener> OncesPending = new List<UpdateListener>();
		
		public List<UpdateAction> PhaseDeferredActions = new List<UpdateAction>();
		public int PhaseDeferredActionsAt = -1;
		
		public bool HasListenersRemoved;

		public Stage(UpdateStage _updateStage)
		{
			UpdateStage = _updateStage;
		}
		
		//insert them in sequenced order
		public void Insert( List<UpdateListener> list, UpdateListener us )
		{
			for (int i=list.Count; i > 0; --i)
			{
				if (list[i-1].sequence <= us.sequence)
				{
					list.Insert(i, us);
					return;
				}
			}
			
			list.Insert(0,us);
		}
		
		/*
			Keeps a sorted list of deferred actions. This list is processed in parallel, thus
			the add only scans back to PhaseDeferredActionsAt (the one currently being executed).
		*/
		public void AddDeferredAction( Action pu, IUpdateListener ul, int priority = 0 )
		{
			int at = PhaseDeferredActions.Count;
			while( at - 1 > PhaseDeferredActionsAt && 
				PhaseDeferredActions[at-1].priority > priority )
				at--;
				
			PhaseDeferredActions.Insert(at, new UpdateAction{ action = pu, update = ul, priority = priority });
		}
		
		public void ResetDeferredActions()
		{
			PhaseDeferredActions.Clear();
			PhaseDeferredActionsAt = -1;
		}
	}

	class UpdateDispatcher: Uno.Threading.IDispatcher
	{
		public void Invoke(Action action)
		{
			UpdateManager.PostAction(action);
		}
	}
	
	public static class UpdateManager
	{
		static List<Stage> _stages = new List<Stage>();
		
		static List<Action> _postActions = new List<Action>();
		static List<Action> _postActionsSwap = new List<Action>();
		
		static UpdateManager()
{'fraction_non_alphanumeric': 0.09076875578882371, 'fraction_numerical': 0.006174745291756715, 'mean_word_length': 3.304983388704319, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5813783', 'n_tokens_mistral': 2296, 'n_tokens_neox': 1884, 'n_words': 636}