////////////////////////////////////////////////////////////////////////// // Building ////////////////////////////////////////////////////////////////////////// // Add new column and return builder for column metadata. // Columns cannot be added after adding the first row. public HDictBuilder addCol(string name) { if (m_rows.Count > 0) { throw new InvalidOperationException("Cannot add cols after rows have been added"); } if (!HDict.isTagName(name)) { throw new ArgumentException("Invalid column name: " + name, "name"); } BCol col = new BCol(name); m_cols.Add(col); return(col.Meta); }
// Convert current state to an immutable HGrid instance public HGrid toGrid() { // meta HDict meta = Meta.toDict(); // cols List <HCol> hcols = new List <HCol>(m_cols.Count); for (int i = 0; i < m_cols.Count; ++i) { BCol bc = (BCol)m_cols[i]; hcols.Add(new HCol(i, bc.Name, bc.Meta.toDict())); } // let HGrid constructor do the rest... return(new HGrid(meta, hcols, m_rows)); }
// Convenience to build grid from array of HDict. // Any null entry will be row of all null cells. public static HGrid dictsToGrid(HDict meta, HDict[] dicts) { HCol colEmpty = new HCol(0, "empty", HDict.Empty); // If empty return an empty cols collection and no values Grid if (dicts.Length == 0) { List <List <HVal> > rowListEmpty = new List <List <HVal> >(); List <HCol> rowEmpty = new List <HCol>(); rowEmpty.Add(colEmpty); return(new HGrid(meta, rowEmpty, rowListEmpty)); } HGridBuilder b = new HGridBuilder(); b.Meta.add(meta); // collect column names - why does this need to be a dictionary (hashmap in the java code)? // it only stores the col name twice. Dictionary <string, string> colsByName = new Dictionary <string, string>(); for (int i = 0; i < dicts.Length; ++i) { HDict dict = dicts[i]; if (dict != null) { for (int it = 0; it < dict.size(); it++) { string name = dict.getKeyAt(it, false); if (name != null) { if (!colsByName.Keys.Contains(name)) { colsByName.Add(name, name); b.addCol(name); } } } } } // if all dicts were null, handle special case // by creating a dummy column if (colsByName.Count == 0) { colsByName.Add("empty", "empty"); b.addCol("empty"); } // now map rows int numCols = b.colCount; for (int ri = 0; ri < dicts.Length; ++ri) { HDict dict = dicts[ri]; HVal[] cells = new HVal[numCols]; for (int ci = 0; ci < numCols; ++ci) { if (dict == null) { cells[ci] = null; } else { BCol colatci = b.GetColAt(ci); if (colatci != null) { cells[ci] = dict.get(colatci.Name, false); } } } b.addRow(cells); } return(b.toGrid()); }