Data structure for storing data indexed by column and row indices

I have a list of objects to display to the user. Basically, this is a set of user permissions. I want to display a table with users as columns and actions as rows. Then permission will be displayed at the intersection between the user and the action.

I am looking for a way to represent this data in a C # data structure. This will allow access using the user (row) and action (row), as well as provide a default value for each "cell". Ideally, it will be linked to Grid Control with automatic column creation.

An array would be large, but it only allows integers as indices. This will also require re-creating and copying the array when adding a new row / column.

I was thinking about creating an object Dictionary<String, Dictionary<String, Permission>>, but it requires a lot of error checking and does not work with datagrid controls.

Is there an existing open source project for such a data structure? Basically, this is just an extended version of the "two-key value" structure.

thank

+5
source share
1 answer

, , DataTable. A DataTable , , . , , , . , , DataTable , !

PermissionMap, a DataTable, , . , - DataTable, :

  • ,
  • AddUser
  • A ,

:

public enum Permission
{
    Default = 0,
    Disallow = 0,
    Allow = 1,
}

public class PermissionMap : DataTable
{
    private Dictionary<string, int> actionMap = new Dictionary<string, int>();

    public PermissionMap(IEnumerable<string> actions)
    {
        Columns.Add(new DataColumn("Action"));
        int i = 0;
        foreach (var action in actions)
        {
            actionMap.Add(action, i++);
            var row = NewRow();
            row["Action"] = action;
            Rows.Add(row);
        }
    }

    public void AddUser(string user)
    {
        Columns.Add(new DataColumn(user));
        foreach (DataRow row in Rows)
            row[user] = Permission.Default;
    }

    public Permission this[string user, string action]
    {
        get { return (Permission)Rows[actionMap[action]][user]; }
        set { Rows[actionMap[action]][user] = value; }
    }
}

:

class Program
{
    static void Main(string[] args)
    {
        var permissionMap = new PermissionMap(new[] { "Read", "Write" });
        permissionMap.AddUser("Bill");
        permissionMap.AddUser("Jane");
        permissionMap["Bill", "Read"] = Permission.Allow;
        permissionMap["Jane", "Write"] = Permission.Allow;
        foreach (DataColumn column in permissionMap.Columns)
        {
            Console.Write(column.ColumnName);
            Console.Write(",");
        }
        Console.WriteLine();
        foreach (DataRow row in permissionMap.Rows)
        {
            foreach (DataColumn column in permissionMap.Columns)
            {
                Console.Write(row[column]);
                Console.Write(",");
            }
            Console.WriteLine();
        }
        Console.ReadKey();
    }
}

:

Action,Bill,Jane,
Read,Allow,Disallow,
Write,Disallow,Allow,
+3

All Articles