26 using System.Collections.Generic;
28 using System.Xml.Linq;
40 internal static class Utils
43 public static double LargestPowerOfTwo(
double d)
45 long bits = BitConverter.DoubleToInt64Bits(d);
46 bits &= 0x7FF0000000000000;
47 if (bits == 0x7FF0000000000000) bits = 0x7FE0000000000000;
48 return BitConverter.Int64BitsToDouble(bits);
51 public static V MakeEntry<K, V>(Dictionary<K, V> dict, K key)
55 if (dict.TryGetValue(key, out result))
return result;
56 return (dict[key] =
new V());
59 public static void RemoveAll<T>(HashSet<T>
set, Func<T, bool> condition)
61 foreach (var x
in set.Where(condition).ToList())
65 public static void RemoveAll<K, V>(Dictionary<K, V> dict, Func<K, bool> condition)
67 foreach (var key
in dict.Keys.Where(condition).ToList())
71 public static void RemoveLast<T>(List<T> list, T element)
72 where T : IEquatable<T>
74 for (
int i = list.Count - 1; i >= 0; i--)
76 if (element.Equals(list[i]))
85 public static IEnumerable<XElement> ElementsLocal(XElement xParent,
string localName)
87 return xParent.Elements().Where(x => x.Name.LocalName == localName);
91 public static XElement ElementLocal(XElement xParent,
string localName)
93 return ElementsLocal(xParent, localName).FirstOrDefault();
98 internal abstract class IdAllocator
100 private long randomSeed;
101 private long lastAllocated;
105 randomSeed = 205891132094649;
109 private long Random()
111 return (randomSeed *= 3);
115 protected abstract bool IsAllocated(
long id);
125 public long Allocate()
127 long id = lastAllocated+1;
132 if (!IsAllocated(
id))