Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 72 lines (62 sloc) 2.266 kb
86ac7c9 @odeheurles Initial checkin
odeheurles authored
1 namespace Disruptor
2 {
3 /// <summary>
4 /// Set of common functions used by the Disruptor
5 /// </summary>
6 internal static class Util
7 {
8 /// <summary>
9 /// Calculate the next power of 2, greater than or equal to x.
10 /// </summary>
11 /// <param name="x">Value to round up</param>
12 /// <returns>The next power of 2 from x inclusive</returns>
2396f7a @damageboy Ported Java version 2.8.0
damageboy authored
13 public static int CeilingNextPowerOfTwo(this int x)
86ac7c9 @odeheurles Initial checkin
odeheurles authored
14 {
15 var result = 2;
16
17 while (result < x)
18 {
2396f7a @damageboy Ported Java version 2.8.0
damageboy authored
19 result <<= 1;
86ac7c9 @odeheurles Initial checkin
odeheurles authored
20 }
21
22 return result;
23 }
24
25 /// <summary>
2396f7a @damageboy Ported Java version 2.8.0
damageboy authored
26 /// Test whether a given integer is a power of 2
27 /// </summary>
28 /// <param name="x"></param>
29 /// <returns></returns>
30 public static bool IsPowerOf2(this int x)
31 {
32 return x > 0 && (x & (x - 1)) == 0;
33 }
34
35
36 /// <summary>
86ac7c9 @odeheurles Initial checkin
odeheurles authored
37 /// Get the minimum sequence from an array of <see cref="Sequence"/>s.
38 /// </summary>
39 /// <param name="sequences">sequences to compare.</param>
40 /// <returns>the minimum sequence found or lon.MaxValue if the array is empty.</returns>
41 public static long GetMinimumSequence(Sequence[] sequences)
42 {
43 if (sequences.Length == 0) return long.MaxValue;
44
45 var min = long.MaxValue;
46 for (var i = 0; i < sequences.Length; i++)
47 {
48 var sequence = sequences[i].Value; // volatile read
49 min = min < sequence ? min : sequence;
50 }
51 return min;
52 }
53
54 /// <summary>
55 /// Get an array of <see cref="Sequence"/>s for the passed <see cref="IEventProcessor"/>s
56 /// </summary>
57 /// <param name="processors">processors for which to get the sequences</param>
58 /// <returns>the array of <see cref="Sequence"/>s</returns>
59 public static Sequence[] GetSequencesFor(params IEventProcessor[] processors)
60 {
61 var sequences = new Sequence[processors.Length];
62 for (int i = 0; i < sequences.Length; i++)
63 {
64 sequences[i] = processors[i].Sequence;
65 }
66
67 return sequences;
68 }
69
70
71 }
72 }
Something went wrong with that request. Please try again.