Changeset 2192
- Timestamp:
- Dec 18, 2006, 3:18:24 PM (14 years ago)
- Location:
- to-imperative/trunk/java/org/refal/plus
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
to-imperative/trunk/java/org/refal/plus/Expr.java
r2058 r2192 549 549 } 550 550 } 551 552 553 public Iterator iterator () 554 { 555 return new Iterator(); 556 } 557 558 public class Iterator implements java.util.ListIterator 559 { 560 int last; 561 int idx; 562 563 public Iterator () 564 { 565 last = start + length; 566 idx = start; 567 } 568 569 public boolean hasNext () 570 { 571 return idx < last; 572 } 573 574 public boolean hasPrevious () 575 { 576 return idx > start; 577 } 578 579 public Object next () 580 { 581 return terms[idx++]; 582 } 583 584 public int nextIndex () 585 { 586 return idx + 1; 587 } 588 589 public Object previous () 590 { 591 return terms[--idx]; 592 } 593 594 public int previousIndex () 595 { 596 return idx - 1; 597 } 598 599 public void add (Object o) { throw new UnsupportedOperationException(); } 600 public void remove () { throw new UnsupportedOperationException(); } 601 public void set (Object o) { throw new UnsupportedOperationException(); } 602 } 551 603 } -
to-imperative/trunk/java/org/refal/plus/library/Channel.java
r1963 r2192 16 16 { 17 17 Table.init(); 18 RefalRuntime.setPriorityAfter(Channel.class, java.util.TreeMap.class);18 RefalRuntime.setPriorityAfter(Channel.class, Table.class); 19 19 } 20 20 -
to-imperative/trunk/java/org/refal/plus/library/Class.java
r2024 r2192 57 57 public static boolean Table_q_ (Expr e) 58 58 { 59 return e.getLen() == 1 && e.at(0) instanceof java.util.TreeMap; 59 return e.getLen() == 1 && e.at(0) instanceof Table; 60 } 61 62 public static boolean Vector_q_ (Expr e) 63 { 64 return e.getLen() == 1 && e.at(0) instanceof Vector; 65 } 66 67 public static boolean String_q_ (Expr e) 68 { 69 return e.getLen() == 1 && e.at(0) instanceof String; 60 70 } 61 71 -
to-imperative/trunk/java/org/refal/plus/library/Convert.java
r2024 r2192 37 37 { 38 38 Character[] chars = new Character[e.getLen()]; 39 for (int i = 0; i < chars.length; i++){40 try{39 try { 40 for (int i = 0; i < chars.length; i++) { 41 41 chars[i] = new Character(Character.toLowerCase(((Character)e.at(i)).charValue())); 42 } catch (ClassCastException _) {43 throw new RefalException("Convert", "To-Lower", "Invalid argument");44 42 } 43 } catch (ClassCastException _) { 44 throw new RefalException("Convert", "To-Lower", "Invalid argument"); 45 45 } 46 46 res.assign(chars); … … 50 50 { 51 51 Character[] chars = new Character[e.getLen()]; 52 for (int i = 0; i < chars.length; i++){53 try{52 try { 53 for (int i = 0; i < chars.length; i++) { 54 54 chars[i] = new Character(Character.toUpperCase(((Character)e.at(i)).charValue())); 55 } catch (ClassCastException _) {56 throw new RefalException("Convert", "To-Upper", "Invalid argument");57 55 } 56 } catch (ClassCastException _) { 57 throw new RefalException("Convert", "To-Upper", "Invalid argument"); 58 58 } 59 59 res.assign(chars); … … 63 63 { 64 64 BigInteger[] arr = new BigInteger[e.getLen()]; 65 for (int i = 0; i < arr.length; i++){66 try{65 try { 66 for (int i = 0; i < arr.length; i++) { 67 67 arr[i] = BigInteger.valueOf(((Character)e.at(i)).charValue()); 68 } catch (ClassCastException _) {69 throw new RefalException("Convert", "Chars-To-Bytes", "Invalid argument");70 68 } 69 } catch (ClassCastException _) { 70 throw new RefalException("Convert", "Chars-To-Bytes", "Invalid argument"); 71 71 } 72 72 res.assign(arr); … … 79 79 { 80 80 Character[] arr = new Character[e.getLen()]; 81 for (int i = 0; i < arr.length; i++){82 try{81 try { 82 for (int i = 0; i < arr.length; i++) { 83 83 BigInteger b = (BigInteger) e.at(i); 84 84 if (b.compareTo(min_char) < 0 || b.compareTo(max_char) > 0) 85 85 throw new RefalException("Convert", "Bytes-To-Chars", "Invalid argument"); 86 86 arr[i] = new Character((char) b.intValue()); 87 } catch (ClassCastException _) {88 throw new RefalException("Convert", "Bytes-To-Chars", "Invalid argument");89 87 } 88 } catch (ClassCastException _) { 89 throw new RefalException("Convert", "Bytes-To-Chars", "Invalid argument"); 90 90 } 91 91 res.assign(arr); -
to-imperative/trunk/java/org/refal/plus/library/NamedString.java
r1846 r2192 1 1 /* 2 * Created on 27.01.2006 3 * 4 * TODO To change the template for this generated file go to 5 * Window - Preferences - Java - Code Style - Code Templates 2 * $Id$ 6 3 */ 4 7 5 package org.refal.plus.library; 8 6 9 7 import org.refal.plus.*; 10 8 11 /** 12 * @author alex 13 * 14 * Refal java runtime library: 27.01.2006 15 */ 16 public class NamedString extends String implements Named { 9 public class NamedString extends String implements Named 10 { 11 static 12 { 13 String.init(); 14 RefalRuntime.setPriorityTheSame(NamedString.class, String.class); 15 } 17 16 18 17 private java.lang.String name; … … 20 19 public NamedString (java.lang.String n) 21 20 { 22 super();23 21 name = n; 24 22 } … … 26 24 public java.lang.String toString () 27 25 { 28 return name;26 return super.toString(); 29 27 } 30 31 28 } -
to-imperative/trunk/java/org/refal/plus/library/NamedTable.java
r1963 r2192 7 7 import org.refal.plus.*; 8 8 import java.lang.String; 9 import java.util.TreeMap;10 9 11 public class NamedTable extends T reeMapimplements Named10 public class NamedTable extends Table implements Named 12 11 { 13 12 static 14 13 { 15 14 Table.init(); 16 RefalRuntime.setPriorityTheSame(NamedTable.class, T reeMap.class);15 RefalRuntime.setPriorityTheSame(NamedTable.class, Table.class); 17 16 } 18 17 -
to-imperative/trunk/java/org/refal/plus/library/NamedVector.java
r1846 r2192 1 1 /* 2 * Created on 14.02.2006 3 * 4 * TODO To change the template for this generated file go to 5 * Window - Preferences - Java - Code Style - Code Templates 2 * $Id$ 6 3 */ 4 7 5 package org.refal.plus.library; 8 6 … … 10 8 import java.lang.String; 11 9 12 /** 13 * @author alex 14 * 15 * Refal java runtime library: 14.02.2006 16 */ 17 public class NamedVector extends Vector implements Named { 10 public class NamedVector extends Vector implements Named 11 { 12 static 13 { 14 Vector.init(); 15 RefalRuntime.setPriorityTheSame(NamedVector.class, Vector.class); 16 } 17 18 18 private String name; 19 19 20 20 public NamedVector (String n) 21 21 { 22 super();23 22 name = n; 24 23 } 25 24 26 25 public String toString () 27 26 { -
to-imperative/trunk/java/org/refal/plus/library/String.java
r1846 r2192 1 /* 2 * $Id$ 3 */ 4 1 5 package org.refal.plus.library; 2 6 3 import org.refal.plus.Expr; 4 import org.refal.plus.Int; 5 import org.refal.plus.RefalException; 6 import org.refal.plus.ValueOutOfBoundsException; 7 import org.refal.plus.*; 8 import java.math.BigInteger; 7 9 8 /** 9 * @author alex 10 * 11 * Refal java runtime library: 13.09.2005 12 */ 10 public class String 11 { 12 static { init(); } 13 13 14 public class String { 15 public StringBuffer str; 16 17 /** 18 * 19 */ 20 public String() { 21 str = new StringBuffer (); 14 public static void init () 15 { 16 Vector.init(); 17 RefalRuntime.setPriorityAfter(String.class, Vector.class); 22 18 } 23 19 24 /**25 * @return 26 */27 public StringBuffer getStr(){28 return str;20 StringBuffer buffer; 21 22 public String () 23 { 24 buffer = new StringBuffer(); 29 25 } 30 26 31 public void setStr(StringBuffer str) { 32 this.str = str; 27 public String (StringBuffer sb) 28 { 29 buffer = sb; 33 30 } 34 31 35 /** 36 * @param str 37 */ 38 public String (StringBuffer str) { 39 this.str = str; 32 public java.lang.String toString() 33 { 34 return buffer.toString(); 40 35 } 41 36 42 public String (java.lang.String str) { 43 this.str = new StringBuffer (str); 37 public static void String (Expr e, Result res) 38 { 39 res.assign(new String(e.toStringBuffer())); 44 40 } 45 41 46 /** 47 * @return 48 */ 49 public int length() { 50 return str.length(); 51 } 52 53 /** 54 * @param e 55 * @param res 56 */ 57 static public void String(Expr e, Expr res) { 58 StringBuffer s = new StringBuffer (); 59 for (int i = 0; i < e.getLen(); i++) { 60 Object o = e.at(i); 61 s.append (o.toString()); 62 } 63 res.assign (new Expr (new String(s))); 64 } 65 66 /** 67 * @param e 68 * @param len 69 * @param fill 70 */ 71 static public void String_m_Init(Expr e, Expr len, Expr fill) throws RefalException { 72 if (!(e.at(0) instanceof String)) 73 throw new RefalException("String", "String-Init", "Invalid argument"); 74 String str = (String) e.at (0); 75 int f; 42 public static void String_m_Init (Expr str, Expr len, Expr fill) throws RefalException 43 { 44 assert str.getLen() == 1; 45 assert len.getLen() == 1; 46 assert fill.getLen() == 1; 76 47 try { 77 f = Int.intValue(len.at(0)); 78 } catch (ValueOutOfBoundsException exception) { 79 throw new RefalException("String", "String-Init", "Invalid argument"); 80 } 81 StringBuffer strbuf = new StringBuffer(f); 82 int j = 0; 83 for (int i = 0; i < f; i++, j++) { 84 if (j == fill.getLen()) 85 j = 0; 86 strbuf.insert(i, fill.at(j)); 87 } 88 str.setStr (strbuf); 89 } 90 91 /** 92 * @param e 93 * @param fill 94 */ 95 static public void String_m_Fill(Expr e, Expr fill) throws RefalException { 96 if (!(e.at(0) instanceof String)) 97 throw new RefalException("String", "String-Fill", "Invalid argument"); 98 String s = (String) e.at(0); 99 for (int i = 0; i < s.length(); i++) { 100 s.getStr ().insert(i, fill.at(0)); 48 BigInteger bigLen = (BigInteger) len.at(0); 49 if (!Util.fitsNonnegativeInteger(bigLen)) 50 throw new RefalException("String", "String-Init", "Invalid argument"); 51 char[] buf = new char[bigLen.intValue()]; 52 java.util.Arrays.fill(buf, ((Character) fill.at(0)).charValue()); 53 StringBuffer sb = ((String) str.at(0)).buffer; 54 sb.setLength(0); 55 sb.append(buf); 56 } catch (ClassCastException _) { 57 throw new RefalException("String", "String-Init", "Invalid argument"); 101 58 } 102 59 } 103 60 104 /** 105 * @param e 106 * @param res 107 */ 108 static public void String_m_Length(Expr e, Expr res) throws RefalException { 109 if (!(e.at(0) instanceof String)) 110 throw new RefalException("String", "String-Length", "Invalid argument"); 111 String s = (String) e.at(0); 112 res.setAt(new Integer(s.length()), 0); 113 } 114 115 /** 116 * @param e 117 * @param index 118 * @param res 119 */ 120 static public void String_m_Ref(Expr e, Expr index, Expr res) throws RefalException { 121 if (!(e.at(0) instanceof String)) 122 throw new RefalException("String", "String-Ref", "Invalid argument"); 123 String s = (String) e.at(0); 124 int i; 61 public static void String_m_Fill (Expr str, Expr fill) throws RefalException 62 { 63 assert str.getLen() == 1; 64 assert fill.getLen() == 1; 125 65 try { 126 i = Int.intValue(index.at(0)); 127 } catch (ValueOutOfBoundsException exception) { 128 throw new RefalException("String", "String-Ref", "Invalid argument"); 129 } 130 131 res.setAt(new Character((s.getStr().toString ()).charAt(i)),0); 132 } 133 134 /** 135 * @param e 136 * @param index 137 * @param ch 138 */ 139 static public void String_m_Set(Expr e, Expr index, Expr ch) throws RefalException { 140 if (!(e.at(0) instanceof String)) 141 throw new RefalException("String", "String-Set", "Invalid argument"); 142 String s = (String) e.at(0); 143 144 Character c = (Character) ch.at(0); 145 int i; 146 try { 147 i = Int.intValue(index.at(0)); 148 } catch (ValueOutOfBoundsException exception) { 149 throw new RefalException("String", "String-Set", "Invalid argument"); 150 } 151 s.getStr ().setCharAt(i, c.charValue()); 152 } 153 154 /** 155 * @param e 156 * @param source 157 */ 158 static public void String_m_Replace(Expr e, Expr source) throws RefalException { 159 if (!(e.at(0) instanceof String)) 160 throw new RefalException("String", "String-Replace", "Invalid argument"); 161 String s = (String) e.at(0); 162 for (int i = 0; i < source.getLen(); i++) { 163 Object o = source.at(i); 164 s.getStr ().append (o.toString()); 165 } 166 e.setAt(s, 0); 167 } 168 169 /** 170 * @param estr 171 * @param index 172 * @param len 173 * @param res 174 */ 175 static public void Substring(Expr e, Expr index, Expr len, Expr res) throws RefalException { 176 if (!(e.at(0) instanceof String)) 177 throw new RefalException("String", "Substring", "Invalid argument"); 178 String s = (String) e.at(0); 179 int i; 180 try { 181 i = Int.intValue(index.at(0)); 182 } catch (ValueOutOfBoundsException exception) { 183 throw new RefalException("String", "Substring", "Invalid argument"); 184 } 185 int l; 186 try { 187 l = Int.intValue(len.at(0)); 188 } catch (ValueOutOfBoundsException exception) { 189 throw new RefalException("String", "Substring", "Invalid argument"); 190 } 191 192 java.lang.String substr = s.getStr ().substring(i, l + i); 193 res.setAt(new String (substr), 0); 194 } 195 196 /** 197 * @param e 198 * @param index 199 * @param len 200 * @param fill 201 */ 202 static public void Substring_m_Fill(Expr e, Expr index, Expr len, 203 Expr fill) throws RefalException { 204 if (!(e.at(0) instanceof String)) 205 throw new RefalException("String", "Substring-Fill", "Invalid argument"); 206 String s = (String) e.at(0); 207 int i; 208 try { 209 i = Int.intValue(index.at(0)); 210 } catch (ValueOutOfBoundsException exception) { 211 throw new RefalException("String", "Substring-Fill", "Invalid argument"); 212 } 213 int l; 214 try { 215 l = Int.intValue(len.at(0)) + i; 216 } catch (ValueOutOfBoundsException exception) { 217 throw new RefalException("String", "Substring-Fill", "Invalid argument"); 218 } 219 220 for (; i < l; i++) { 221 Character ch = (Character) fill.at(0); 222 s.getStr().setCharAt(i, ch.charValue()); 66 StringBuffer sb = ((String) str.at(0)).buffer; 67 char[] buf = new char[sb.length()]; 68 java.util.Arrays.fill(buf, ((Character) fill.at(0)).charValue()); 69 sb.setLength(0); 70 sb.append(buf); 71 } catch (ClassCastException _) { 72 throw new RefalException("String", "String-Fill", "Invalid argument"); 223 73 } 224 74 } 225 75 76 public static void String_m_Length (Expr str, Result res) throws RefalException 77 { 78 assert str.getLen() == 1; 79 try { 80 res.assign(BigInteger.valueOf(((String) str.at(0)).buffer.length())); 81 } catch (ClassCastException _) { 82 throw new RefalException("String", "String-Length", "Invalid argument"); 83 } 84 } 85 86 public static void String_m_Ref (Expr str, Expr idx, Result res) throws RefalException 87 { 88 assert str.getLen() == 1; 89 assert idx.getLen() == 1; 90 try { 91 BigInteger bigIdx = (BigInteger) idx.at(0); 92 if (!Util.fitsNonnegativeInteger(bigIdx)) 93 throw new RefalException("String", "String-Ref", "Invalid argument"); 94 StringBuffer sb = ((String) str.at(0)).buffer; 95 res.assign(new Character(sb.charAt(bigIdx.intValue()))); 96 } catch (IndexOutOfBoundsException _) { 97 throw new RefalException("String", "String-Ref", "Index out of range"); 98 } 99 catch (ClassCastException _) { 100 throw new RefalException("String", "String-Ref", "Invalid argument"); 101 } 102 } 103 104 public static void String_m_Set (Expr str, Expr idx, Expr ch) throws RefalException 105 { 106 assert str.getLen() == 1; 107 assert idx.getLen() == 1; 108 assert ch.getLen() == 1; 109 try { 110 BigInteger bigIdx = (BigInteger) idx.at(0); 111 if (!Util.fitsNonnegativeInteger(bigIdx)) 112 throw new RefalException("String", "String-Set", "Invalid argument"); 113 StringBuffer sb = ((String) str.at(0)).buffer; 114 sb.setCharAt(bigIdx.intValue(), ((Character) ch.at(0)).charValue()); 115 } catch (IndexOutOfBoundsException _) { 116 throw new RefalException("String", "String-Set", "Index out of range"); 117 } 118 catch (ClassCastException _) { 119 throw new RefalException("String", "String-Set", "Invalid argument"); 120 } 121 } 122 123 public static void String_m_Replace (Expr str, Expr src) throws RefalException 124 { 125 assert str.getLen() == 1; 126 try { 127 ((String) str.at(0)).buffer = src.toStringBuffer(); 128 } catch (ClassCastException _) { 129 throw new RefalException("String", "String-Replace", "Invalid argument"); 130 } 131 } 132 133 public static void Substring (Expr str, Expr idx, Expr len, Result res) throws RefalException 134 { 135 assert str.getLen() == 1; 136 assert idx.getLen() == 1; 137 assert len.getLen() == 1; 138 try { 139 BigInteger bigIdx = (BigInteger) idx.at(0); 140 if (!Util.fitsNonnegativeInteger(bigIdx)) 141 throw new RefalException("String", "Substring", "Invalid argument"); 142 BigInteger bigLen = (BigInteger) len.at(0); 143 if (!Util.fitsNonnegativeInteger(bigLen)) 144 throw new RefalException("String", "Substring", "Invalid argument"); 145 int s = bigIdx.intValue(); 146 int e = s + bigLen.intValue(); 147 StringBuffer sb = ((String) str.at(0)).buffer; 148 res.assign(new StringBuffer(sb.substring(s, e))); 149 } catch (IndexOutOfBoundsException _) { 150 throw new RefalException("String", "Substring", "Index out of range"); 151 } 152 catch (ClassCastException _) { 153 throw new RefalException("String", "Substring", "Invalid argument"); 154 } 155 } 156 157 public static void Substring_m_Fill (Expr str, Expr idx, Expr len, Expr fill) 158 throws RefalException 159 { 160 assert str.getLen() == 1; 161 assert idx.getLen() == 1; 162 assert len.getLen() == 1; 163 assert fill.getLen() == 1; 164 try { 165 BigInteger bigIdx = (BigInteger) idx.at(0); 166 if (!Util.fitsNonnegativeInteger(bigIdx)) 167 throw new RefalException("String", "Substring-Fill", "Invalid argument"); 168 BigInteger bigLen = (BigInteger) len.at(0); 169 if (!Util.fitsNonnegativeInteger(bigLen)) 170 throw new RefalException("String", "Substring-Fill", "Invalid argument"); 171 int s = bigIdx.intValue(); 172 int l = bigLen.intValue(); 173 char[] buf = new char[l]; 174 java.util.Arrays.fill(buf, ((Character) fill.at(0)).charValue()); 175 ((String) str.at(0)).buffer.delete(s, s + l).insert(s, buf); 176 } catch (IndexOutOfBoundsException _) { 177 throw new RefalException("String", "Substring-Fill", "Index out of range"); 178 } 179 catch (ClassCastException _) { 180 throw new RefalException("String", "Substring-Fill", "Invalid argument"); 181 } 182 } 226 183 } -
to-imperative/trunk/java/org/refal/plus/library/Table.java
r2062 r2192 15 15 public static void init () 16 16 { 17 Box.init(); 18 RefalRuntime.setPriorityAfter(TreeMap.class, Box.class); 17 String.init(); 18 RefalRuntime.setPriorityAfter(Table.class, String.class); 19 } 20 21 TreeMap map; 22 23 public Table () 24 { 25 map = new TreeMap(); 26 } 27 28 public Table (TreeMap m) 29 { 30 map = m; 19 31 } 20 32 21 33 static public void Table (Result res) 22 34 { 23 res.assign(new T reeMap());35 res.assign(new Table()); 24 36 } 25 37 … … 28 40 assert tab.getLen() == 1; 29 41 try { 30 ((T reeMap) tab.at(0)).put(key, val);42 ((Table) tab.at(0)).map.put(key, val); 31 43 } catch (ClassCastException _) { 32 44 throw new RefalException("Table", "Bind", "Invalid argument"); … … 38 50 assert tab.getLen() == 1; 39 51 try { 40 ((T reeMap) tab.at(0)).remove(key);52 ((Table) tab.at(0)).map.remove(key); 41 53 } catch (ClassCastException _) { 42 54 throw new RefalException("Table", "Unbind", "Invalid argument"); … … 48 60 assert tab.getLen() == 1; 49 61 try { 50 Expr e = (Expr) ((T reeMap) tab.at(0)).get(key);62 Expr e = (Expr) ((Table) tab.at(0)).map.get(key); 51 63 if (e == null) return false; 52 64 val.assign(e); … … 61 73 assert tab.getLen() == 1; 62 74 try { 63 return ((T reeMap) tab.at(0)).containsKey(key);75 return ((Table) tab.at(0)).map.containsKey(key); 64 76 } catch (ClassCastException _) { 65 77 throw new RefalException("Table", "In-Table?", "Invalid argument"); … … 71 83 assert tab.getLen() == 1; 72 84 try { 73 res.assign(((T reeMap) tab.at(0)).keySet().toArray());85 res.assign(((Table) tab.at(0)).map.keySet().toArray()); 74 86 } catch (ClassCastException _) { 75 87 throw new RefalException("Table", "Domain", "Invalid argument"); … … 81 93 assert tab.getLen() == 1; 82 94 try { 83 res.assign(((T reeMap) tab.at(0)).values().toArray());95 res.assign(((Table) tab.at(0)).map.values().toArray()); 84 96 } catch (ClassCastException _) { 85 97 throw new RefalException("Table", "Values", "Invalid argument"); … … 91 103 assert tab.getLen() == 1; 92 104 try { 93 TreeMap t = ( TreeMap) tab.at(0);105 TreeMap t = ((Table) tab.at(0)).map; 94 106 Expr[] e = new Expr[t.size()]; 95 107 Iterator i = t.entrySet().iterator(); … … 110 122 assert tab.getLen() == 1; 111 123 try { 112 res.assign( ((TreeMap) tab.at(0)).clone());124 res.assign(new Table(new TreeMap(((Table) tab.at(0)).map))); 113 125 } catch (ClassCastException _) { 114 126 throw new RefalException("Table", "Table-Copy", "Invalid argument"); … … 121 133 assert source.getLen() == 1; 122 134 try { 123 TreeMap t = (TreeMap) target.at(0); 124 t.clear(); 125 t.putAll((TreeMap) source.at(0)); 135 ((Table) target.at(0)).map = new TreeMap(((Table) source.at(0)).map); 126 136 } catch (ClassCastException _) { 127 137 throw new RefalException("Table", "Replace-Table", "Invalid argument"); … … 133 143 assert tab.getLen() == 1; 134 144 try { 135 ((T reeMap) tab.at(0)).clear();145 ((Table) tab.at(0)).map.clear(); 136 146 } catch (ClassCastException _) { 137 147 throw new RefalException("Table", "Clear-Table", "Invalid argument"); … … 143 153 assert tab.getLen() == 1; 144 154 try { 145 res.assign(BigInteger.valueOf(((T reeMap) tab.at(0)).size()));155 res.assign(BigInteger.valueOf(((Table) tab.at(0)).map.size())); 146 156 } catch (ClassCastException _) { 147 157 throw new RefalException("Table", "Table-Size", "Invalid argument"); -
to-imperative/trunk/java/org/refal/plus/library/Vector.java
r1846 r2192 1 /* 2 * $Id$ 3 */ 4 1 5 package org.refal.plus.library; 2 6 3 7 import org.refal.plus.*; 8 import java.math.BigInteger; 4 9 5 /** 6 * @author alex 7 * 8 * Refal java runtime library: 13.09.2005 9 */ 10 public class Vector extends java.util.Vector { 10 public class Vector 11 { 12 static { init(); } 11 13 12 /** 13 * @param size 14 */ 15 public Vector(int size) { 16 super(size); 14 public static void init () 15 { 16 Box.init(); 17 RefalRuntime.setPriorityAfter(Vector.class, Box.class); 17 18 } 18 19 19 /**20 * 21 */22 public Vector(){23 super();20 java.util.Vector vector; 21 22 public Vector () 23 { 24 vector = new java.util.Vector(); 24 25 } 25 26 26 /** 27 * @param length 28 */ 29 public Vector(Expr length) throws ValueOutOfBoundsException { 30 super(Int.intValue(length)); 27 public Vector (java.util.Vector v) 28 { 29 vector = v; 31 30 } 32 31 33 /** 34 * @param obj 35 * @param res 36 */ 37 static public void Vector(Object obj, Expr res) throws RefalException { 38 if (! (obj instanceof Expr)) 39 throw new RefalException("Vector", "Vector", "Invalid argument"); 32 public Vector (java.util.Collection c) 33 { 34 vector = new java.util.Vector(c); 35 } 40 36 41 if (obj == null) 42 res.assign(new Vector()); 43 else { 44 Expr e = (Expr) obj; 45 Vector v = new Vector(e.getLen()); 46 for (int i = 0; i < e.getLen(); i++) { 47 if (e.at(i) instanceof Vector) { 48 Vector v1 = (Vector) e.at(i); 49 Object[] r = new Object[v1.length()]; 50 v1.copyInto(r); 51 for (int j = 0; j < r.length; j++) 52 v.addElement(r[j]); 53 continue; 54 } 55 v.addElement(e.at(i)); 56 } 57 res.assign(new Expr (v)); 37 public static void Vector (Expr src, Result res) 38 { 39 java.util.Vector v = new java.util.Vector(src.getLen(), src.getLen()/2); 40 for (Expr.Iterator i = src.iterator(); i.hasNext(); ) { 41 Object o = i.next(); 42 if (o instanceof Vector) 43 v.addAll(((Vector) o).vector); 44 else 45 v.add(o); 46 } 47 res.assign(new Vector(v)); 48 } 49 50 public static void Vector_m_To_m_Exp (Expr vec, Result res) 51 { 52 assert vec.getLen() == 1; 53 res.assignUnsafe(((Vector) vec.at(0)).vector.toArray()); 54 } 55 56 public static void Vector_m_Init (Expr vec, Expr len, Expr fill) throws RefalException 57 { 58 assert vec.getLen() == 1; 59 assert len.getLen() == 1; 60 try { 61 BigInteger bigLen = (BigInteger) len.at(0); 62 if (!Util.fitsNonnegativeInteger(bigLen)) 63 throw new RefalException("Vector", "Vector-Init", "Invalid argument"); 64 int s = bigLen.intValue(); 65 java.util.Vector v = ((Vector) vec.at(0)).vector; 66 v.setSize(s); 67 for (int i = 0; i < s; i++) 68 v.set(i, fill); 69 } catch (ClassCastException _) { 70 throw new RefalException("Vector", "Vector-Init", "Invalid argument"); 58 71 } 59 72 } 60 73 61 /** 62 * @param vec 63 * @param res 64 */ 65 static public void Vector_m_To_m_Exp(Expr vec, Expr res) throws RefalException { 66 if (! (vec.at(0) instanceof Vector)) 67 throw new RefalException("Vector", "Vector-To-Exp", "Invalid argument"); 68 Vector v = (Vector) vec.at(0); 69 Object[] o = new Object[v.size()]; 70 for (int i = 0; i < v.size(); i++) { 71 o[i] = v.elementAt(i); 72 } 73 res.assign(o); 74 } 75 76 /** 77 * @param vec 78 * @param res 79 */ 80 static public void Vector_m_Length(Expr vec, Expr res) throws RefalException { 81 if (! (vec.at(0) instanceof Vector)) 82 throw new RefalException("Vector", "Vector-Length", "Invalid argument"); 83 84 Vector v = (Vector) vec.at(0); 85 int len = 0; 86 for (int i = 0; i < v.size(); i++) { 87 if (v.elementAt(i) instanceof Vector) { 88 len += ((Vector) (v.elementAt(i))).length(); 89 } else 90 len += 1; 91 } 92 res.assign(new Expr (len + "")); 93 } 94 95 /** 96 * @param vec 97 * @param length 98 * @param fill 99 */ 100 static public void Vector_m_Init(Expr vec, Expr length, Expr fill) throws RefalException { 101 if (! (vec.at(0) instanceof Vector)) 102 throw new RefalException("Vector", "Vector-Init", "Invalid argument"); 103 104 Vector v = (Vector) vec.at(0); 105 v.clear(); 106 int l; 74 public static void Vector_m_Fill (Expr vec, Expr fill) throws RefalException 75 { 76 assert vec.getLen() == 1; 107 77 try { 108 l = Int.intValue(length.at(0)); 109 } catch (ValueOutOfBoundsException exception) { 110 throw new RefalException("Vector", "Vector-Init", "Invalid argument"); 111 } 112 113 v.setSize(l); 114 for (int i = 0; i < v.size(); i++) { 115 v.setElementAt(fill, i); 78 java.util.Vector v = ((Vector) vec.at(0)).vector; 79 for (int i = v.size() - 1; i >= 0; i--) 80 v.set(i, fill); 81 } catch (ClassCastException _) { 82 throw new RefalException("Vector", "Vector-Fill", "Invalid argument"); 116 83 } 117 84 } 118 85 119 /** 120 * @param vec 121 * @param index 122 * @param e 123 */ 124 static public void Vector_m_Set(Expr vec, int index, Expr e) throws RefalException { 125 if (! (vec.at(0) instanceof Vector)) 126 throw new RefalException("Vector", "Vector-Set", "Invalid argument"); 127 128 Vector v = (Vector) vec.at(0); 129 v.setElementAt(e, index); 86 public static void Vector_m_Length (Expr vec, Result res) throws RefalException 87 { 88 assert vec.getLen() == 1; 89 try { 90 res.assign(BigInteger.valueOf(((Vector) vec.at(0)).vector.size())); 91 } catch (ClassCastException _) { 92 throw new RefalException("Vector", "Vector-Length", "Invalid argument"); 93 } 130 94 } 131 95 132 /** 133 * @param vec 134 * @param index 135 * @param e 136 */ 137 static public void Vector_m_Set(Expr vec, Expr index, Expr e) throws RefalException { 138 if (! (vec.at(0) instanceof Vector)) 139 throw new RefalException("Vector", "Vector-Set", "Invalid argument"); 140 Vector v = (Vector) vec.at(0); 141 int l; 96 public static void Vector_m_Ref (Expr vec, Expr idx, Result res) throws RefalException 97 { 98 assert vec.getLen() == 1; 99 assert idx.getLen() == 1; 142 100 try { 143 l = Int.intValue(index.at(0)); 144 } catch (ValueOutOfBoundsException exception) { 145 throw new RefalException("Vector", "Vector-Set", "Invalid argument"); 101 BigInteger bigIdx = (BigInteger) idx.at(0); 102 if (!Util.fitsNonnegativeInteger(bigIdx)) 103 throw new RefalException("Vector", "Vector-Ref", "Invalid argument"); 104 java.util.Vector v = ((Vector) vec.at(0)).vector; 105 res.assign((Expr) v.get(bigIdx.intValue())); 106 } catch (IndexOutOfBoundsException _) { 107 throw new RefalException("Vector", "Vector-Ref", "Index out of range"); 146 108 } 147 148 if (l == v.size()) 149 v.insertElementAt(e, l); 150 else 151 v.setElementAt(e, l); 109 catch (ClassCastException _) { 110 throw new RefalException("Vector", "Vector-Ref", "Invalid argument"); 111 } 152 112 } 153 113 154 /** 155 * @param vec 156 * @param index 157 * @param res 158 */ 159 static public void Vector_m_Ref(Expr vec, Expr index, Expr res) throws RefalException { 160 if (! (vec.at(0) instanceof Vector)) 161 throw new RefalException("Vector", "Vector-Ref", "Invalid argument"); 162 Vector v = (Vector) vec.at(0); 163 int l; 114 public static void Vector_m_Set (Expr vec, Expr idx, Expr e) throws RefalException 115 { 116 assert vec.getLen() == 1; 117 assert idx.getLen() == 1; 164 118 try { 165 l = Int.intValue(index.at(0)); 166 } catch (ValueOutOfBoundsException exception) { 167 throw new RefalException("Vector", "Vector-Ref", "Invalid argument"); 119 BigInteger bigIdx = (BigInteger) idx.at(0); 120 if (!Util.fitsNonnegativeInteger(bigIdx)) 121 throw new RefalException("Vector", "Vector-Set", "Invalid argument"); 122 java.util.Vector v = ((Vector) vec.at(0)).vector; 123 v.set(bigIdx.intValue(), e); 124 } catch (IndexOutOfBoundsException _) { 125 throw new RefalException("Vector", "Vector-Set", "Index out of range"); 168 126 } 169 170 res.assign((Expr) v.elementAt(l)); 127 catch (ClassCastException _) { 128 throw new RefalException("Vector", "Vector-Set", "Invalid argument"); 129 } 171 130 } 172 131 173 /** 174 * @param vec 175 * @param source 176 */ 177 static public void Vector_m_Replace(Expr vec, Expr source) throws RefalException { 178 if (! (vec.at(0) instanceof Vector)) 179 throw new RefalException("Vector", "Vector-Replace", "Invalid argument"); 180 Vector v = new Vector (); 181 for (int i = 0; i < source.getLen(); i++) { 182 if (source.at(i) instanceof Vector) { 183 Vector v1 = (Vector) source.at(i); 184 Object[] r = new Object[v1.size ()]; 185 v1.copyInto(r); 186 for (int j = 0; j < r.length; j++) 187 v.add(r[j]); 188 continue; 132 public static void Vector_m_Replace (Expr vec, Expr src) throws RefalException 133 { 134 assert vec.getLen() == 1; 135 try { 136 java.util.Vector v = new java.util.Vector(src.getLen(), src.getLen()/2); 137 for (Expr.Iterator i = src.iterator(); i.hasNext(); ) { 138 Object o = i.next(); 139 if (o instanceof Vector) 140 v.addAll(((Vector) o).vector); 141 else 142 v.add(o); 189 143 } 190 v.add(source.at(i)); 144 ((Vector) vec.at(0)).vector = v; 145 } catch (ClassCastException _) { 146 throw new RefalException("Vector", "Vector-Replace", "Invalid argument"); 191 147 } 192 193 vec.setAt (v, 0);194 148 } 195 149 196 /** 197 * @param vec 198 * @param fill 199 */ 200 static public void Vector_m_Fill(Expr vec, Expr fill) throws RefalException { 201 if (! (vec.at(0) instanceof Vector)) 202 throw new RefalException("Vector", "Vector-Fill", "Invalid argument"); 203 Vector v = (Vector) vec.at(0); 204 Vector new_vec = new Vector(v.size ()); 205 206 for (int i = 0; i < v.size (); i++) { 207 new_vec.insertElementAt(fill, i); 150 public static void Subvector (Expr vec, Expr idx, Expr len, Result res) throws RefalException 151 { 152 assert vec.getLen() == 1; 153 assert idx.getLen() == 1; 154 assert len.getLen() == 1; 155 try { 156 BigInteger bigIdx = (BigInteger) idx.at(0); 157 if (!Util.fitsNonnegativeInteger(bigIdx)) 158 throw new RefalException("Vector", "Subvector", "Invalid argument"); 159 BigInteger bigLen = (BigInteger) len.at(0); 160 if (!Util.fitsNonnegativeInteger(bigLen)) 161 throw new RefalException("Vector", "Subvector", "Invalid argument"); 162 int s = bigIdx.intValue(); 163 int e = s + bigLen.intValue(); 164 res.assign(new Vector(((Vector) vec.at(0)).vector.subList(s, e))); 165 } catch (IndexOutOfBoundsException _) { 166 throw new RefalException("Vector", "Subvector", "Index out of range"); 208 167 } 209 vec.assign(new_vec); 168 catch (ClassCastException _) { 169 throw new RefalException("Vector", "Subvector", "Invalid argument"); 170 } 210 171 } 211 172 212 /** 213 * @param vec 214 * @param index 215 * @param len 216 * @param res 217 */ 218 static public void Subvector(Expr vec, Expr index, Expr len, Expr res) throws RefalException { 219 if (! (vec.at(0) instanceof Vector)) 220 throw new RefalException("Vector", "Subvector", "Invalid argument"); 221 int i; 173 public static void Subvector_m_Fill (Expr vec, Expr idx, Expr len, Expr fill) 174 throws RefalException 175 { 176 assert vec.getLen() == 1; 177 assert idx.getLen() == 1; 178 assert len.getLen() == 1; 222 179 try { 223 i = Int.intValue(index.at(0)); 224 } catch (ValueOutOfBoundsException exception) { 225 throw new RefalException("Vector", "Subvector", "Invalid argument"); 180 BigInteger bigIdx = (BigInteger) idx.at(0); 181 if (!Util.fitsNonnegativeInteger(bigIdx)) 182 throw new RefalException("Vector", "Subvector-Fill", "Invalid argument"); 183 BigInteger bigLen = (BigInteger) len.at(0); 184 if (!Util.fitsNonnegativeInteger(bigLen)) 185 throw new RefalException("Vector", "Subvector-Fill", "Invalid argument"); 186 int s = bigIdx.intValue(); 187 int e = s + bigLen.intValue(); 188 java.util.Vector v = ((Vector) vec.at(0)).vector; 189 for (int i = s; i < e; i++) 190 v.set(i, fill); 191 } catch (IndexOutOfBoundsException _) { 192 throw new RefalException("Vector", "Subvector-Fill", "Index out of range"); 226 193 } 227 int l; 228 try { 229 l = Int.intValue(len.at(0)); 230 } catch (ValueOutOfBoundsException exception) { 231 throw new RefalException("Vector", "Subvector", "Invalid argument"); 194 catch (ClassCastException _) { 195 throw new RefalException("Vector", "Subvector-Fill", "Invalid argument"); 232 196 } 233 // Vector new_vector = new Vector(l - i);234 Vector new_vector = new Vector(l);235 Vector old_vector = (Vector) vec.at(0);236 int k = i;237 for (; k < l + i; k++) {238 new_vector.addElement(old_vector.elementAt(k));239 }240 vec.assign(new_vector);241 res.assign(new_vector);242 }243 244 /**245 * @param vec246 * @param index247 * @param len248 * @param fill249 */250 static public void Subvector_m_Fill(Expr vec, Expr index, Expr len,251 Expr fill) throws RefalException {252 if (! (vec.at(0) instanceof Vector))253 throw new RefalException("Vector", "Subvector-Fill", "Invalid argument");254 Vector v = (Vector) vec.at(0);255 int i;256 try {257 i = Int.intValue(index.at(0));258 } catch (ValueOutOfBoundsException exception) {259 throw new RefalException("Vector", "Subvector-Fill", "Invalid argument");260 }261 int l;262 try {263 l = Int.intValue(len.at(0));264 } catch (ValueOutOfBoundsException exception) {265 throw new RefalException("Vector", "Subvector-Fill", "Invalid argument");266 }267 l += i;268 for (; i < l; i++) {269 v.removeElementAt(i);270 v.insertElementAt(fill, i);271 }272 vec.assign(v);273 }274 275 /**276 * @return277 */278 public int length() {279 return capacity();280 197 } 281 198 } 282
Note: See TracChangeset
for help on using the changeset viewer.