Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / com / stevesoft / pat / FastBracket.js
1 Clazz.declarePackage ("com.stevesoft.pat");
2 Clazz.load (["com.stevesoft.pat.Bracket"], "com.stevesoft.pat.FastBracket", ["com.stevesoft.pat.Range", "$.oneChar", "java.util.BitSet", "$.Vector"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.$min = 0;
5 this.$max = 0;
6 this.bs = null;
7 Clazz.instantialize (this, arguments);
8 }, com.stevesoft.pat, "FastBracket", com.stevesoft.pat.Bracket);
9 c$.process = Clazz.defineMethod (c$, "process", 
10 function (b, ignc) {
11 var v = b.v;
12 b.pv = null;
13 try {
14 var nv = v;
15 if (ignc) {
16 nv =  new java.util.Vector ();
17 for (var i = 0; i < v.size (); i++) {
18 var p = v.elementAt (i);
19 nv.addElement (p);
20 if (Clazz.instanceOf (p, com.stevesoft.pat.oneChar)) {
21 var oc = p;
22 nv.addElement ( new com.stevesoft.pat.oneChar (oc.altc));
23 } else if (Clazz.instanceOf (p, com.stevesoft.pat.Range)) {
24 var ra = p;
25 nv.addElement ( new com.stevesoft.pat.Range (ra.altlo, ra.althi));
26 }}
27 }v = nv;
28 for (var i = 0; i < v.size () - 1; i++) {
29 for (var j = 0; j < v.size () - 1; j++) {
30 var c1 = com.stevesoft.pat.FastBracket.getl (v.elementAt (j));
31 var c2 = com.stevesoft.pat.FastBracket.getl (v.elementAt (j + 1));
32 if (c2 < c1) {
33 var o = v.elementAt (j);
34 v.setElementAt (v.elementAt (j + 1), j);
35 v.setElementAt (o, j + 1);
36 }}
37 }
38 nv =  new java.util.Vector ();
39 var p = v.elementAt (0);
40 nv.addElement (p);
41 for (var i = 1; i < v.size (); i++) {
42 if ((com.stevesoft.pat.FastBracket.geth (p)).charCodeAt (0) + 1 >= (com.stevesoft.pat.FastBracket.getl (v.elementAt (i))).charCodeAt (0)) {
43 var p2 = v.elementAt (i);
44 var lo = com.stevesoft.pat.FastBracket.min (com.stevesoft.pat.FastBracket.getl (p), com.stevesoft.pat.FastBracket.getl (p2));
45 var hi = com.stevesoft.pat.FastBracket.max (com.stevesoft.pat.FastBracket.geth (p), com.stevesoft.pat.FastBracket.geth (p2));
46 nv.setElementAt (p = com.stevesoft.pat.FastBracket.mkelem (lo, hi), nv.size () - 1);
47 } else {
48 p = v.elementAt (i);
49 nv.addElement (p);
50 }}
51 b.v = v = nv;
52 } catch (e) {
53 if (Clazz.exceptionOf (e, com.stevesoft.pat.RegSyntax)) {
54 e.printStackTrace ();
55 } else {
56 throw e;
57 }
58 }
59 var negv = com.stevesoft.pat.FastBracket.neg (v);
60 if (v.size () == 1) {
61 return b;
62 }if (negv.size () == 1) {
63 b.v = negv;
64 b.neg = !b.neg;
65 return b;
66 }var fb = com.stevesoft.pat.FastBracket.newbrack (v, b.neg);
67 if (fb == null) {
68 fb = com.stevesoft.pat.FastBracket.newbrack (negv, !b.neg);
69 }if (fb != null) {
70 fb.parent = b.parent;
71 fb.next = b.next;
72 return fb;
73 }return b;
74 }, "com.stevesoft.pat.Bracket,~B");
75 c$.newbrack = Clazz.defineMethod (c$, "newbrack", 
76 function (v, neg) {
77 var fb =  new com.stevesoft.pat.FastBracket (neg);
78 fb.v = v;
79 if (v.size () == 0) {
80 return null;
81 }fb.$min = (com.stevesoft.pat.FastBracket.getl (v.elementAt (0))).charCodeAt (0);
82 fb.$max = (com.stevesoft.pat.FastBracket.geth (v.elementAt (v.size () - 1))).charCodeAt (0);
83 if (fb.$max - fb.$min <= 256) {
84 fb.bs =  new java.util.BitSet (fb.$max - fb.$min + 1);
85 for (var i = 0; i < v.size (); i++) {
86 var o = v.elementAt (i);
87 var min0 = (com.stevesoft.pat.FastBracket.getl (o)).charCodeAt (0) - fb.$min;
88 var max0 = (com.stevesoft.pat.FastBracket.geth (o)).charCodeAt (0) - fb.$min;
89 for (var j = min0; j <= max0; j++) {
90 fb.bs.set (j);
91 }
92 }
93 return fb;
94 }return null;
95 }, "java.util.Vector,~B");
96 c$.neg = Clazz.defineMethod (c$, "neg", 
97 function (v) {
98 try {
99 var nv =  new java.util.Vector ();
100 if (v.size () == 0) {
101 nv.addElement ( new com.stevesoft.pat.Range (String.fromCharCode (0), String.fromCharCode (65535)));
102 return nv;
103 }var p0 = (com.stevesoft.pat.FastBracket.getl (v.elementAt (0))).charCodeAt (0);
104 if (p0 != 0) {
105 nv.addElement (com.stevesoft.pat.FastBracket.mkelem (String.fromCharCode (0), String.fromCharCode (p0 - 1)));
106 }for (var i = 0; i < v.size () - 1; i++) {
107 var hi = (com.stevesoft.pat.FastBracket.getl (v.elementAt (i + 1))).charCodeAt (0) - 1;
108 var lo = (com.stevesoft.pat.FastBracket.geth (v.elementAt (i))).charCodeAt (0) + 1;
109 nv.addElement (com.stevesoft.pat.FastBracket.mkelem (String.fromCharCode (lo), String.fromCharCode (hi)));
110 }
111 var pN = (com.stevesoft.pat.FastBracket.geth (v.lastElement ())).charCodeAt (0);
112 if (pN != 65535) {
113 nv.addElement (com.stevesoft.pat.FastBracket.mkelem (String.fromCharCode (pN + 1), String.fromCharCode (65535)));
114 }return nv;
115 } catch (rs) {
116 if (Clazz.exceptionOf (rs, com.stevesoft.pat.RegSyntax)) {
117 return null;
118 } else {
119 throw rs;
120 }
121 }
122 }, "java.util.Vector");
123 c$.mkelem = Clazz.defineMethod (c$, "mkelem", 
124 function (lo, hi) {
125 return lo == hi ? ( new com.stevesoft.pat.oneChar (lo)) : ( new com.stevesoft.pat.Range (lo, hi));
126 }, "~S,~S");
127 c$.min = Clazz.defineMethod (c$, "min", 
128 function (a, b) {
129 return a < b ? a : b;
130 }, "~S,~S");
131 c$.max = Clazz.defineMethod (c$, "max", 
132 function (a, b) {
133 return a > b ? a : b;
134 }, "~S,~S");
135 c$.getl = Clazz.defineMethod (c$, "getl", 
136 function (o) {
137 var p = o;
138 if (Clazz.instanceOf (p, com.stevesoft.pat.Range)) {
139 return (p).lo;
140 }return (p).c;
141 }, "~O");
142 c$.geth = Clazz.defineMethod (c$, "geth", 
143 function (o) {
144 var p = o;
145 if (Clazz.instanceOf (p, com.stevesoft.pat.Range)) {
146 return (p).hi;
147 }return (p).c;
148 }, "~O");
149 Clazz.overrideMethod (c$, "matchInternal", 
150 function (pos, pt) {
151 if (pos >= pt.src.length () || this.Masked (pos, pt)) {
152 return -1;
153 }var c = pt.src.charAt (pos);
154 return ( new Boolean (this.neg ^ (c.charCodeAt (0) >= this.$min && c.charCodeAt (0) <= this.$max && this.bs.get (c.charCodeAt (0) - this.$min))).valueOf ()) ? this.nextMatch (pos + 1, pt) : -1;
155 }, "~N,com.stevesoft.pat.Pthings");
156 });