2012-06-05 4 views
2

많은 행과 두 개 이상의 열이있는 테이블을 만들어야합니다. 훌륭하고 빠른 데이터 구조를 제안하십시오. 나는 해당 테이블에서 일부 항목을 업데이트하고 삭제하지 않을 것입니다. 난 그냥 조회 기능을 사용하는 것입니다. c의 값이 있어야 asgf 끝에
테이블의 데이터 구조는 무엇입니까?

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

: I 가질

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

예를 들어
는 I 테이블을 갖는다.

+1

완전히 데이터 당신에 따라 달라집니다 그리고 열은

상세 정보 (키와 행 이름) 값의 사전 있어야합니다 저장하려고합니다. 당신이하고 싶은 것에 대한 더 많은 정보를 제공해야합니다. – Config

+2

당신은 무엇을 찾을 것입니까? –

+0

이들은 문자열이됩니다 –

답변

0

해시 테이블을 기반으로하는 연관 배열 (사전이라고 함)을 사용해야합니다. 검색을위한 평균 시간 복잡성 - 최악의 경우 O (1 + n/k) 및 O (n). 표를 열 사전으로 구성해야합니다 (열 이름을 키로 사용). C#에서

http://en.wikipedia.org/wiki/Dictionary_(data_structure) http://en.wikipedia.org/wiki/Hash_table

예 :

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")); 
     } 
    } 
} 
+0

P.S : 중복 행과 열 추가에 대한 예제가 없습니다. –

+0

매우 큰 개체를 각 셀과 행에 할당 할 때 매우 효율적으로 보이지는 않습니다. 또한 열 이름을 키로 복제합니다. 차라리 열 이름에 대한 내부 배열과 레코드 목록의 목록을 가진 클래스를 만들 것입니다. 또한 목록을 더 쉽게 찾아보기 위해 벡터 나 링크 된 목록을 쉽게 바꿀 수 있습니다. –

관련 문제