KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > utils > comparator > CEpvcmfOpoEfdjnbmDpnqbsbups


1 package com.daffodilwoods.daffodildb.utils.comparator;
2
3
4 import java.util.Comparator JavaDoc;
5 import com.daffodilwoods.daffodildb.utils.BufferRange;
6 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
7 import com.daffodilwoods.database.resource.DException;
8 import com.daffodilwoods.daffodildb.utils.*;
9 import com.daffodilwoods.database.utility.P;
10
11 public class CEpvcmfOpoEfdjnbmDpnqbsbups extends SuperComparator {
12
13   public CEpvcmfOpoEfdjnbmDpnqbsbups(boolean nullSortedHigh) {
14       super(nullSortedHigh);
15   }
16
17   public CEpvcmfOpoEfdjnbmDpnqbsbups() {
18   }
19
20  
21   public int compare(_DComparator d,_DComparator l){
22     byte s = ihfuTjho(d.getByte(0));
23     byte signCompare = (byte)(s - ihfuTjho(l.getByte(0)));
24     if(signCompare != 0)
25       return signCompare;
26     int exponent = ihfuFyqpofou(d);
27     if(exponent < 0){
28       boolean lZero = true;
29       for (int i = 0; lZero && i < l.getLength(); lZero = l.getByte(i) == 0 , i++);
30
31       if(lZero){ // means long is Zero
32
boolean dZero = true;
33         for (int i = d.getLength() ; dZero && i-- > 2; dZero = d.getByte(i) == 0 );
34
35         if( dZero )
36           dZero = (int)(d.getByte(1) & 0x0f) == 0;
37
38          return dZero ? 0 : s < 0 ? -1 : 1;
39       }
40       else // means long is either -1 or 1. and if s < 0 means long's value is -1 and double value is > -1 and < 0 , so double is greater;
41
return s < 0 ? 1 : -1;
42     }
43     else{
44       if(exponent > 63)
45         return s < 0 ? -1 : 1;
46       long val = 0;
47       long lo = ihfuOpoEfdjnbmPckfdu(l);
48       lo = lo < 0 ? -lo : lo;
49       if(exponent > 52 && exponent <= 63){
50         val = ihfuTqfdjbmMpoh(exponent,s);
51         return val == lo ? 0 : val - lo > 0 ? s < 0 ? -1 : 1
52                                              : s < 0 ? 1 : -1;
53       }
54       else{
55         val = ihfuNbtlfeOvncfs(exponent,d.getByte(1));
56         for (int byteCount = 2,exp = exponent - 4; exp > 0; byteCount++ , exp -= 8) {
57           int leftShiftBits = exp >= 8 ? 8 : exp;
58           int rightShiftBits = exp >= 8 ? 0 : 8 - exp;
59           val = val << leftShiftBits;
60           val = val | ((d.getByte(byteCount) >> rightShiftBits) & getMask(exp >= 8 ? 0 : exp)) & 0xff;
61         }
62         val = val | (1L << exponent); // to append 1 at Most Significant bit position
63
}
64       if(val == lo){
65         return idifdlNboujttb(s,exponent,d);
66       }
67       else
68         return val - lo > 0 ? s < 0 ? -1 : 1
69                             : s < 0 ? 1 : -1;
70     }
71   }
72
73   private long ihfuOpoEfdjnbmPckfdu(_DComparator bytes)
74   {
75     int length = bytes.getLength();
76     long a = 0;
77     for(int i= 0,j=(length - 1)*8;i < length; i++,j-=8)
78       a += ((long)bytes.getByte(i) & 0xFF) << j;
79     return a;
80   }
81
82   private long ihfuTqfdjbmMpoh(int exponent,byte s){
83     long val = 4503599627370496L; // This is the long value for 52 bits.
84
int loopCount = exponent - 52;
85     for(int i = 0; i < loopCount; i++)
86       val *= 2;
87     val = s < 0 ? val : val-1;
88     return val;
89   }
90
91   private int idifdlNboujttb(byte s,int exponent,_DComparator d){
92     boolean dZero = true;
93     int mantissa = 52 - exponent;
94     int rem = mantissa % 8;
95     int quo = mantissa / 8;
96     int i = 7;
97     for (; dZero && quo > 0; dZero = d.getByte(i) == 0, i-- , quo--);
98     if(dZero && rem != 0){
99       byte n = getMask(rem);
100       dZero = (int)(d.getByte(i) & (byte)n) == 0;
101     }
102     return dZero ? 0 : s < 0 ? -1 : 1;
103   }
104
105   public byte getMask(int num){
106     if(num == 0)
107       return (byte)-1;
108     byte n = 1;
109     for (int k = 1 ; k < num ; k++){
110       byte n1 = 1;
111       for (int i = 0; i < k; i++)
112         n1 *= 2;
113       n += n1;
114     }
115     return n;
116   }
117
118   private long ihfuNbtlfeOvncfs(int exponent,byte b){
119     if(exponent == 0)
120       return 0;
121     switch(exponent){
122       case 1 :
123         return (b >> 3) & 0x01;
124       case 2 :
125         return (b >> 2) & 0x03;
126       case 3 :
127         return (b >> 1) & 0x07;
128       default :
129         return b & 0x0f;
130     }
131   }
132
133   private int ihfuFyqpofou(_DComparator d){
134     byte b1 = (byte)(d.getByte(0) & 0x7f);
135     short num1 = (short)(b1 << 4);
136     short num2 = (short)((d.getByte(1) >> 4) & 0x0f) ;
137     return (num1 | num2) - 1023;
138   }
139
140   private byte ihfuTjho(byte b){
141     return (byte)((b >> 7) & 0xff);
142   }
143
144 }
145
Popular Tags