Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
BitSetSupport.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 namespace Lucene.Net.Support
23 {
24  /// <summary>
25  /// This class provides supporting methods of java.util.BitSet
26  /// that are not present in System.Collections.BitArray.
27  /// </summary>
28  public class BitSetSupport
29  {
30  /// <summary>
31  /// Returns the next set bit at or after index, or -1 if no such bit exists.
32  /// </summary>
33  /// <param name="bitArray"></param>
34  /// <param name="index">the index of bit array at which to start checking</param>
35  /// <returns>the next set bit or -1</returns>
36  public static int NextSetBit(System.Collections.BitArray bitArray, int index)
37  {
38  while (index < bitArray.Length)
39  {
40  // if index bit is set, return it
41  // otherwise check next index bit
42  if (bitArray.Get(index))
43  return index;
44  else
45  index++;
46  }
47  // if no bits are set at or after index, return -1
48  return -1;
49  }
50 
51  /// <summary>
52  /// Returns the next un-set bit at or after index, or -1 if no such bit exists.
53  /// </summary>
54  /// <param name="bitArray"></param>
55  /// <param name="index">the index of bit array at which to start checking</param>
56  /// <returns>the next set bit or -1</returns>
57  public static int NextClearBit(System.Collections.BitArray bitArray, int index)
58  {
59  while (index < bitArray.Length)
60  {
61  // if index bit is not set, return it
62  // otherwise check next index bit
63  if (!bitArray.Get(index))
64  return index;
65  else
66  index++;
67  }
68  // if no bits are set at or after index, return -1
69  return -1;
70  }
71 
72  /// <summary>
73  /// Returns the number of bits set to true in this BitSet.
74  /// </summary>
75  /// <param name="bits">The BitArray object.</param>
76  /// <returns>The number of bits set to true in this BitSet.</returns>
77  public static int Cardinality(System.Collections.BitArray bits)
78  {
79  int count = 0;
80  for (int i = 0; i < bits.Count; i++)
81  {
82  if (bits[i])
83  count++;
84  }
85  return count;
86  }
87  }
88 }