Reorders columns of a Frame so that columns that only used to make interactions
are at the end of the Frame. Only Vecs that will actually be used are kept in the frame.
@param f frame to adjust
@return reordered frame
556996
public Frame reorderColumns(Frame f) {
      if ((_interactionsOnly == null) || (f == null))
        return f;
      Vec[] interOnlyVecs = f.vecs(_interactionsOnly);
      f.remove(_interactionsOnly);
      for (int i = 0; i < _interactionsOnly.length; i++) {
        if (isUsed(_interactionsOnly[i])) {
          f.add(_interactionsOnly[i], interOnlyVecs[i]);
        } else if (! isIgnored(_interactionsOnly[i])) {
          Log.warn("Column '" + _interactionsOnly[i] + "' was marked to be used for interactions only " +
                  "but it is not actually required in any interaction.");
        }
      }
      return f;
    }
741450
public Matrix4x3f set(Vector3fc col0, Vector3fc col1, Vector3fc col2, Vector3fc col3) {
        if (col0 instanceof Vector3f &&
            col1 instanceof Vector3f &&
            col2 instanceof Vector3f &&
            col3 instanceof Vector3f) {
            MemUtil.INSTANCE.set(this, (Vector3f) col0, (Vector3f) col1, (Vector3f) col2, (Vector3f) col3);
        } else {
            setVector3fc(col0, col1, col2, col3);
        }
        this.properties = 0;
        return this;
    }
1195113
private ValFrame rowwiseWhichVal(Frame fr, final boolean na_rm) {
        String[] newnames = {"which." + searchVal()};
        Key<Frame> newkey = Key.make();

        // Determine how many columns of different types we have
        int n_numeric = 0, n_time = 0;
        for (Vec vec : fr.vecs()) {
            if (vec.isNumeric()) n_numeric++;
            if (vec.isTime()) n_time++;
        }
        // Compute the type of the resulting column: if all columns are TIME then the result is also time; otherwise
        // if at least one column is numeric then the result is also numeric.
        byte resType = n_numeric > 0 ? Vec.T_NUM : Vec.T_TIME;

        // Construct the frame over which the val index should be computed
        Frame compFrame = new Frame();
        for (int i = 0; i < fr.numCols(); i++) {
            Vec vec = fr.vec(i);
            if (n_numeric > 0? vec.isNumeric() : vec.isTime())
                compFrame.add(fr.name(i), vec);
        }
        Vec anyvec = compFrame.anyVec();

        // Take into account certain corner cases
        if (anyvec == null) {
            Frame res = new Frame(newkey);
            anyvec = fr.anyVec();
            if (anyvec != null) {
                // All columns in the original frame are non-numeric -> return a vec of NAs
                res.add("which." + searchVal(), anyvec.makeCon(Double.NaN));
            } // else the original frame is empty, in which case we return an empty frame too
            return new ValFrame(res);
        }
        if (!na_rm && n_numeric < fr.numCols() && n_time < fr.numCols()) {
            // If some of the columns are non-numeric and na_rm==false, then the result is a vec of NAs
            Frame res = new Frame(newkey, newnames, new Vec[]{anyvec.makeCon(Double.NaN)});
            return new ValFrame(res);
        }

        // Compute over all rows
        final int numCols = compFrame.numCols();
        Frame res = new MRTask() {
            @Override
            public void map(Chunk[] cs, NewChunk nc) {
                for (int i = 0; i < cs[0]._len; i++) {
                    int numNaColumns = 0;
                    double value = Double.NEGATIVE_INFINITY;
                    int valueIndex = 0;
                    if (searchVal() == "max") { //Looking for the max?
                        for (int j = 0; j < numCols; j++) {
                            double val = cs[j].atd(i);
                            if (Double.isNaN(val)) {
                                numNaColumns++;
                            } else if (val > value) { //Return the first occurrence of the val
                                value = val;
                                valueIndex = j;
                            }
                        }
                    }else if(searchVal()=="min"){ //Looking for the min?
                        for (int j = 0; j < numCols; j++) {
                            double val = cs[j].atd(i);
                            if (Double.isNaN(val)) {
                                numNaColumns++;
                            }
                            else if(val <  value) { //Return the first occurrence of the min index
                                value = val;
                                valueIndex = j;
                            }
                        }
                    }else{
                        throw new IllegalArgumentException("Incorrect argument: expected to search for max() or min(), received " + searchVal());
                    }
                    if (na_rm ? numNaColumns < numCols : numNaColumns == 0)
                        nc.addNum(valueIndex);
                    else
                        nc.addNum(Double.NaN);
                }
            }
        }.doAll(1, resType, compFrame)
                .outputFrame(newkey, newnames, null);

        // Return the result
        return new ValFrame(res);
    }
704418
Frame addRefCnt(Frame fr, int i) {
    if (fr != null)  // Allow and ignore null Frame, easier calling convention
      for (Key<Vec> vec : fr.keys()) _addRefCnt(vec, i);
    return fr;                  // Flow coding
  }

  /**
   * Found in the DKV, if not a tracked TEMP make it a global
   */
  Frame addGlobals(Frame fr) {
    if (!FRAMES.containsKey(fr._key))
      Collections.addAll(GLOBALS, fr.keys());
    return fr;                  // Flow coding
  }

  /**
   * Track a freshly minted tmp frame.  This frame can be removed when the session ends (unlike global frames), or
   * anytime during the session when the client removes it.
   */
  public Frame track_tmp(Frame fr) {
    assert fr._key != null;     // Temps have names
    FRAMES.put(fr._key, fr);     // Track for session
    addRefCnt(fr, 1);            // Refcnt is also up: these Vecs stick around after single Rapids call for the next one
    DKV.put(fr);                // Into DKV, so e.g. Flow can view for debugging
    return fr;                  // Flow coding
  }

  /**
   * Remove and delete a session-tracked frame.
   * Remove from all session tracking spaces.
   * Remove any newly-unshared Vecs, but keep the shared ones.
   */
  public void remove(Frame fr) {
    if (fr == null) return;
    Futures fs = new Futures();
    if (!FRAMES.containsKey(fr._key)) { // In globals and not temps?
      for (Key<Vec> vec : fr.keys()) {
        GLOBALS.remove(vec);         // Not a global anymore
        if (REFCNTS.get(vec) == null) // If not shared with temps
          vec.remove(fs);            // Remove unshared dead global
      }
    } else {                    // Else a temp and not a global
      fs = downRefCnt(fr, fs);   // Standard down-ref counting of all Vecs
      FRAMES.remove(fr._key);   // And remove from temps
    }
    DKV.remove(fr._key, fs);     // Shallow remove, internal were Vecs removed 1-by-1
    fs.blockForPending();
  }

  /**
   * Lower refcnt of all Vecs in frame, deleting Vecs that go to zero refs.
   * Passed in a Futures which is returned, and set to non-null if something gets deleted.
   */
  Futures downRefCnt(Frame fr, Futures fs) {
    for (Key<Vec> vec : fr.keys())    // Refcnt -1 all Vecs
      if (addRefCnt(vec, -1) == 0) {
        if (fs == null) fs = new Futures();
        vec.remove(fs);
      }
    return fs;
  }

  /**
   * Update a global ID, maintaining sharing of Vecs
   */
  public Frame assign(Key<Frame> id, Frame src) {
    if (FRAMES.containsKey(id)) throw new IllegalArgumentException("Cannot reassign temp " + id);
    Futures fs = new Futures();
    // Vec lifetime invariant: Globals do not share with other globals (but can
    // share with temps).  All the src Vecs are about to become globals.  If
    // the ID already exists, and global Vecs within it are about to die, and thus
    // may be deleted.
    Frame fr = DKV.getGet(id);
    if (fr != null) {          // Prior frame exists
      for (Key<Vec> vec : fr.keys()) {
        if (GLOBALS.remove(vec) && _getRefCnt(vec) == 0)
          vec.remove(fs);       // Remove unused global vec
      }
    }
    // Copy (defensive) the base vecs array.  Then copy any vecs which are
    // already globals - this new global must be independent of any other
    // global Vecs - because global Vecs get side-effected by unrelated
    // operations.
    Vec[] svecs = src.vecs().clone();
    for (int i = 0; i < svecs.length; i++)
      if (GLOBALS.contains(svecs[i]._key))
        svecs[i] = svecs[i].makeCopy();
    // Make and install new global Frame
    Frame fr2 = new Frame(id, src._names.clone(), svecs);
    DKV.put(fr2, fs);
    addGlobals(fr2);
    fs.blockForPending();
    return fr2;
  }

  /**
   * Support C-O-W optimizations: the following list of columns are about to be updated.  Copy them as-needed and
   * replace in the Frame.  Return the updated Frame vecs for flow-coding.
   */
  public Vec[] copyOnWrite(Frame fr, int[] cols) {
    Vec did_copy = null;        // Did a copy?
    Vec[] vecs = fr.vecs();
    for (int col : cols) {
      Vec vec = vecs[col];
      int refcnt = getRefCnt(vec._key);
      assert refcnt > 0;
      if (refcnt > 1)          // If refcnt is 1, we allow the update to take in-place
        fr.replace(col, (did_copy = vec.makeCopy()));
    }
    if (did_copy != null && fr._key != null) DKV.put(fr); // Then update frame in the DKV
    return vecs;
  }

  /**
   * Check that ref counts are in a consistent state.
   * This should only be called between calls to Rapids expressions (otherwise may blow false-positives).
   * @param returning If sanity check is done at the end of the session, there is a value being returned. This value
   *                  might be a Frame (which would not be in FRAMES). So we pass the "returning" value explicitly,
   *                  so that all its references can be properly accounted for.
   */
  private void sanity_check_refs(Val returning) {
    if ((sanityChecksCounter++) % 1000 >= sanityChecksFrequency) return;

    // Compute refcnts from tracked frames only.  Since we are between Rapids
    // calls the only tracked Vecs should be those from tracked frames.
    NonBlockingHashMap<Key<Vec>, Integer> refcnts = new NonBlockingHashMap<>(REFCNTS.size());
    for (Frame fr : FRAMES.values())
      for (Key<Vec> vec : fr.keys()) {
        Integer count = refcnts.get(vec);
        refcnts.put(vec, count == null ? 1 : count + 1);
      }
    // Now account for the returning frame (if it is a Frame). Note that it is entirely possible that this frame is
    // already in the FRAMES list, however we need to account for it anyways -- this is how Env works...
    if (returning != null && returning.isFrame())
      for (Key<Vec> vec : returning.getFrame().keys()) {
        Integer count = refcnts.get(vec);
        refcnts.put(vec, count == null ? 1 : count + 1);
      }

    // Now compare computed refcnts to cached REFCNTS.
    // First check that every Vec in computed refcnt is also in REFCNTS, with equal counts.
    for (Map.Entry<Key<Vec>,Integer> pair : refcnts.entrySet()) {
      Key<Vec> vec = pair.getKey();
      Integer count = pair.getValue();
      Integer savedCount = REFCNTS.get(vec);
      if (savedCount == null) throw new IllegalStateException("REFCNTS missing vec " + vec);
      if (count.intValue() != savedCount.intValue())
        throw new IllegalStateException(
            "Ref-count mismatch for vec " + vec + ": REFCNT = " + savedCount + ", should be " + count);
    }
    // Then check that every cached REFCNT is in the computed set as well.
    if (refcnts.size() != REFCNTS.size())
      for (Map.Entry<Key<Vec>, Integer> pair : REFCNTS.entrySet()) {
        if (!refcnts.containsKey(pair.getKey()))
          throw new IllegalStateException(
              "REFCNTs contains an extra vec " + pair.getKey() + ", count = " + pair.getValue());
      }
  }

  // To avoid a class-circularity hang, we need to force other members of the
  // cluster to load the Rapids & AstRoot classes BEFORE trying to execute code
  // remotely, because e.g. ddply runs functions on all nodes.
  private static volatile boolean _initialized; // One-shot init

  static void cluster_init() {
    if (_initialized) return;
    // Touch a common class to force loading
    new MRTask() {
      @Override
      public void setupLocal() {
        new AstPlus();
      }
    }.doAllNodes();
    _initialized = true;
  }
}
57888
public Frame mult(Frame y) {
    int xrows = (int)_x.numRows();
    int xcols =      _x.numCols();
    int yrows = (int) y.numRows();
    int ycols =       y.numCols();
    if(xcols != yrows)
      throw new IllegalArgumentException("Matrices are not compatible for multiplication: ["+xrows+"x"+xcols+"] * ["+yrows+"x"+ycols+"]. Requires [n x m] * [m x p]");

    Vec[] x_vecs = _x.vecs();
    Vec[] y_vecs =  y.vecs();
    for(int k = 0; k < xcols; k++) {
      if(x_vecs[k].isEnum())
        throw new IllegalArgumentException("Multiplication not meaningful for factor column "+k);
    }
    for(int j = 0; j < ycols; j++) {
      if(y_vecs[j].isEnum())
        throw new IllegalArgumentException("Multiplication not meaningful for factor column "+j);
    }

    Vec[] output = new Vec[ycols];
    for(int j = 0; j < ycols; j++)
      output[j] = Vec.makeSeq(xrows);

    for(int i = 0; i < xrows; i++) {
      for(int j = 0; j < ycols; j++) {
        Vec yvec = y_vecs[j];
        double d = 0;
        for(int k = 0; k < xcols; k++)
          d += x_vecs[k].at(i) * yvec.at(k);
        output[j].set(i, d);
      }
    }
    return new Frame(y._names,output);
  }
355304
def reorder(self, single_column=False):
        """"""
        if single_column:
            columns = self.sortOrder[:1]
        else:
            columns = self.sortOrder
        for ascending,column in columns[::-1]:
            # Python 2.2+ guarantees stable sort, so sort by each column in reverse 
            # order will order by the assigned columns 
            self.sorted.sort( key=column.get, reverse=(not ascending))
897406
public <K, C> ColumnMutation prepareColumnMutation(ColumnFamily<K, C> cf, K rowKey, C column, ConsistencyLevel consistency) {
        return _astyanaxKeyspace.prepareColumnMutation(cf, rowKey, column).setConsistencyLevel(clamp(consistency));
    }
733236
protected List appendListOfColumns(String[] columns, StringBuffer buf)
    {
        ArrayList columnList = new ArrayList();

        for (int i = 0; i < columns.length; i++)
        {
            if (i > 0)
            {
                buf.append(",");
            }
            appendColName(columns[i], false, null, buf);
            columnList.add(columns[i]);
        }
        return columnList;
    }
489872
private ValFrame colwise(Env env, Env.StackHelp stk, Frame fr, AstPrimitive fun) {
    // Break each column into it's own Frame, then execute the function passing
    // the 1 argument.  All columns are independent, and this loop should be
    // parallized over each column.
    Vec vecs[] = fr.vecs();
    Val vals[] = new Val[vecs.length];
    AstRoot[] asts = new AstRoot[]{fun, null};
    for (int i = 0; i < vecs.length; i++) {
      asts[1] = new AstFrame(new Frame(new String[]{fr._names[i]}, new Vec[]{vecs[i]}));
      try (Env.StackHelp stk_inner = env.stk()) {
        vals[i] = fun.apply(env, stk_inner, asts);
      }
    }

    // All the resulting Vals must be the same scalar type (and if ValFrames,
    // the columns must be the same count and type).  Build a Frame result with
    // 1 row column per applied function result (per column), and as many rows
    // as there are columns in the returned Frames.
    Val v0 = vals[0];
    Vec ovecs[] = new Vec[vecs.length];
    switch (v0.type()) {
      case Val.NUM:
        for (int i = 0; i < vecs.length; i++)
          ovecs[i] = Vec.makeCon(vals[i].getNum(), 1L); // Since the zero column is a number, all must be numbers
        break;
      case Val.FRM:
        long nrows = v0.getFrame().numRows();
        for (int i = 0; i < vecs.length; i++) {
          Frame res = vals[i].getFrame(); // Since the zero column is a frame, all must be frames
          if (res.numCols() != 1)
            throw new IllegalArgumentException("apply result Frames must have one column, found " + res.numCols() + " cols");
          if (res.numRows() != nrows)
            throw new IllegalArgumentException("apply result Frames must have all the same rows, found " + nrows + " rows and " + res.numRows());
          ovecs[i] = res.vec(0);
        }
        break;
      case Val.NUMS:
        for (int i = 0; i < vecs.length; i++)
          ovecs[i] = Vec.makeCon(vals[i].getNums()[0], 1L);
        break;
      case Val.STRS:
        throw H2O.unimpl();
      case Val.FUN:
        throw water.H2O.unimpl();
      case Val.STR:
        throw water.H2O.unimpl();
      default:
        throw water.H2O.unimpl();
    }
    return new ValFrame(new Frame(fr._names, ovecs));
  }
99334
private void getHeadersFromFrame() {
        byte[] hbf = null;
        if (currentFrame.getFrameType() == FrameTypes.HEADERS || currentFrame.getFrameType() == FrameTypes.PUSHPROMISEHEADERS) {
            hbf = ((FrameHeaders) currentFrame).getHeaderBlockFragment();
        } else if (currentFrame.getFrameType() == FrameTypes.CONTINUATION) {
            hbf = ((FrameContinuation) currentFrame).getHeaderBlockFragment();
        }

        if (hbf != null) {
            if (headerBlock == null) {
                headerBlock = new ArrayList<byte[]>();
            }
            headerBlock.add(hbf);
        }
    }
386399
public Matrix3f set(Vector3fc col0, Vector3fc col1, Vector3fc col2) {
        if (col0 instanceof Vector3f &&
            col1 instanceof Vector3f &&
            col2 instanceof Vector3f) {
            MemUtil.INSTANCE.set(this, (Vector3f) col0, (Vector3f) col1, (Vector3f) col2);
        } else {
            setVector3fc(col0, col1, col2);
        }
        return this;
    }
1042467
public static MutableFst copyFrom(Fst fst) {
    MutableFst copy = emptyWithCopyOfSymbols(fst);
    // build up states
    for (int i = 0; i < fst.getStateCount(); i++) {
      State source = fst.getState(i);
      MutableState target = new MutableState(source.getArcCount());
      target.setFinalWeight(source.getFinalWeight());
      copy.setState(i, target);
    }
    // build arcs now that we have target state refs
    for (int i = 0; i < fst.getStateCount(); i++) {
      State source = fst.getState(i);
      MutableState target = copy.getState(i);
      for (int j = 0; j < source.getArcCount(); j++) {
        Arc sarc = source.getArc(j);
        MutableState nextTargetState = copy.getState(sarc.getNextState().getId());
        MutableArc tarc = new MutableArc(sarc.getIlabel(), sarc.getOlabel(), sarc.getWeight(), nextTargetState);
        target.addArc(tarc);
      }
    }
    MutableState newStart = copy.getState(fst.getStartState().getId());
    copy.setStart(newStart);
    return copy;
  }
1206900
@SuppressWarnings("unused") // called through reflection by RequestServer
  public FramesV3 columnSummary(int version, FramesV3 s) {
    Frame frame = getFromDKV("key", s.frame_id.key()); // safe
    Vec vec = frame.vec(s.column);
    if (null == vec)
      throw new H2OColumnNotFoundArgumentException("column", s.frame_id.toString(), s.column);

    // Compute second pass of rollups: the histograms.
    vec.bins();

    // Cons up our result
    s.frames = new FrameV3[1];
    s.frames[0] = new FrameV3(new Frame(new String[]{s.column}, new Vec[]{vec}), s.row_offset, s.row_count, s.column_offset, s.column_count);
    return s;
  }
1188800
public static ReconfigurationPlan readReconfigurationPlan(File f) {
        try (Reader in = makeIn(f)) {
            return readReconfigurationPlan(in);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }
119367
public Frame nameFrame(String name,int col, int row)
    {
        if (frameMap==null)
        {
            frameMap=new Hashtable(10);
            frameNames=new Vector(10);
        }
        
        Frame frame = frames[col][row];
        if (frame==null)
            frame = frames[col][row] = new Frame();
        
        if (frameMap.get(name)==null)
            frameNames.addElement(name);
        frameMap.put(name,frame);
        frame.name(name);

        return frame;
    }
891263
static Frame uniqueValuesBy(Frame fr, int columnIndex) {
    Vec vec0 = fr.vec(columnIndex);
    Vec v;
    if (vec0.isCategorical()) {
      v = Vec.makeSeq(0, (long) vec0.domain().length, true);
      v.setDomain(vec0.domain());
      DKV.put(v);
    } else {
      UniqTask t = new UniqTask().doAll(vec0);
      int nUniq = t._uniq.size();
      final AstGroup.G[] uniq = t._uniq.keySet().toArray(new AstGroup.G[nUniq]);
      v = Vec.makeZero(nUniq, vec0.get_type());
      new MRTask() {
        @Override
        public void map(Chunk c) {
          int start = (int) c.start();
          for (int i = 0; i < c._len; ++i) c.set(i, uniq[i + start]._gs[0]);
        }
      }.doAll(v);
    }
    return new Frame(v);
  }
399735
private ValFrame cleanCategorical(Frame oldfr, Frame newfr) {
    final boolean categoricalOK = categoricalOK();
    final Vec oldvecs[] = oldfr.vecs();
    final Vec newvecs[] = newfr.vecs();
    Futures fs = new Futures();
    for (int i = 0; i < oldvecs.length; i++)
      if ((oldvecs[i].isCategorical() && !categoricalOK)) { // categorical are OK (op is EQ/NE)
        Vec cv = newvecs[i].makeCon(Double.NaN);
        newfr.replace(i, cv).remove(fs);
      }
    fs.blockForPending();
    return new ValFrame(newfr);
  }