Class indexer for C++
I'm working on re-implementing Microsoft PINQ framework using C++ and at some point I need "Class indexer" feature of C# in my C++ implementation (and I'm curious if there is some solution to Java too). Can any one suggest a good library or something to use?
The C# class:
/// <summary>
/// PINQAgent class resulting from the Partition operation.
/// Contains a list of epsilon values, and tracks the maximum value.
/// Increments to the maximum are forwarded to the source IQueryable.
/// Requests that do not increment the maximum are accepted.
/// </summary>
/// <typeparam name="K">The type of the key used to partition the data set.</typeparam>
//this feature is called Indexers http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
public class PINQAgentPartition<K> : PINQAgent
{
private PINQAgent target; // agent of data source that has been partitioned.
private double[] maximum; // should be shared
private Dictionary<K, double> table; // dictionary shared among several PINQAgentPartitions.
private K key; // key associated with *this* PINQAgentPartition.
/// <summary>
/// Accepts iff the increment to the maximum value is accepted by the target.
/// </summary>
/// <param name="epsilon">epsilon</param>
/// <returns>Accepts if the increment to the maximum value is accepted by the target.</returns>
public override bool apply(double epsilon)
{
// if we increment the maximum, test and update
if (table[key] + epsilon > maximum[0])
{
if (target.apply((table[key] + epsilon) - maximum[0]))
{
table[key] += epsilon;
maximum[0] = table[key];
return true;
}
return false;
}
// if we were the maximum, and we decrement, re-establish the maximum.
if (table[key] == maximum[0] && epsilon < 0.0)
{
table[key] += epsilon;
maximum[0] = table.Select(x => x.Value).Max();
}
else
开发者_如何学C table[key] += epsilon;
return true;
}
/// <summary>
/// Constructor for PINQAgentPartition
/// </summary>
/// <param name="t">Target PINQAgent</param>
/// <param name="tbl">Table of (key,epsilon) pairs</param>
/// <param name="k">Key associated with this agent</param>
/// <param name="m">Stores a shared maximum between all peers</param>
public PINQAgentPartition(PINQAgent t, Dictionary<K, double> tbl, K k, double[] m)
{
target = (t == null) ? new PINQAgent() : t;
table = tbl;
key = k;
maximum = m;
}
};
Note: I use g++ compiler under Linux platform
An equivalent for C++ would be to overload operator[]
A java equivalent would be a method such as T get(int index){ ... }
AFAICS, indexers are just a convenience to let you use brackets instead of a getter method (i.e. syntactic sugar). Is there a reason why you really need them?
精彩评论