1 50 51 package org.openlaszlo.iv.flash.util; 52 53 import java.io.*; 54 import java.util.*; 55 56 import org.openlaszlo.iv.flash.api.*; 57 58 63 public final class IVMap { 64 65 private static final int DEFAULT_SIZE = 257; 66 private static final float DEFAULT_RATIO = 0.75f; 67 68 private MyNode[] buckets; 69 private int size; 70 private int limit; 71 private int length; 72 73 76 public IVMap() { 77 clear(); 78 } 79 80 85 public void add( FlashDef def ) { 86 put( def.getID(), def ); 87 } 88 89 95 public void put( int key, FlashDef def ) { 96 int probe = key % length; 97 MyNode newNode = new MyNode( key, def ); 98 newNode.next = buckets[probe]; 99 buckets[probe] = newNode; 100 if( size++ > limit ) expand(); 101 } 102 103 109 public FlashDef get( int key ) { 110 for( MyNode node = buckets[key%length]; node != null; node = node.next ) { 111 if( key == node.key ) return node.value; 112 } 113 return null; 114 } 115 116 121 public int size() { 122 return size; 123 } 124 125 130 public Enumeration values() { 131 return new Enumeration() { 132 int cur = 0; 133 MyNode node = null; 134 public boolean hasMoreElements() { 135 while( node == null ) { 136 if( cur >= length ) return false; 137 node = buckets[cur++]; 138 } 139 return true; 140 } 141 public Object nextElement() { 142 if( !hasMoreElements() ) return null; 143 Object value = node.value; 144 node = node.next; 145 return value; 146 } 147 }; 148 } 149 150 153 public void clear() { 154 length = DEFAULT_SIZE; 155 buckets = new MyNode[length]; 156 size = 0; 157 limit = (int) (length*DEFAULT_RATIO); 158 } 159 160 protected void expand() { 161 length = length*2+1; 162 MyNode[] new_bucket = new MyNode[length]; 163 for( int i=buckets.length; --i>=0; ) { 164 MyNode node = buckets[i]; 165 while( node != null ) { 166 MyNode cnode = node; 167 node = node.next; 168 int probe = cnode.key % length; 169 cnode.next = new_bucket[probe]; 170 new_bucket[probe] = cnode; 171 } 172 } 173 limit = (int) (length*DEFAULT_RATIO); 174 buckets = new_bucket; 175 } 176 177 static class MyNode { 178 public int key; 179 public FlashDef value; 180 public MyNode next; 181 182 public MyNode( int key, FlashDef value ) { 183 this.key = key;; 184 this.value = value; 185 } 186 } 187 188 } 189 | Popular Tags |