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());
60 public static void RemoveAll<T>(List<T> list, Func<T, bool> condition)
62 for (
int i = 0; i < list.Count; ++i)
64 if (condition(list[i]))
66 if (i < list.Count - 1)
67 list[i] = list[list.Count - 1];
68 list.RemoveAt(list.Count - 1);
73 public static void RemoveAll<T>(HashSet<T>
set, Func<T, bool> condition)
75 foreach (var x
in set.Where(condition).ToList())
79 public static void RemoveAll<K, V>(Dictionary<K, V> dict, Func<K, bool> condition)
81 foreach (var key
in dict.Keys.Where(condition).ToList())
85 public static void RemoveLast<T>(List<T> list, T element)
86 where T : IEquatable<T>
88 for (
int i = list.Count - 1; i >= 0; i--)
90 if (element.Equals(list[i]))
99 public static IEnumerable<XElement> ElementsLocal(XElement xParent,
string localName)
101 return xParent.Elements().Where(x => x.Name.LocalName == localName);
105 public static XElement ElementLocal(XElement xParent,
string localName)
107 return ElementsLocal(xParent, localName).FirstOrDefault();
112 internal abstract class IdAllocator
114 private long randomSeed;
115 private long lastAllocated;
119 randomSeed = 205891132094649;
123 private long Random()
125 return (randomSeed *= 3);
129 protected abstract bool IsAllocated(
long id);
139 public long Allocate()
141 long id = lastAllocated+1;
146 if (!IsAllocated(
id))