Satsuma
a delicious .NET graph library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
Utils.cs
Go to the documentation of this file.
1 #region License
2 /*This file is part of Satsuma Graph Library
3 Copyright © 2013 Balázs Szalkai
4 
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.*/
23 #endregion
24 
25 using System;
26 using System.Collections.Generic;
27 using System.Linq;
28 using System.Xml.Linq;
29 
30 namespace Satsuma
31 {
33  public interface IClearable
34  {
36  void Clear();
37  }
38 
40  internal static class Utils
41  {
43  public static double LargestPowerOfTwo(double d)
44  {
45  long bits = BitConverter.DoubleToInt64Bits(d);
46  bits &= 0x7FF0000000000000; // clear mantissa
47  if (bits == 0x7FF0000000000000) bits = 0x7FE0000000000000; // deal with infinity
48  return BitConverter.Int64BitsToDouble(bits);
49  }
50 
51  public static V MakeEntry<K, V>(Dictionary<K, V> dict, K key)
52  where V : new()
53  {
54  V result;
55  if (dict.TryGetValue(key, out result)) return result;
56  return (dict[key] = new V());
57  }
58 
59  public static void RemoveAll<T>(HashSet<T> set, Func<T, bool> condition)
60  {
61  foreach (var x in set.Where(condition).ToList())
62  set.Remove(x);
63  }
64 
65  public static void RemoveAll<K, V>(Dictionary<K, V> dict, Func<K, bool> condition)
66  {
67  foreach (var key in dict.Keys.Where(condition).ToList())
68  dict.Remove(key);
69  }
70 
71  public static void RemoveLast<T>(List<T> list, T element)
72  where T : IEquatable<T>
73  {
74  for (int i = list.Count - 1; i >= 0; i--)
75  {
76  if (element.Equals(list[i]))
77  {
78  list.RemoveAt(i);
79  break;
80  }
81  }
82  }
83 
85  public static IEnumerable<XElement> ElementsLocal(XElement xParent, string localName)
86  {
87  return xParent.Elements().Where(x => x.Name.LocalName == localName);
88  }
89 
91  public static XElement ElementLocal(XElement xParent, string localName)
92  {
93  return ElementsLocal(xParent, localName).FirstOrDefault();
94  }
95  }
96 
98  internal abstract class IdAllocator
99  {
100  private long randomSeed;
101  private long lastAllocated;
102 
103  public IdAllocator()
104  {
105  randomSeed = 205891132094649; // 3^30
106  Rewind();
107  }
108 
109  private long Random()
110  {
111  return (randomSeed *= 3);
112  }
113 
115  protected abstract bool IsAllocated(long id);
116 
118  public void Rewind()
119  {
120  lastAllocated = 0;
121  }
122 
125  public long Allocate()
126  {
127  long id = lastAllocated+1;
128  int streak = 0;
129  while (true)
130  {
131  if (id == 0) id = 1;
132  if (!IsAllocated(id))
133  {
134  lastAllocated = id;
135  return id;
136  }
137 
138  id++;
139  streak++;
140  if (streak >= 100)
141  {
142  id = Random();
143  streak = 0;
144  }
145  }
146  }
147  }
148 }