using System; using System.Collections.Generic; using System.Text; namespace Sunweaver { public class SystemVariables { private static string[] Variables; private static Dictionary Indices; public bool Contains(string variable) { if (this[variable] != -1) return true; return false; } public string this[int index] { get { if (Variables == null) Initialize(); return Variables[Math.Abs(index)%Variables.Length]; } } public int this[string variable] { get { if (Indices == null) Initialize(); if(Indices.ContainsKey(variable)) return Indices[variable]; return -1; } } private static void Initialize() { Variables = new string[1000]; Indices = new Dictionary(); AddUsedVariables(); int count = 0; for (int i = 0; i < Variables.Length; i++) { if (Variables[i] == null) { Variables[i] = "freemem" + count.ToString(); count++; } Indices.Add(Variables[i], i); } } private static void AddUsedVariables() { AddUsedVariable("mydir"); AddUsedVariable("myvelx"); AddUsedVariable("myvely"); AddUsedVariable("mynrg"); AddUsedVariable("myfat"); AddUsedVariable("myleaf"); AddUsedVariable("mymuscle"); } private static void AddUsedVariable(string variable, int index) { if (Variables[index] == null) { Variables[index] = variable; } } private static void AddUsedVariable(string variable) { int i = Variables.Length-1; while(i>=0 && Variables[i]==null) { i--; } if (i >= 0) { AddUsedVariable(variable, i); } } } }