KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > math > Base27CoderLowerCase


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on May 18, 2005
33  */

34 package com.nightlabs.math;
35
36 /**
37  * @author Marco Schulze - marco at nightlabs dot de
38  */

39 public class Base27CoderLowerCase
40 extends BaseNCoder
41 {
42     protected boolean skip0;
43
44     public Base27CoderLowerCase(boolean skip0)
45     {
46         this.skip0 = skip0;
47
48         char[] symbols = new char[27];
49         symbols[0] = '0';
50         char c = 'a';
51         for (int i = 1; i < symbols.length; ++i) {
52             symbols[i] = c++;
53         }
54
55         init(symbols);
56     }
57
58     /**
59      * @see com.nightlabs.math.BaseNCoder#encode(long, int)
60      */

61     public String JavaDoc encode(long number, int minDigitCount)
62     {
63         if (skip0) {
64             int base = getBase();
65             int period = base - 1;
66
67             long n = number;
68             long m = number + 1;
69
70             int digitIdx = 0;
71             while (n >= period) {
72                 long divisor = (long)Math.pow(period, digitIdx + 1);
73                 long f = n / divisor;
74                 n = n - divisor;
75                 m = m + (long)Math.pow(base, digitIdx) * f;
76                 ++digitIdx;
77             }
78             number = m;
79         }
80
81         return super.encode(number, minDigitCount);
82     }
83
84     /**
85      * @see com.nightlabs.math.BaseNCoder#decode(java.lang.String)
86      */

87     public long decode(String JavaDoc s)
88     {
89         long number = super.decode(s);
90
91         if (skip0) {
92             // We use interval finding mechanism. If someone finds a better inverse function
93
long a = 0;
94             long b = number;
95             long middle = b / 2;
96             long v = super.decode(encode(middle, 1));
97             while (v != number) {
98                 if (v > number)
99                     b = middle;
100                 else
101                     a = middle;
102
103                 middle = (b - a) / 2 + a;
104
105                 v = super.decode(encode(middle, 1));
106             }
107
108             number = middle;
109         }
110
111         return number;
112     }
113
114     public static void main(String JavaDoc[] args)
115     {
116         Base27CoderLowerCase b = new Base27CoderLowerCase(true);
117
118         for (int i = 0; i < 1000; ++i) {
119             System.out.print("" + i);
120             String JavaDoc s = b.encode(i, 3);
121             long m = b.decode(s);
122 // System.out.println("\t" + s + "\t" + m + "\t" );
123
String JavaDoc d;
124             if (s.startsWith("a"))
125                 d = s.substring(1);
126             else
127                 d = s;
128             
129             if (d.length() > 0) {
130                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(d);
131                 for (int c = d.length() - 1; c >= 0; --c) {
132                     if (d.charAt(c) == 'a')
133                         sb.replace(c, c+1, "1");
134                     else
135                         break;
136                 }
137                 d = sb.toString().replaceAll("[^1]", "");
138             }
139             
140 // String d = s.replaceAll("^a", "").replaceAll("a", "1").replaceAll("[^1]", "");
141
long back = b.decode(s);
142             System.out.println("\t" + s + "\t" + back + "\t" + (back - i) + "\t" + d);
143 // System.out.println("\t" + s + "\t" + m + "\t" + Double.toString(((double)m - i) / Math.pow(3, s.length())));
144
// System.out.println("\t" + s + "\t" + m + "\t" + (i == 0 ? "" : Double.toString((double)m / i)));
145
}
146     }
147 }
148
Popular Tags