4 // Copyright (C) 2005 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 using System
.Collections
;
30 namespace Beagle
.Util
{
32 public class SmallIntArray
{
35 private int bits_per_int
;
37 private BetterBitArray bit_array
;
39 public SmallIntArray (int length
, int max_int
)
42 this.max_int
= max_int
;
44 this.bits_per_int
= 0;
45 while (max_int
!= 0) {
47 max_int
= max_int
>> 1;
50 this.bit_array
= new BetterBitArray (this.length
* this.bits_per_int
);
53 public int this [int index
] {
54 get { return Get (index); }
55 set { Set (index, value); }
59 get { return length; }
63 get { return length; }
66 public int MaxInteger
{
67 get { return max_int; }
70 public int Get (int index
)
74 i
= index
* bits_per_int
;
75 for (j
= 0; j
< bits_per_int
; ++j
) {
78 if (bit_array
.Get (i
+j
))
85 public void Set (int index
, int value)
87 if (value < 0 || value > max_int
)
88 throw new ArgumentException ("invalid value");
90 i
= (index
+ 1) * bits_per_int
- 1;
91 for (j
= 0; j
< bits_per_int
; ++j
) {
92 bit_array
.Set (i
-j
, (value & 1) == 1);
97 public void SetAll (int value)
100 bit_array
.SetAll (false);
104 // This is not particularly efficient
105 for (int i
= 0; i
< length
; ++i
)
109 public void Incr (BetterBitArray bit_array
)
111 if (bit_array
.Length
!= length
)
112 throw new Exception ("Incr BetterBitArray has wrong length!");
116 i
= bit_array
.GetNextTrueIndex (i
);
119 Set (i
, Get (i
) + 1);
124 public bool ContainsNonZero ()
126 return bit_array
.ContainsTrue ();
129 public int GetNextNonZeroIndex (int start
)
135 return bit_array
.GetNextTrueIndex (start
* bits_per_int
) / bits_per_int
;