KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sosnoski > util > hashset > PrimitiveSetBase


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package com.sosnoski.util.hashset;
24
25 import java.lang.reflect.Array JavaDoc;
26
27 import com.sosnoski.util.PrimitiveHashBase;
28
29 /**
30  * Base class for type-specific hash set classes with primitive keys. This
31  * class builds on the basic structure provided by
32  * <code>PrimitiveHashBase</code>, specializing it for the case of a hash set
33  * where the keys are the only data. See the base class description for
34  * details of the implementation.<p>
35  *
36  * Hash sets based on this class are unsynchronized in order to provide the
37  * best possible performance for typical usage scenarios, so explicit
38  * synchronization must be implemented by the subclass or the application in
39  * cases where they are to be modified in a multithreaded environment.<p>
40  *
41  * Subclasses need to implement the abstract methods defined by the base class
42  * for working with the key array, and the abstract restructuring method
43  * defined by this class, as well as the actual data access methods (at least
44  * the basic <code>add</code>, <code>contains</code>, and <code>remove</code>
45  * methods).
46  *
47  * @see com.sosnoski.util.PrimitiveHashBase
48  * @author Dennis M. Sosnoski
49  * @version 1.0
50  */

51
52 public abstract class PrimitiveSetBase extends PrimitiveHashBase
53 {
54     /**
55      * Constructor with full specification.
56      *
57      * @param count number of values to assume in initial sizing of table
58      * @param fill fraction full allowed for table before growing
59      * @param ktype type of primitives used for keys
60      */

61     
62     public PrimitiveSetBase(int count, double fill, Class JavaDoc ktype) {
63         super(count, fill, ktype);
64     }
65
66     /**
67      * Copy (clone) constructor.
68      *
69      * @param base instance being copied
70      */

71     
72     public PrimitiveSetBase(PrimitiveSetBase base) {
73         super(base);
74     }
75
76     /**
77      * Restructure the table. This abstract method is used when the table
78      * is increasing or decreasing in size, and works directly with the old
79      * table representation arrays. It should insert keys from the old array
80      * directly into the table without adjusting the count present or checking
81      * the table size.
82      *
83      * @param flags array of flags for array slots used
84      * @param karray array of keys
85      */

86     
87     protected abstract void restructure(boolean[] flags, Object JavaDoc karray);
88
89     /**
90      * Resize the base arrays after a size change. This implementation of the
91      * abstract base class method allocates the new arrays and then calls
92      * another method for handling the actual transfer of the key set from the
93      * old arrays to the new ones.
94      *
95      * @param size new size for base arrays
96      */

97     
98     protected void reallocate(int size) {
99     
100         // allocate the larger arrays
101
boolean[] flags = m_flagTable;
102         m_flagTable = new boolean[size];
103         Object JavaDoc keys = getKeyArray();;
104         Class JavaDoc type = keys.getClass().getComponentType();
105         setKeyArray(Array.newInstance(type, size));
106         
107         // reinsert all entries into new arrays
108
restructure(flags, keys);
109     }
110 }
111
Popular Tags