Revision: 3363
http://vexi.svn.sourceforge.net/vexi/?rev=3363&view=rev
Author: clrg
Date: 2009-01-20 03:01:06 +0000 (Tue, 20 Jan 2009)
Log Message:
-----------
Some more [JS]Array clean up
Modified Paths:
--------------
trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp
trunk/core/org.ibex.util/src/org/ibex/util/Basket.java
Modified: trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp 2009-01-19 12:36:32 UTC
(rev 3362)
+++ trunk/core/org.ibex.js/src/org/ibex/js/JSArray.jpp 2009-01-20 03:01:06 UTC
(rev 3363)
@@ -45,8 +45,9 @@
} catch (IndexOutOfBoundsException e) { return null; }
}
public void put(JS key, JS val) throws JSExn {
- if (JSU.toString(key).equals("length")) { size(JSU.toInt(val)); }
-
+ // special array property put for length
+ if (JSU.isString(key) && JSU.toString(key).equals("length")) {
size(JSU.toInt(val)); return; }
+ // normal array[i] put
if (key == null || !(JSU.isInt(key)))
throw new JSExn("Arrays only support positive integer keys, can
not use: "+JSU.toString(key));
int i = JSU.toInt(key);
@@ -90,7 +91,11 @@
public JS.Trap getTrap(JS k) { return null; }
/** FEATURE: move to specialised ArrayStore superclass. */
- public void addAll(JS[] entries) { for (int i=0; i < entries.length; i++)
add(entries[i]); }
+ public void addAll(JS[] entries) {
+ int start = size();
+ size(start+entries.length);
+ System.arraycopy(entries, 0, o, start, entries.length);
+ }
// ECMA Implementation ////////////////////////////////////////////////////
@@ -120,7 +125,7 @@
if (start > length) start = length;
if (end > length) end = length;
JSArray ret = new JSArray(end-start);
- Array.arraycopy(this, start, ret, 0, end-start);
+ System.arraycopy(o, start, ret, 0, end-start);
return ret;
}
@@ -140,13 +145,13 @@
JSArray ret = new JSArray(deleteCount);
ret.size(deleteCount);
// first copy the specified indice into the return array
- Array.arraycopy(this, start, ret, 0, deleteCount);
+ System.arraycopy(o, start, ret.o, 0, deleteCount);
// if array grows, must grow it before shuffling remaining contents
if (newLength > oldLength) size(newLength);
// copy the remaining array contents to new position
- Array.arraycopy(this, start+deleteCount, this, start+newCount,
oldLength-deleteCount-start);
+ System.arraycopy(o, start+deleteCount, o, start+newCount,
oldLength-deleteCount-start);
// copy in the additionally specified elements required
- if (newCount > 0) Array.arraycopy(new Array(args, 2, args.length-2),
0, this, start, newCount);
+ if (newCount > 0) System.arraycopy(args, 2, o, start, newCount);
// hide remaining contents
if (oldLength > newLength) size(newLength);
return ret;
@@ -179,8 +184,9 @@
private JSArray copy() {
JSArray r = new JSArray(0);
r.size = size;
- r.o = new Object[o.length];
- System.arraycopy(o, 0, r.o, 0, o.length);
+ r.o = new Object[size];
+ System.arraycopy(o, 0, r.o, 0, size);
+ if (r.size>r.o.length) throw new Error("Size and o.length mismatch:
"+size+">"+o.length);
return r;
}
Modified: trunk/core/org.ibex.util/src/org/ibex/util/Basket.java
===================================================================
--- trunk/core/org.ibex.util/src/org/ibex/util/Basket.java 2009-01-19
12:36:32 UTC (rev 3362)
+++ trunk/core/org.ibex.util/src/org/ibex/util/Basket.java 2009-01-20
03:01:06 UTC (rev 3363)
@@ -5,6 +5,7 @@
package org.ibex.util;
import java.io.Serializable;
+import java.util.Arrays;
import java.util.HashMap;
public interface Basket extends Serializable {
@@ -52,16 +53,18 @@
public class Array implements RandomAccess, Stack, Queue {
private static final long serialVersionUID = 1233428092L;
+ private static final Object[] emptyArray = new Object[0];
protected Object[] o;
protected int size = 0;
public Array() { this(10); }
- public Array(int initialCapacity) { o = new Object[initialCapacity]; }
+ public Array(int initialCapacity) { o = initialCapacity==0 ?
emptyArray : new Object[initialCapacity]; }
public Array(Object entry) { this(1); add(entry); }
public Array(Object[] src, int start, int num) {
this(num);
System.arraycopy(src, start, o, 0, num);
+ size = num;
}
public void enqueue(Object o) { add(o); }
@@ -80,7 +83,7 @@
}
public Object set(int i, Object obj) {
if (i >= o.length) throw new IndexOutOfBoundsException(
- "index "+i+" is beyond list boundary "+size);
+ "index "+i+" is beyond list boundary "+size+"
(capacity:"+o.length+")");
Object old = o[i]; o[i] = obj;
size = Math.max(i+1, size);
return old;
@@ -112,16 +115,16 @@
if ((obj == null && o[i] == null) || obj.equals(o[i])) return
true;
return false;
}
- public void clear() { for (int i=0; i < size; i++) o[i] = null; size =
0; }
+ public void clear() { if (size>0) { Arrays.fill(o, 0, size-1, null);
size = 0; } }
public int size() { return size; }
public void size(int s) {
if (o.length >= s) {
// housekeeping
- if (size > s) java.util.Arrays.fill(o, s, size-1, null);
+ if (size > s) Arrays.fill(o, s, size-1, null);
size = s;
return;
}
- Object[] newo = new Object[s];
+ Object[] newo = new Object[Math.max(s,Math.max(2,o.length*2))];
System.arraycopy(o, 0, newo, 0, size);
o = newo;
size = s;
@@ -175,11 +178,6 @@
sort(a, b, c, start, lo-1);
sort(a, b, c, lo+1, end);
}
-
- /** proxy for System.arraycopy */
- public static void arraycopy(Array src, int srcPos, Array dest, int
destPos, int length) {
- System.arraycopy(src.o, srcPos, dest.o, destPos, length);
- }
private static final void swap(Array vec, int a, int b) {
if (a != b) {
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Vexi-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vexi-svn