Files
spark/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs
T
2023-09-04 16:25:40 -04:00

128 lines
4.3 KiB
C#

using System;
using BCG.Utilities;
using System.Collections;
namespace BCG
{
/// <summary>
/// All rows with the same values for designated columns are stored in
/// the same IBlueRowCollection. Such a structure will be useful for operations
/// such as GroupBy, Union and finding distinct rows in a table
/// </summary>
public class RowDictionaryTable : IBlueRowCollection
{
protected IDictionary myRowCollections = new Hashtable ();
protected BlueTable myLinearCollection;
protected string[] myCols;
protected IBlueTableSchema mySchema;
protected RowDictionaryTable () {}
public RowDictionaryTable (IBlueRowCollection rows, params string[] cols)
: this (rows.schema, cols)
{
foreach (IBlueRow row in rows)
addRow (row);
}
public RowDictionaryTable (IBlueTableSchema schema, params string[] cols)
{
mySchema = schema;
myCols = cols;
foreach (string col in myCols)
{
if (mySchema[col] == null){}
// BlueEventLog.logAssert (false,"Unknown col " + col + " in " + mySchema.tableName);
}
myLinearCollection = new BlueTable (mySchema);
}
public virtual void addRow (IBlueRow row)
{
BlueTable rows = (BlueTable)getGroup (row);
if (rows == null)
{
//i.e we dont have any group for this row => we need to create a new group
rows = new BlueTable (mySchema);
myRowCollections [myRowToString (row)] = rows;
}
rows.addRow (row);
myLinearCollection.addRow (row);
}
public virtual void removeRow (IBlueRow row)
{
BlueTable rowGroup = (BlueTable)getGroup (row);
// BlueEventLog.logAssert (rowGroup != null && rowGroup.Count > 0);
rowGroup.deleteRow (row);
myLinearCollection.deleteRow (row);
if (rowGroup.Count == 0)
myRowCollections.Remove (myRowToString (row));
//we dont keep any groups with no rows, if row count goes to 0, we delete the group
//in its entirety
}
public void removeGroup (IBlueRow representativeRow)
{
IBlueRowCollection rowGroup = getGroup (representativeRow);
// BlueEventLog.logAssert (rowGroup != null && rowGroup.Count > 0);
foreach (IBlueRow row in rowGroup)
myLinearCollection.deleteRow (row);
myRowCollections.Remove (myRowToString (representativeRow));
}
public void clear ()
{
myLinearCollection.clear ();
myRowCollections.Clear ();
}
/// <summary>
/// Retuns a group to which the row belong. If theres no such group a null is returned
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
//public IBlueRowCollection getGroup (IBlueRow row) {return getGroup (
public IBlueRowCollection getGroup (IBlueRow r) {return getGroup (myRowToString (r));}
public ICollection getGroups () {return myRowCollections.Values;}
private bool myAreRowsEqual (IBlueRow r1, IBlueRow r2)
{
bool retVal = true;
foreach (string col in myCols)
retVal &= r1[col].Equals(r2[col]);
return retVal;
}
public IBlueRowCollection getGroup (string repString)
{
IBlueRowCollection retVal = myRowCollections[repString] != null ?
(IBlueRowCollection) myRowCollections[repString] : null;
return retVal;
}
private string myRowToString (IBlueRow r) {return BlueDataUtility.rowToString (r, myCols);}
#region IBlueRowCollection members
public IBlueRow this[int id] {get{return myLinearCollection[id];}}
public IBlueTableSchema schema {get{return mySchema;}}
public bool containsID (int id) {return myLinearCollection.containsID (id);}
#endregion
#region ICollection members
public int Count {get{return myLinearCollection.Count;}}
public bool IsSynchronized {get{return false;}}
public object SyncRoot {get{return this;}}
public void CopyTo (Array array, int index) {myLinearCollection.CopyTo (array, index);}
#endregion
#region IEnumerable members
public IEnumerator GetEnumerator() {return myLinearCollection.GetEnumerator ();}
#endregion
}
}