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 
60  public static void RemoveAll<T>(List<T> list, Func<T, bool> condition)
61  {
62  for (int i = 0; i < list.Count; ++i)
63  {
64  if (condition(list[i]))
65  {
66  if (i < list.Count - 1)
67  list[i] = list[list.Count - 1];
68  list.RemoveAt(list.Count - 1);
69  }
70  }
71  }
72 
73  public static void RemoveAll<T>(HashSet<T> set, Func<T, bool> condition)
74  {
75  foreach (var x in set.Where(condition).ToList())
76  set.Remove(x);
77  }
78 
79  public static void RemoveAll<K, V>(Dictionary<K, V> dict, Func<K, bool> condition)
80  {
81  foreach (var key in dict.Keys.Where(condition).ToList())
82  dict.Remove(key);
83  }
84 
85  public static void RemoveLast<T>(List<T> list, T element)
86  where T : IEquatable<T>
87  {
88  for (int i = list.Count - 1; i >= 0; i--)
89  {
90  if (element.Equals(list[i]))
91  {
92  list.RemoveAt(i);
93  break;
94  }
95  }
96  }
97 
99  public static IEnumerable<XElement> ElementsLocal(XElement xParent, string localName)
100  {
101  return xParent.Elements().Where(x => x.Name.LocalName == localName);
102  }
103 
105  public static XElement ElementLocal(XElement xParent, string localName)
106  {
107  return ElementsLocal(xParent, localName).FirstOrDefault();
108  }
109  }
110 
112  internal abstract class IdAllocator
113  {
114  private long randomSeed;
115  private long lastAllocated;
116 
117  public IdAllocator()
118  {
119  randomSeed = 205891132094649; // 3^30
120  Rewind();
121  }
122 
123  private long Random()
124  {
125  return (randomSeed *= 3);
126  }
127 
129  protected abstract bool IsAllocated(long id);
130 
132  public void Rewind()
133  {
134  lastAllocated = 0;
135  }
136 
139  public long Allocate()
140  {
141  long id = lastAllocated+1;
142  int streak = 0;
143  while (true)
144  {
145  if (id == 0) id = 1;
146  if (!IsAllocated(id))
147  {
148  lastAllocated = id;
149  return id;
150  }
151 
152  id++;
153  streak++;
154  if (streak >= 100)
155  {
156  id = Random();
157  streak = 0;
158  }
159  }
160  }
161  }
162 }