Data Structure for a table? -
i need create table many rows , more 2 columns. suggest , fast data structure. won't updating , deleting entries table. using lookup functions.
example, have table:
| column 1 | column 2 | column 3| | asd | awd | asfc | b | asgf | aasf | asgfc |
i have:
string = "column 1" string b = "b" string c = find(a,b);
at end value in c should asgf.
you must use associative arrays (so called dictionaries), based on hash-tables. average time-complexity lookup — o(1 + n/k) , o(n) in worst case. must organize table dictionary of columns (with column names key). , column must dictionary of values (with row names key)
more info:
http://en.wikipedia.org/wiki/dictionary_(data_structure) http://en.wikipedia.org/wiki/hash_table
example in c#:
using system; using system.collections; using system.collections.generic; namespace test { public class test { class table { class column { public dictionary<string, string> cells; public column() { cells = new dictionary<string, string>(); } public string find(string rowname) { string resultvalue; if (cells.trygetvalue(rowname, out resultvalue)) return resultvalue; else throw new exception("oops, no such cell"); } } dictionary<string, column> columns; list<string> rownames; public table() { columns = new dictionary<string, column>(); rownames = new list<string>(); } public void addcolumn(string columnname, params string[] values) { column column = new column(); columns.add(columnname, column); // fill new cells int counter = 0; foreach (string rowname in rownames) { if (counter < values.length) column.cells.add(rowname, values[counter]); else column.cells.add(rowname, ""); counter++; } } public void addrow(string rowname, params string[] values) { rownames.add(rowname); // fill new cells int counter = 0; foreach (keyvaluepair<string, column> columnpair in columns) { column column = columnpair.value; if (counter < values.length) column.cells.add(rowname, values[counter]); else column.cells.add(rowname, ""); counter++; } } public string find(string columnname, string rowname) { column resultcolumn; if (columns.trygetvalue(columnname, out resultcolumn)) return resultcolumn.find(rowname); else throw new exception("oops, no such cell"); } } public static void main() { table table = new table(); table.addrow("a"); table.addrow("b"); table.addcolumn("column 1", "asd", "asgf"); table.addcolumn("column 2", "awd", "aasf"); table.addcolumn("column 3", "asfc", "asgfc"); console.writeline(table.find("column 1", "b") ); } } }
Comments
Post a Comment