/// <summary> /// Returns the data entry to the pool. /// </summary> private void ReturnToPool(LexiconFocusData data) { List <LexiconFocusData> dataPool; if (focusDataPool.TryGetValue(data.GetType(), out dataPool)) { dataPool.Add(data); } else { List <LexiconFocusData> newPool = new List <LexiconFocusData>(); newPool.Add(data); focusDataPool.Add(data.GetType(), newPool); } }
/// <summary> /// Record a focus data entry. /// </summary> public void AddFocusData(LexiconFocusData data) { List <LexiconFocusData> dataEntries; if (focusDataDict.TryGetValue(data.GetType(), out dataEntries)) { dataEntries.Add(data); } else { List <LexiconFocusData> newList = new List <LexiconFocusData>(); newList.Add(data); focusDataDict.Add(data.GetType(), newList); } }
/// <summary> /// Retrieve a data entry from the pool for a particular type. Use this to avoid allocations. /// </summary> public T GetPooledData <T>() where T : LexiconFocusData, new() { List <LexiconFocusData> dataPool; if (focusDataPool.TryGetValue(typeof(T), out dataPool)) { if (dataPool.Count > 0) { LexiconFocusData data = dataPool[0]; dataPool.RemoveAt(0); data.Timestamp = Time.realtimeSinceStartup; return((T)data); } } T newData = new T(); newData.IsPooled = true; return(newData); }
/// <summary> /// Gets the closest data entry of type T to the given timestamp, within maxTimeOffset seconds. /// </summary> public T GetFocusData <T>(float realtime, float maxTimeOffset = 0.5f) where T : LexiconFocusData { float minDist = float.MaxValue; LexiconFocusData result = null; List <LexiconFocusData> dataEntries; if (focusDataDict.TryGetValue(typeof(T), out dataEntries)) { foreach (LexiconFocusData data in dataEntries) { // TODO: Optimize this. float dist = Mathf.Abs(realtime - data.Timestamp); if (dist < minDist && dist < maxTimeOffset) { minDist = dist; result = data; } } } return((T)result); }