C ++的类索引器

时间:2011-04-23 23:41:12

标签: c# java c++

我正在使用C ++重新实现Microsoft PINQ framework,在某些时候我需要在我的C ++实现中使用"Class indexer" feature of C#(我很好奇是否有一些Java解决方案)。任何人都可以建议一个好的图书馆或者其他东西吗?

C#类:

    /// <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
            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;
    }
};

注意:我在Linux平台下使用g ++编译器

1 个答案:

答案 0 :(得分:1)

C ++的等价物是重载operator[]

java等价物是一种方法,例如T get(int index){ ... }

AFAICS,索引器只是方便你使用括号而不是getter方法(即语法糖)。你有没有理由需要它们?