/// <summary>Adds the given element to this lookup.</summary> public void Add(string key, Element ele) { // Create link: AttributeLookupLink link = new AttributeLookupLink(ele); // Already got a link? AttributeLookupLink chain; if (!Lookup.TryGetValue(key, out chain)) { // Add it now: Lookup.Add(key, link); return; } // Follow the chain to the end and add it there: // We do this because it's rare in comparison to finding the "first" one which is always at the front. while (chain.Next != null) { chain = chain.Next; } // Add to the end: chain.Next = link; }
/// <summary>Removes the given element value from this lookup.</summary> /// <returns>True if the cache should also be removed.</returns> public bool Remove(string key, Element ele) { AttributeLookupLink chain; if (!Lookup.TryGetValue(key, out chain)) { return(false); } AttributeLookupLink previous = null; // Scan the chain looking for ele: while (chain != null) { if (chain.Element == ele) { // Chop it out. if (previous == null) { // Removing the first one. if (chain.Next == null) { // Obliterate it! Lookup.Remove(key); if (Lookup.Count == 0) { // Remove this cache. return(true); } return(false); } else { // We're going to keep this link in the lookup, // rather than removing it and putting the next one in instead. chain.Element = chain.Next.Element; chain.Next = chain.Next.Next; } } else { previous.Next = chain.Next; } } previous = chain; chain = chain.Next; } return(false); }