This really should've been an array of unboxed ints. Ugh. :)

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@201 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Robert Zubeck
2007-02-20 01:34:06 +00:00
parent 08618a4d74
commit cfed094aa3
@@ -34,6 +34,7 @@ import java.io.File;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random; import java.util.Random;
@@ -67,15 +68,12 @@ public class DictionaryManager
*/ */
public Dictionary (File wordfile) public Dictionary (File wordfile)
{ {
ArrayList <Integer> list = new ArrayList <Integer> ();
try try
{ {
if (wordfile.exists() && wordfile.isFile() && wordfile.canRead()) if (wordfile.exists() && wordfile.isFile() && wordfile.canRead())
{ {
// File length will give us a rough starting point for the word array
long bytecount = wordfile.length();
int approxWords = (int) (bytecount / 5); // just a heuristic,
_hashes.ensureCapacity (approxWords); // we'll trim the vector later
// Read it line by line // Read it line by line
BufferedReader reader = new BufferedReader (new FileReader (wordfile)); BufferedReader reader = new BufferedReader (new FileReader (wordfile));
String line = null; String line = null;
@@ -83,7 +81,7 @@ public class DictionaryManager
{ {
// Add the word to the dictionary // Add the word to the dictionary
int hash = hashWord (line); int hash = hashWord (line);
_hashes.add (hash); list.add (hash);
// Add each letter to a letter set // Add each letter to a letter set
int len = line.length (); int len = line.length ();
@@ -99,16 +97,17 @@ public class DictionaryManager
_letters.put (ch, _letters.get (ch) + 1); _letters.put (ch, _letters.get (ch) + 1);
} }
} }
} }
// Trim and sort the vector // Now dump it into a tighter, unboxed, sorted array.
_hashes.trimToSize(); _hashes = new int[list.size()];
Collections.sort (_hashes); int i = 0;
for (Integer value : list) { _hashes[i++] = (int) value; }
Arrays.sort (_hashes);
log.log (Level.INFO, log.log (Level.INFO,
"Loaded dictionary file " + wordfile.getName () + "Loaded dictionary file " + wordfile.getName () +
" with " + _hashes.size () + " entries, " + " with " + _hashes.length + " entries, " +
_letters.size () + " letters."); _letters.size () + " letters.");
} }
@@ -121,7 +120,7 @@ public class DictionaryManager
catch (Exception ex) catch (Exception ex)
{ {
log.log (Level.WARNING, "Failed to load dictionary file", ex); log.log (Level.WARNING, "Failed to load dictionary file", ex);
_hashes.clear(); // dump everything _hashes = new int[] { }; // dump everything
} }
} }
@@ -130,7 +129,7 @@ public class DictionaryManager
{ {
// Hash the word and check // Hash the word and check
int hash = hashWord (word); int hash = hashWord (word);
int result = Collections.binarySearch (_hashes, hash); int result = Arrays.binarySearch (_hashes, hash);
return (result >= 0); return (result >= 0);
} }
@@ -163,8 +162,8 @@ public class DictionaryManager
// PRIVATE STORAGE // PRIVATE STORAGE
/** Sorted vector of word hashes */ /** Sorted array of word hashes */
private Vector <Integer> _hashes = new Vector <Integer> (); private int[] _hashes;
/** Mapping from letters in this language to their total count */ /** Mapping from letters in this language to their total count */
private HashMap <Character, Integer> _letters = new HashMap <Character, Integer> (); private HashMap <Character, Integer> _letters = new HashMap <Character, Integer> ();