Search code examples
data-structurestabular

Data Structure for a table?


I need to create a table with many rows and more than two columns. Suggest a good and fast data structure. I won't be updating and deleting some entries from that table. I will be using just the lookup functions.
For Example, I have a table:

 
   | column 1 | column 2 | column 3|
a  | asd      | awd      | asfc    |
b  | asgf     | aasf     | asgfc   |

I have:

String a = "column 1"
String b = "b"
String c = find(a,b);

At the end the value in c should be asgf.


Solution

  • You must use associative arrays (so called Dictionaries), based on hash-tables. Average time-complexity for lookup — O(1 + n/k) and O(n) in worst case. You must organize your Table as dictionary of columns (with column names as key). And Column must be dictionary of values (with row names as 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") );
            }
        }
    }