01: // Copyright (c) 2002 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.functions;
05:
06: import gnu.lists.*;
07: import gnu.mapping.*;
08:
09: public class ArrayRef extends ProcedureN {
10: public static final ArrayRef arrayRef = new ArrayRef();
11:
12: public static Object arrayRef(Array array, Sequence index) {
13: int dims = index.size();
14: int[] indexes = new int[dims];
15: for (int i = 0; i < dims; i++) {
16: indexes[i] = ((Number) index.get(i)).intValue();
17: }
18: return array.get(indexes);
19: }
20:
21: public Object apply2(Object arg0, Object arg1) throws Throwable {
22: if (arg1 instanceof Sequence)
23: return arrayRef((Array) arg0, (Sequence) arg1);
24: return super .apply2(arg0, arg1);
25: }
26:
27: public Object applyN(Object[] args) throws Throwable {
28: Array array = (Array) args[0];
29:
30: if (args.length == 2) {
31: Object arg1 = args[1];
32: if (arg1 instanceof Sequence)
33: return arrayRef(array, (Sequence) arg1);
34: }
35: int[] indexes = new int[args.length - 1];
36: for (int i = args.length - 1; --i >= 0;) {
37: indexes[i] = ((Number) args[i + 1]).intValue();
38: }
39: return array.get(indexes);
40: }
41: }
|