Data Structure for a table? - data-structures

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.

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

Related

Algorithm to bucket set of numbers based on buckets sum

I have a set of items that I would like to bucket into N different buckets. Each item has a property associated with it (size) and I would like the sum of this property in each bucket to be roughly equal. What is the best way to determine this? Note the range of the size on the items is fairly large, in the data set I'm using the smallest size is 1 and the largest is 325,220.
Example:
Item A - size 5
Item B - size 10
Item C - size 8
Item D - size 16
Item E - size 7
If I wanted to group these into 3 buckets I would want
Bucket 1: A, B
Bucket 2: C, E
Bucket 3: D
I ended up implementing the complete greedy algorithm described in the paper linked by Joe Farrel. The full C# code I used is below:
public class Item
{
public int Id { get; }
public int Size { get; }
public Item(int id, int size)
{
Id = id;
size = size;
}
}
public class Partition
{
public int Index { get; }
public ImmutableList<Item> Items { get; } = ImmutableList<Item>.Empty;
public int Sum { get; }
public Partition(int index)
{
Index = index;
}
private Partition(int index, ImmutableList<Item> items, int sum)
{
Index = index;
Item = items;
Sum = sum;
}
public Partition Add(Item item) => new Partition(Index, Items.Add(item), Sum + item.Size);
public static double AverageDifference(ImmutableList<Partition> partitions)
{
var differences = new List<int>();
for (var i = 0; i < partitions.Count; i++)
{
var partition = partitions[i];
var otherPartitions = partitions.RemoveAt(i);
foreach (var otherPartition in otherPartitions)
{
differences.Add(Math.Abs(partition.Sum - otherPartition.Sum));
}
}
return differences.Average();
}
}
public class Node
{
public Item Item { get; set; }
public int Partition { get; set; }
public Node[] Children { get; set; }
}
private (Node tree, int totalSum) InitTree(IEnumerable<Item> items)
{
var root = new Node();
var totalSum = 0;
Node[] previousLevel = {root};
foreach (var item in items.OrderByDescending(i => i.Size))
{
totalSum += item.Size;
var currentLevel = new Node[_numPartitions];
for (var i = 0; i < _numPartitions; i++)
{
currentLevel[i] = new Node
{
Item = item,
Partition = i
};
}
foreach (var node in previousLevel)
{
node.Children = currentLevel;
}
previousLevel = currentLevel;
}
return (root, totalSum);
}
private ImmutableList<Partition> GetPartitions(Node tree, int totalSum)
{
var partitions = ImmutableList<Partition>.Empty;
for (var i = 0; i < _numPartitions; i++)
{
partitions = partitions.Add(new Partition(i));
}
return TraverseTree(tree, partitions, totalSum, double.MaxValue, ImmutableList<Partition>.Empty);
}
private ImmutableList<Partition> TraverseTree(Node node, ImmutableList<Partition> partitions, int totalSum, double bestDifference, ImmutableList<Partition> bestPartitions)
{
var currentPartitions = partitions;
if (node.Item != null) // skip root
{
// place item into its partition
var updatedPartition = currentPartitions[node.Partition].Add(node.Item);
currentPartitions = currentPartitions.SetItem(node.Partition, updatedPartition);
}
// if this is a leaf, partition is complete
if (node.Children == null)
{
return currentPartitions;
}
// terminate path if partition is sufficiently bad
var largestSum = currentPartitions.Max(p => p.Sum);
if (largestSum - (totalSum - largestSum) / (_numPartitions - 1) >= bestDifference)
{
return null;
}
// contintue to traverse tree in ascending partition size order
foreach (var partition in currentPartitions.OrderBy(p => p.Sum))
{
var nextNode = node.Children[partition.Index];
var nextPartitions = TraverseTree(nextNode, currentPartitions, totalSum, bestDifference, bestPartitions);
if (nextPartitions == null) // path was terminated
{
continue;
}
// if we hit a perfect parition set, return it
var nextDifference = Partition.AverageDifference(nextPartitions);
if (nextDifference <= 1)
{
return nextPartitions;
}
// hold on to the best partition
if (nextDifference < bestDifference)
{
bestDifference = nextDifference;
bestPartitions = nextPartitions;
}
}
return bestPartitions;
}
_numPartitions = 4
var items = GetItems()
var (tree, totalSum) = InitTree(items);
var partitions = GetPartitions(tree, totalSum);
My answer to this question on laying out pictures might be able to be adapted.
Pictures with height become items with size. Columns per page becomes buckets.
The algorithm has three parts: first-fit; greedy-swapping; and reverse sorting, some may be of more use than others with your data.

JFace Tableviewer multiple filters one textbox

I have problem regarding my Filtering of my Table. I'm using a ViewerFilter and override the select Method to fitler that Table. The Filtertext itself is entered via a Textfield.
So now to my problem. For example my table looks like the following:
column
123
124
In my textfield the user can enter the columnname=data1,data2 to show all rows which have either data1 or data2 as data. so in my above example if the user enters column=123,124 both rows should still be visible. The problem here is that I refresh my tableviewer after each entered character. So when the user enters column=123 the Table only shows one column. When adding ,124 to the filtertext I filter my already filtered table. So no data gets shown at the end. How can I still filter the original Tabledata?
#Override
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
final String filterString = filterText.getText().toLowerCase();
if (filterString.length() == 0) { return true; }
final mydata myData= (mydata) element;
if (filterString.matches("columnName" + ".+")) {
index = filterString.indexOf("columnName" + ".+");
evaluateText(myData, filterString, i, index + tableColumnsText[i].length())
}
public boolean evaluateText(final mydata data, final String filterText, final int beginningIndex) {
subString = filterText.substring(beginningIndex, filterText.length());
return evaluateString(data.getString(), subString);
}
public boolean evaluateString(final String cellString, final String commaString) {
int countSubstrings = 0;
final String[] items = commaString.split(",");
countSubstrings = items.length;
for (final String s : items) {
if (s.length() != 0) {
if (!cellString.contains(s)) { return false; }
}
}
return true;
}
So I tried to filter out the main components of the method. Can I somehow access the unfiltered table?

ascending descending table column with combobox in swt?

I am create eclipse RCP application and use SWT table and i am trying to ascending descending column value but not working in column shell value is not change so please help me.
my column value (cell value) is combo box , how to sort column combo value in my table.
public static void main(String[] args) {
int size = 5;
Random random = new Random();
final int[][] data = new int[size][];
for (int i = 0; i < data.length; i++) {
data[i] = new int[] { i, random.nextInt() };
}
// create a virtual table to display data
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.VIRTUAL);
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setItemCount(size);
final TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Key");
column1.setWidth(200);
final TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Value");
column2.setWidth(200);
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event e) {
TableEditor fd_editor = new TableEditor(table);
fd_editor.grabHorizontal = true;
CCombo combo = new CCombo(table, SWT.CHECK);
combo.add("ABC");
combo.add("XYZ");
combo.add("PQR");
combo.add("BABA");
combo.add("PAVAN");
combo.add("RAJA");
combo.select(1);
TableItem item = (TableItem) e.item;
int index = table.indexOf(item);
int[] datum = data[index];
item.setText(0, Integer.toString(datum[0]));
fd_editor.setEditor(combo, item, 1);
}
});
// Add sort indicator and sort data when column selected
Listener sortListener = new Listener() {
public void handleEvent(Event e) {
// determine new sort column and direction
TableColumn sortColumn = table.getSortColumn();
TableColumn currentColumn = (TableColumn) e.widget;
int dir = table.getSortDirection();
if (sortColumn == currentColumn) {
dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
} else {
table.setSortColumn(currentColumn);
dir = SWT.UP;
}
// sort the data based on column and direction
final int index = currentColumn == column1 ? 0 : 1;
final int direction = dir;
Arrays.sort(data, new Comparator() {
public int compare(Object arg0, Object arg1) {
int[] a = (int[]) arg0;
int[] b = (int[]) arg1;
if (a[index] == b[index])
return 0;
if (direction == SWT.UP) {
return a[index] < b[index] ? -1 : 1;
}
return a[index] < b[index] ? 1 : -1;
}
});
// update data displayed in table
table.setSortDirection(dir);
table.clearAll();
}
};
column1.addListener(SWT.Selection, sortListener);
column2.addListener(SWT.Selection, sortListener);
table.setSortColumn(column1);
table.setSortColumn(column2);
table.setSortDirection(SWT.DOWN);
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

Flatten LINQ collection object with nested object collections

This is a tricky one. I an trying to flatten a LINQ object collection. Each item in the collection has the potential of having two collections of other objects. See the example below.
public class DemoClass
{
public string Name {get; set;}
public string Address {get; set;}
public List<Foo> Foos = new List<Foo>();
public List<Bar> Bars = new List<Bars>();
}
What I had been doing is this using this code block to flatten this object
var output = from d in DemoClassCollection
from f in d.Foos
from b in d.Bars
select new {
d.Name,
d.Address,
f.FooField1,
f.FooField2,
b.BarField1,
b.BarField2
};
But the problem I'm having is that the result I get is only those DemoClass objects that have objects in the Foos and Bars collections. I need to get all objects in the DemoClass regardless if there are objects in the Foos and Bars collections.
Any help would be greatly appreciated.
Thanks!
Sounds like you might want to use DefaultIfEmpty:
var output = from d in DemoClassCollection
from f in d.Foos.DefaultIfEmpty()
from b in d.Bars.DefaultIfEmpty()
select new {
d.Name,
d.Address,
FooField1 = f == null ? null : f.FooField1,
FooField2 = f == null ? null : f.FooField2,
BarField1 = b == null ? null : b.BarField1,
BarField2 = b == null ? null : b.BarField2
};
Looks like a left outer join in Linq will work (http://msdn.microsoft.com/en-us/library/bb397895.aspx
var output = from d in DemoClassCollection
from f in d.Foos.DefaultIfEmpty()
from b in d.Bars.DefaultIfEmpty()
select new {
d.Name,
d.Address,
f.FooField1,
f.FooField2,
b.BarField1,
b.BarField2
};
I believe you can implement an IComparer to perform custom JOINS or UNIONS in linq based on how you implement the CompareTo() method
From MSDN: http://msdn.microsoft.com/en-us/library/system.icomparable.aspx
using System;
using System.Collections;
public class Temperature : IComparable
{
// The temperature value
protected double temperatureF;
public int CompareTo(object obj) {
if (obj == null) return 1;
Temperature otherTemperature = obj as Temperature;
if (otherTemperature != null)
return this.temperatureF.CompareTo(otherTemperature.temperatureF);
else
throw new ArgumentException("Object is not a Temperature");
}
public double Fahrenheit
{
get
{
return this.temperatureF;
}
set {
this.temperatureF = value;
}
}
public double Celsius
{
get
{
return (this.temperatureF - 32) * (5.0/9);
}
set
{
this.temperatureF = (value * 9.0/5) + 32;
}
}
}
public class CompareTemperatures
{
public static void Main()
{
ArrayList temperatures = new ArrayList();
// Initialize random number generator.
Random rnd = new Random();
// Generate 10 temperatures between 0 and 100 randomly.
for (int ctr = 1; ctr <= 10; ctr++)
{
int degrees = rnd.Next(0, 100);
Temperature temp = new Temperature();
temp.Fahrenheit = degrees;
temperatures.Add(temp);
}
// Sort ArrayList.
temperatures.Sort();
foreach (Temperature temp in temperatures)
Console.WriteLine(temp.Fahrenheit);
}
}
// The example displays the following output to the console (individual
// values may vary because they are randomly generated):
// 2
// 7
// 16
// 17
// 31
// 37
// 58
// 66
// 72
// 95

How to retrieve ordering information from IQueryable object?

Let's say, I have an instance of IQueryable. How can I found out by which parameters it was ordered?
Here is how OrderBy() method looks like (as a reference):
public static IOrderedQueryable<T> OrderBy<T, TKey>(
this IQueryable<T> source, Expression<Func<T, TKey>> keySelector)
{
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(
Expression.Call(null,
((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(
new Type[] { typeof(T), typeof(TKey) }
),
new Expression[] { source.Expression, Expression.Quote(keySelector) }
)
);
}
A hint from Matt Warren:
All queryables (even IOrderedQueryable's) have expression trees underlying them that encode the activity they represent. You should find using the IQueryable.Expression property a method-call expression node representing a call to the Queryable.OrderBy method with the actual arguments listed. You can decode from the keySelector argument the expression used for ordering. Take a look at the IOrderedQueryable object instance in the debugger to see what I mean.
This isn't pretty, but it seems to do the job:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Forms;
public class Test
{
public int A;
public string B { get; set; }
public DateTime C { get; set; }
public float D;
}
public class QueryOrderItem
{
public QueryOrderItem(Expression expression, bool ascending)
{
this.Expression = expression;
this.Ascending = ascending;
}
public Expression Expression { get; private set; }
public bool Ascending { get; private set; }
public override string ToString()
{
return (Ascending ? "asc: " : "desc: ") + Expression;
}
}
static class Program
{
public static List<QueryOrderItem> GetQueryOrder(Expression expression)
{
var members = new List<QueryOrderItem>(); // queue for easy FILO
GetQueryOrder(expression, members, 0);
return members;
}
static void GetQueryOrder(Expression expr, IList<QueryOrderItem> members, int insertPoint)
{
if (expr == null) return;
switch (expr.NodeType)
{
case ExpressionType.Call:
var mce = (MethodCallExpression)expr;
if (mce.Arguments.Count > 1)
{ // OrderBy etc is expressed in arg1
switch (mce.Method.Name)
{ // note OrderBy[Descending] shifts the insertPoint, but ThenBy[Descending] doesn't
case "OrderBy": // could possibly check MemberInfo
members.Insert(insertPoint, new QueryOrderItem(mce.Arguments[1], true));
insertPoint = members.Count; // swaps order to enforce stable sort
break;
case "OrderByDescending":
members.Insert(insertPoint, new QueryOrderItem(mce.Arguments[1], false));
insertPoint = members.Count;
break;
case "ThenBy":
members.Insert(insertPoint, new QueryOrderItem(mce.Arguments[1], true));
break;
case "ThenByDescending":
members.Insert(insertPoint, new QueryOrderItem(mce.Arguments[1], false));
break;
}
}
if (mce.Arguments.Count > 0)
{ // chained on arg0
GetQueryOrder(mce.Arguments[0], members, insertPoint);
}
break;
}
}
static void Main()
{
var data = new[] {
new Test { A = 1, B = "abc", C = DateTime.Now, D = 12.3F},
new Test { A = 2, B = "abc", C = DateTime.Today, D = 12.3F},
new Test { A = 1, B = "def", C = DateTime.Today, D = 10.1F}
}.AsQueryable();
var ordered = (from item in data
orderby item.D descending
orderby item.C
orderby item.A descending, item.B
select item).Take(20);
// note: under the "stable sort" rules, this should actually be sorted
// as {-A, B, C, -D}, since the last order by {-A,B} preserves (in the case of
// a match) the preceding sort {C}, which in turn preserves (for matches) {D}
var members = GetQueryOrder(ordered.Expression);
foreach (var item in members)
{
Console.WriteLine(item.ToString());
}
// used to investigate the tree
TypeDescriptor.AddAttributes(typeof(Expression), new[] {
new TypeConverterAttribute(typeof(ExpandableObjectConverter)) });
Application.Run(new Form
{
Controls = {
new PropertyGrid { Dock = DockStyle.Fill, SelectedObject = ordered.Expression }
}
});
}
}

Resources