Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / giop / grmic / HashFinder.java
blob2efdb1e76de4a8f4acc35fbb10f3d03007173e9c
1 /* HashFinder.java -- finds the hash character.
2 Copyright (C) 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
23 package gnu.classpath.tools.giop.grmic;
25 import java.util.HashSet;
27 /**
28 * This class finds the hash character (the most different character in
29 * the passed array of strings). This character is used to accelerate the
30 * method invocation by name.
32 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
34 public class HashFinder
36 /**
37 * Find the hash char position in the given collection of strings.
39 * @param strings the string collection
41 * @return the optimal hash character position, always less then the
42 * length of the shortest string.
44 public int findHashCharPosition(String[] strings)
46 // Find the length of the shortest string:
48 int l = strings[0].length();
49 for (int i = 1; i < strings.length; i++)
51 if (strings[i].length() < l)
52 l = strings[i].length();
55 // Find the position with the smallest number of the matching characters:
56 HashSet[] charLists = new HashSet[l];
58 for (int i = 0; i < charLists.length; i++)
60 charLists[i] = new HashSet(strings.length);
63 for (int i = 0; i < strings.length; i++)
64 for (int p = 0; p < l; p++)
66 charLists[p].add(new Integer(strings[i].charAt(p)));
69 int m = 0;
70 int v = charLists[0].size();
72 for (int i = 1; i < charLists.length; i++)
74 // Replace on equality also, seeking the hash char closer to the end
75 // of line.
76 if (charLists[i].size()>=v)
78 m = i;
79 v = charLists[i].size();
82 return m;