| author | Raphael Velt <raph.velt@gmail.com> |
| Thu, 06 Oct 2011 12:39:29 +0200 | |
| changeset 306 | 70c9688a1486 |
| parent 305 | tweetcast/client/lib/gimite-web-socket-js-55ae639/flash-src/third-party/com/hurlant/math/BarrettReduction.as@436a31d11f1d |
| permissions | -rwxr-xr-x |
|
305
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
1 |
package com.hurlant.math |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
2 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
3 |
use namespace bi_internal; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
4 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
5 |
internal class BarrettReduction implements IReduction |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
6 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
7 |
private var m:BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
8 |
private var r2:BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
9 |
private var q3:BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
10 |
private var mu:BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
11 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
12 |
public function BarrettReduction(m:BigInteger) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
13 |
// setup Barrett |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
14 |
r2 = new BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
15 |
q3 = new BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
16 |
BigInteger.ONE.dlShiftTo(2*m.t, r2); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
17 |
mu = r2.divide(m); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
18 |
this.m = m; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
19 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
20 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
21 |
public function revert(x:BigInteger):BigInteger |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
22 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
23 |
return x; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
24 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
25 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
26 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
27 |
* |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
28 |
* @param x |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
29 |
* @param y |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
30 |
* @param r = x*y mod m; x != r |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
31 |
* |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
32 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
33 |
public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
34 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
35 |
x.multiplyTo(y, r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
36 |
reduce(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
37 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
38 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
39 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
40 |
* |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
41 |
* @param x |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
42 |
* @param r = x^2 mod m; x != r |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
43 |
* |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
44 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
45 |
public function sqrTo(x:BigInteger, r:BigInteger):void |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
46 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
47 |
x.squareTo(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
48 |
reduce(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
49 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
50 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
51 |
public function convert(x:BigInteger):BigInteger |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
52 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
53 |
if (x.s<0 || x.t>2*m.t) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
54 |
return x.mod(m); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
55 |
} else if (x.compareTo(m)<0) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
56 |
return x; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
57 |
} else { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
58 |
var r:BigInteger = new BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
59 |
x.copyTo(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
60 |
reduce(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
61 |
return r; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
62 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
63 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
64 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
65 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
66 |
* |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
67 |
* @param x = x mod m (HAC 14.42) |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
68 |
* |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
69 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
70 |
public function reduce(lx:BigInteger):void |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
71 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
72 |
var x:BigInteger = lx as BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
73 |
x.drShiftTo(m.t-1,r2); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
74 |
if (x.t>m.t+1) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
75 |
x.t = m.t+1; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
76 |
x.clamp(); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
77 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
78 |
mu.multiplyUpperTo(r2, m.t+1, q3); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
79 |
m.multiplyLowerTo(q3, m.t+1, r2); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
80 |
while (x.compareTo(r2)<0) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
81 |
x.dAddOffset(1, m.t+1); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
82 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
83 |
x.subTo(r2,x); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
84 |
while (x.compareTo(m)>=0) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
85 |
x.subTo(m,x); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
86 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
87 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
88 |
|
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
89 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
90 |
} |