![]() |
Quadcap Embeddable Server |
00001 package com.quadcap.util.collections; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Iterator; 00042 00043 /** 00044 * A map with integer keys 00045 * 00046 * @author Stan Bailes 00047 */ 00048 public class IntMap { 00049 int size; 00050 Entry[] entries; 00051 Entry freeList; 00052 00053 public IntMap(int initSize) { 00054 this.size = initSize | 1; 00055 while (!isPrime(size)) size += 2; 00056 entries = new Entry[size]; 00057 } 00058 00059 public final Object get(int key) { 00060 int h = hash(key); 00061 for (Entry entry = entries[h]; entry != null; entry = entry.next) { 00062 if (entry.key == key) return entry.val; 00063 } 00064 return null; 00065 } 00066 00067 public final void put(int key, Object val) { 00068 int h = hash(key); 00069 Entry entry = getEntry(key, val); 00070 entry.next = entries[h]; 00071 entries[h] = entry; 00072 } 00073 00074 public final void remove(int key) { 00075 int h = hash(key); 00076 Entry prev = null; 00077 for (Entry entry = entries[h]; entry != null; entry = entry.next) { 00078 if (entry.key == key) { 00079 if (prev == null) { 00080 entries[h] = entry.next; 00081 } else { 00082 prev.next = entry.next; 00083 } 00084 freeEntry(entry); 00085 } 00086 prev = entry; 00087 } 00088 } 00089 00090 final Entry getEntry(int key, Object val) { 00091 Entry entry = freeList; 00092 if (entry == null) { 00093 entry = new Entry(); 00094 } else { 00095 freeList = entry.next; 00096 } 00097 entry.key = key; 00098 entry.val = val; 00099 return entry; 00100 } 00101 00102 final void freeEntry(Entry entry) { 00103 entry.next = freeList; 00104 freeList = entry; 00105 } 00106 00107 public final static boolean isPrime(int x) { 00108 if ((x & 1) == 0) return false; 00109 for (int i = 3; i*i <= x; i += 2) { 00110 if ((x % i) == 0) return false; 00111 } 00112 return true; 00113 } 00114 00115 public Iterator keys() { 00116 return new IntMapIterator(this); 00117 } 00118 00119 final int hash(int key) { 00120 int h = key % size; 00121 if (h < 0) { 00122 h = 0 - h; 00123 } 00124 return h; 00125 } 00126 00127 class Entry { 00128 int key; 00129 Object val; 00130 Entry next; 00131 } 00132 00133 class IntMapIterator implements Iterator { 00134 IntMap map; 00135 int epos = 0; 00136 Entry entry = null; 00137 00138 public IntMapIterator(IntMap map) { this.map = map; } 00139 00140 public boolean hasNext() { 00141 while (epos < map.size && entry == null) { 00142 entry = map.entries[epos++]; 00143 } 00144 return entry != null; 00145 } 00146 00147 public Object next() { 00148 Integer ret = null; 00149 if (hasNext()) { 00150 ret = new Integer(entry.key); 00151 entry = entry.next; 00152 } 00153 return ret; 00154 } 00155 00156 public void remove() {} 00157 } 00158 00159 }