001: // kelondroBinSearch.java
002: // -----------------------
003: // part of The Kelondro Database
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2005
007: // created 22.11.2005
008: //
009: // $LastChangedDate: 2005-09-22 22:01:26 +0200 (Thu, 22 Sep 2005) $
010: // $LastChangedRevision: 774 $
011: // $LastChangedBy: orbiter $
012: //
013: // This program is free software; you can redistribute it and/or modify
014: // it under the terms of the GNU General Public License as published by
015: // the Free Software Foundation; either version 2 of the License, or
016: // (at your option) any later version.
017: //
018: // This program is distributed in the hope that it will be useful,
019: // but WITHOUT ANY WARRANTY; without even the implied warranty of
020: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: // GNU General Public License for more details.
022: //
023: // You should have received a copy of the GNU General Public License
024: // along with this program; if not, write to the Free Software
025: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026: //
027: // Using this software in any meaning (reading, learning, copying, compiling,
028: // running) means that you agree that the Author(s) is (are) not responsible
029: // for cost, loss of data or any harm that may be caused directly or indirectly
030: // by usage of this softare or this documentation. The usage of this software
031: // is on your own risk. The installation and usage (starting/running) of this
032: // software may allow other people or application to access your computer and
033: // any attached devices and is highly dependent on the configuration of the
034: // software which must be done by the user of the software; the author(s) is
035: // (are) also not responsible for proper configuration and usage of the
036: // software, even if provoked by documentation provided together with
037: // the software.
038: //
039: // Any changes to this file according to the GPL as documented in the file
040: // gpl.txt aside this file in the shipment you received can be done to the
041: // lines that follows this copyright notice here, but changes must not be
042: // done inside the copyright notive above. A re-distribution must contain
043: // the intact and unchanged copyright notice.
044: // Contributions and changes to the program code must be marked as such.
045:
046: package de.anomic.kelondro;
047:
048: public class kelondroBinSearch {
049:
050: private byte[] chunks;
051: private int chunksize;
052: private int count;
053: private kelondroByteOrder objectOrder = new kelondroNaturalOrder(
054: true);
055:
056: public kelondroBinSearch(byte[] chunks, int chunksize) {
057: this .chunks = chunks;
058: this .chunksize = chunksize;
059: this .count = chunks.length / chunksize;
060: }
061:
062: public boolean contains(byte[] t) {
063: return contains(t, 0, this .count);
064: }
065:
066: private synchronized boolean contains(byte[] t, int beginPos,
067: int endPos) {
068: // the endPos is exclusive, beginPos is inclusive
069: // this method is synchronized to make the use of the buffer possible
070: assert t.length == this .chunksize;
071: if (beginPos >= endPos)
072: return false;
073: int pivot = (beginPos + endPos) / 2;
074: if ((pivot < 0) || (pivot >= this .count))
075: return false;
076: int c = objectOrder.compare(this .chunks,
077: pivot * this .chunksize, this .chunksize, t, 0, t.length);
078: if (c == 0)
079: return true;
080: if (c < 0) /* buffer < t */
081: return contains(t, pivot + 1, endPos);
082: if (c > 0) /* buffer > t */
083: return contains(t, beginPos, pivot);
084: return false;
085: }
086:
087: public int size() {
088: return count;
089: }
090:
091: public byte[] get(int element) {
092: byte[] a = new byte[chunksize];
093: System.arraycopy(this .chunks, element * this .chunksize, a, 0,
094: chunksize);
095: return a;
096: }
097:
098: public static void main(String[] args) {
099: String s = "4CEvsI8FRczRBo_ApRCkwfEbFLn1pIFXg39QGMgj5RHM6HpIMJq67QX3M5iQYr_LyI_5aGDaa_bYbRgJ9XnQjpmq6QkOoGWAoEaihRqhV3kItLFHjRtqauUR";
100: kelondroBinSearch bs = new kelondroBinSearch(s.getBytes(), 6);
101: for (int i = 0; i + 6 <= s.length(); i = i + 6) {
102: System.out
103: .println(s.substring(i, i + 6)
104: + ":"
105: + ((bs.contains(s.substring(i, i + 6)
106: .getBytes())) ? "drin" : "draussen"));
107: }
108: for (int i = 0; i + 7 <= s.length(); i = i + 6) {
109: System.out.println(s.substring(i + 1, i + 7)
110: + ":"
111: + ((bs.contains(s.substring(i + 1, i + 7)
112: .getBytes())) ? "drin" : "draussen"));
113: }
114: }
115: }
|