001 /* 002 NGramJ - n-gram based text classification 003 Copyright (C) 2001- Frank S. Nestel (frank at spieleck.de) 004 005 This program is free software; you can redistribute it and/or modify 006 it under the terms of the GNU Lesser General Public License as published 007 by the Free Software Foundation; either version 2.1 of the License, or 008 (at your option) any later version. 009 010 This program is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 GNU General Public License for more details. 014 015 You should have received a copy of the GNU Lesser General Public License 016 along with this program (lesser.txt); if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 018 */ 019 package de.spieleck.app.cngram; 020 021 /** 022 * A very light (and therefore fast, efficient) implementation 023 * of a CharSequence. Tailored for Ngram purpose. 024 * @author frank nestel 025 * @author $Author: nestefan $ 026 * @version $Revision: 2 $ $Date: 2006-03-27 23:00:21 +0200 (Mo, 27 Mrz 2006) $ $Author: nestefan $ 027 */ 028 public class LightCharSequence 029 implements CharSequence 030 { 031 private char[] chars; 032 033 private int hashCode; 034 035 public LightCharSequence(CharSequence seq) 036 { 037 chars = new char[seq.length()]; 038 for(int i = 0; i < chars.length; i++) 039 chars[i] = seq.charAt(i); 040 calcHashCode(); 041 } 042 043 public LightCharSequence(CharSequence seq, int start, int end) 044 { 045 chars = new char[end - start]; 046 for(int i = start; i < end; i++) 047 chars[i] = seq.charAt(i - start); 048 calcHashCode(); 049 } 050 051 protected void calcHashCode() 052 { 053 hashCode = 42 + length(); 054 for(int i = 0; i < chars.length; i++) 055 hashCode = hashCode * 0x110001 + charAt(i) + 1; 056 } 057 058 public char charAt(int pos) 059 { 060 return chars[pos]; 061 } 062 063 public int length() 064 { 065 return chars.length; 066 } 067 068 public CharSequence subSequence(int start, int end) 069 { 070 return new LightCharSequence(this, start, end); 071 } 072 073 public String toString() 074 { 075 return new String(chars); 076 } 077 078 public boolean equals(Object o) 079 { 080 if ( !(o instanceof LightCharSequence) ) 081 return false; 082 LightCharSequence l = (LightCharSequence)o; 083 if ( l.length() != length() ) 084 return false; 085 for(int i = 0; i < length(); i++) 086 if ( l.charAt(i) != charAt(i) ) 087 return false; 088 return true; 089 } 090 091 public int hashCode() 092 { 093 return hashCode; 094 } 095 } 096