View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.util;
20  
21  
22  /**
23   *  The <code> BooleanSet </code> class implements a set through an array
24   *  of bits.
25   *
26   *  @author  Alberto Montresor
27   *  @author  Salvatore Cammarata
28   *  @since Jgroup 1.2
29   */
30  public class BooleanSet 
31  {
32    
33    /////////////////////////////////////////////////////////////////////////////
34    // Fields
35    /////////////////////////////////////////////////////////////////////////////
36  
37    private boolean[] set;
38    private int size;
39    
40    
41    /////////////////////////////////////////////////////////////////////////////
42    // Constructors
43    /////////////////////////////////////////////////////////////////////////////
44  
45    /**
46     *  Builds an empty bit set 
47     */
48    public BooleanSet(int size)
49    {
50      this.size = 0;
51      set = new boolean[size];
52    }
53  
54    
55    /////////////////////////////////////////////////////////////////////////////
56    // Methods
57    /////////////////////////////////////////////////////////////////////////////
58  
59    /**
60     *  Set the i-th element to true
61     */
62    public void set(int index)
63    {
64      if (index >= set.length) {
65        doubleCapacity();
66      }
67      if (!set[index]) {
68        set[index] = true;
69        size++;
70      }
71    }
72  
73    /**
74     *  Set the i-th element to false
75     */
76    public void unset(int index)
77    {
78      if (index >= set.length) {
79        doubleCapacity();
80      }
81      if (set[index]) {
82        set[index] = false;
83        size--;
84      }
85    }
86  
87    /**
88     *  Get the i-th element
89     */
90    public boolean lookup(int index)
91    { 
92      return set[index];
93    }
94  
95    /**
96     *  Return the number of elements set to true
97     */
98    public int size()
99    {
100     return size;
101   }
102 
103   /**
104    *  Return the capacity of the bit set
105    */
106   public int lastIndex()
107   {
108     return set.length;
109   }
110 
111   /**
112    *  Clear the bit set
113    */
114   public void clear()
115   {
116     for (int i=0; i < set.length; i++)
117       set[i] = false;
118     size = 0;
119   }
120 
121   /**
122    *  Returns a string representation of this object
123    */
124   public String toString()
125   {
126     StringBuilder ret = new StringBuilder();
127     for(int i=0; i <= set.length; i++)
128       ret.append(set[i] ? "1" : "0");
129     return ret.toString(); 
130   }
131 
132   /////////////////////////////////////////////////////////////////////////////
133   // Private methods
134   /////////////////////////////////////////////////////////////////////////////
135 
136   private void doubleCapacity()
137   {
138     // FIXME Write this method
139     throw new UnsupportedOperationException();
140   }
141   
142 } // END BooleanSet
143