Path: blob/master/src/java.base/share/classes/sun/text/BreakDictionary.java
41152 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*27* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved28* (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved29*30* The original version of this source code and documentation31* is copyrighted and owned by Taligent, Inc., a wholly-owned32* subsidiary of IBM. These materials are provided under terms33* of a License Agreement between Taligent and Sun. This technology34* is protected by multiple US and International patents.35*36* This notice and attribution to Taligent may not be removed.37* Taligent is a registered trademark of Taligent, Inc.38*/39package sun.text;4041import java.nio.BufferUnderflowException;42import java.nio.ByteBuffer;43import java.util.MissingResourceException;44import sun.text.CompactByteArray;45import sun.text.SupplementaryCharacterData;4647/**48* This is the class that represents the list of known words used by49* DictionaryBasedBreakIterator. The conceptual data structure used50* here is a trie: there is a node hanging off the root node for every51* letter that can start a word. Each of these nodes has a node hanging52* off of it for every letter that can be the second letter of a word53* if this node is the first letter, and so on. The trie is represented54* as a two-dimensional array that can be treated as a table of state55* transitions. Indexes are used to compress this array, taking56* advantage of the fact that this array will always be very sparse.57*/58class BreakDictionary {5960//=========================================================================61// data members62//=========================================================================6364/**65* The version of the dictionary that was read in.66*/67private static int supportedVersion = 1;6869/**70* Maps from characters to column numbers. The main use of this is to71* avoid making room in the array for empty columns.72*/73private CompactByteArray columnMap = null;74private SupplementaryCharacterData supplementaryCharColumnMap = null;7576/**77* The number of actual columns in the table78*/79private int numCols;8081/**82* Columns are organized into groups of 32. This says how many83* column groups. (We could calculate this, but we store the84* value to avoid having to repeatedly calculate it.)85*/86private int numColGroups;8788/**89* The actual compressed state table. Each conceptual row represents90* a state, and the cells in it contain the row numbers of the states91* to transition to for each possible letter. 0 is used to indicate92* an illegal combination of letters (i.e., the error state). The93* table is compressed by eliminating all the unpopulated (i.e., zero)94* cells. Multiple conceptual rows can then be doubled up in a single95* physical row by sliding them up and possibly shifting them to one96* side or the other so the populated cells don't collide. Indexes97* are used to identify unpopulated cells and to locate populated cells.98*/99private short[] table = null;100101/**102* This index maps logical row numbers to physical row numbers103*/104private short[] rowIndex = null;105106/**107* A bitmap is used to tell which cells in the comceptual table are108* populated. This array contains all the unique bit combinations109* in that bitmap. If the table is more than 32 columns wide,110* successive entries in this array are used for a single row.111*/112private int[] rowIndexFlags = null;113114/**115* This index maps from a logical row number into the bitmap table above.116* (This keeps us from storing duplicate bitmap combinations.) Since there117* are a lot of rows with only one populated cell, instead of wasting space118* in the bitmap table, we just store a negative number in this index for119* rows with one populated cell. The absolute value of that number is120* the column number of the populated cell.121*/122private short[] rowIndexFlagsIndex = null;123124/**125* For each logical row, this index contains a constant that is added to126* the logical column number to get the physical column number127*/128private byte[] rowIndexShifts = null;129130//=========================================================================131// deserialization132//=========================================================================133134BreakDictionary(String dictionaryName, byte[] dictionaryData) {135try {136setupDictionary(dictionaryName, dictionaryData);137} catch (BufferUnderflowException bue) {138MissingResourceException e;139e = new MissingResourceException("Corrupted dictionary data",140dictionaryName, "");141e.initCause(bue);142throw e;143}144}145146private void setupDictionary(String dictionaryName, byte[] dictionaryData) {147ByteBuffer bb = ByteBuffer.wrap(dictionaryData);148149// check version150int version = bb.getInt();151if (version != supportedVersion) {152throw new MissingResourceException("Dictionary version(" + version + ") is unsupported",153dictionaryName, "");154}155156// Check data size157int len = bb.getInt();158if (bb.position() + len != bb.limit()) {159throw new MissingResourceException("Dictionary size is wrong: " + bb.limit(),160dictionaryName, "");161}162163// read in the column map for BMP characteres (this is serialized in164// its internal form: an index array followed by a data array)165len = bb.getInt();166short[] temp = new short[len];167for (int i = 0; i < len; i++) {168temp[i] = bb.getShort();169}170len = bb.getInt();171byte[] temp2 = new byte[len];172bb.get(temp2);173columnMap = new CompactByteArray(temp, temp2);174175// read in numCols and numColGroups176numCols = bb.getInt();177numColGroups = bb.getInt();178179// read in the row-number index180len = bb.getInt();181rowIndex = new short[len];182for (int i = 0; i < len; i++) {183rowIndex[i] = bb.getShort();184}185186// load in the populated-cells bitmap: index first, then bitmap list187len = bb.getInt();188rowIndexFlagsIndex = new short[len];189for (int i = 0; i < len; i++) {190rowIndexFlagsIndex[i] = bb.getShort();191}192len = bb.getInt();193rowIndexFlags = new int[len];194for (int i = 0; i < len; i++) {195rowIndexFlags[i] = bb.getInt();196}197198// load in the row-shift index199len = bb.getInt();200rowIndexShifts = new byte[len];201bb.get(rowIndexShifts);202203// load in the actual state table204len = bb.getInt();205table = new short[len];206for (int i = 0; i < len; i++) {207table[i] = bb.getShort();208}209210// finally, prepare the column map for supplementary characters211len = bb.getInt();212int[] temp3 = new int[len];213for (int i = 0; i < len; i++) {214temp3[i] = bb.getInt();215}216assert bb.position() == bb.limit();217218supplementaryCharColumnMap = new SupplementaryCharacterData(temp3);219}220221//=========================================================================222// access to the words223//=========================================================================224225/**226* Uses the column map to map the character to a column number, then227* passes the row and column number to getNextState()228* @param row The current state229* @param ch The character whose column we're interested in230* @return The new state to transition to231*/232public final short getNextStateFromCharacter(int row, int ch) {233int col;234if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {235col = columnMap.elementAt((char)ch);236} else {237col = supplementaryCharColumnMap.getValue(ch);238}239return getNextState(row, col);240}241242/**243* Returns the value in the cell with the specified (logical) row and244* column numbers. In DictionaryBasedBreakIterator, the row number is245* a state number, the column number is an input, and the return value246* is the row number of the new state to transition to. (0 is the247* "error" state, and -1 is the "end of word" state in a dictionary)248* @param row The row number of the current state249* @param col The column number of the input character (0 means "not a250* dictionary character")251* @return The row number of the new state to transition to252*/253public final short getNextState(int row, int col) {254if (cellIsPopulated(row, col)) {255// we map from logical to physical row number by looking up the256// mapping in rowIndex; we map from logical column number to257// physical column number by looking up a shift value for this258// logical row and offsetting the logical column number by259// the shift amount. Then we can use internalAt() to actually260// get the value out of the table.261return internalAt(rowIndex[row], col + rowIndexShifts[row]);262}263else {264return 0;265}266}267268/**269* Given (logical) row and column numbers, returns true if the270* cell in that position is populated271*/272private boolean cellIsPopulated(int row, int col) {273// look up the entry in the bitmap index for the specified row.274// If it's a negative number, it's the column number of the only275// populated cell in the row276if (rowIndexFlagsIndex[row] < 0) {277return col == -rowIndexFlagsIndex[row];278}279280// if it's a positive number, it's the offset of an entry in the bitmap281// list. If the table is more than 32 columns wide, the bitmap is stored282// successive entries in the bitmap list, so we have to divide the column283// number by 32 and offset the number we got out of the index by the result.284// Once we have the appropriate piece of the bitmap, test the appropriate285// bit and return the result.286else {287int flags = rowIndexFlags[rowIndexFlagsIndex[row] + (col >> 5)];288return (flags & (1 << (col & 0x1f))) != 0;289}290}291292/**293* Implementation of getNextState() when we know the specified cell is294* populated.295* @param row The PHYSICAL row number of the cell296* @param col The PHYSICAL column number of the cell297* @return The value stored in the cell298*/299private short internalAt(int row, int col) {300// the table is a one-dimensional array, so this just does the math necessary301// to treat it as a two-dimensional array (we don't just use a two-dimensional302// array because two-dimensional arrays are inefficient in Java)303return table[row * numCols + col];304}305}306307308