Example #1
0
    public Object execute(
        RArray source, Selector selectorI, Selector selectorJ, boolean drop, int exact)
        throws UnexpectedResultException {
      assert Utils.check(subset);
      int[] ndim = source.dimensions();
      int m = ndim[0];
      int n = ndim[1];
      selectorI.start(m, ast);
      selectorJ.start(n, ast);
      int nm = selectorI.size();
      int nn = selectorJ.size();
      boolean mayHaveNA = selectorI.mayHaveNA() || selectorJ.mayHaveNA();
      int nsize = nm * nn;
      if ((nm != 1 && nn != 1) || !drop) {
        ndim = new int[] {nm, nn};
      } else {
        ndim = null;
      }
      RArray res = Utils.createArray(source, nsize, ndim, null, null); // drop attributes
      if (!mayHaveNA) {
        int resoffset = 0;
        for (int nj = 0; nj < nn; nj++) {
          int j = selectorJ.nextIndex(ast);
          int srcoffset = j * m;
          for (int ni = 0; ni < nm; ni++) {
            int i = selectorI.nextIndex(ast);
            Object value =
                source.getRef(
                    srcoffset
                        + i); // FIXME: check overflow? (the same is at many locations, whenever
                              // indexing a matrix)
            res.set(resoffset++, value);
          }
          selectorI.restart();
        }
      } else {
        for (int nj = 0; nj < nn; nj++) {
          int j = selectorJ.nextIndex(ast);
          if (j != RInt.NA) {
            selectorI.restart();
            for (int ni = 0; ni < nm; ni++) {
              int offset = nj * nm + ni;
              int i = selectorI.nextIndex(ast);
              if (i != RInt.NA) {
                Object value;
                value =
                    source.getRef(
                        j * m
                            + i); // FIXME: check overflow? (the same is at many locations, whenever
                                  // indexing a matrix)
                res.set(offset, value);
              } else {
                Utils.setNA(res, offset);
              }
            }
          } else {
            for (int ni = 0; ni < nm; ni++) {
              Utils.setNA(res, nj * nm + ni);
            }
          }
        }
      }

      return res;
    }