2011-04-23 76 views
1

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

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平台的编译器

+0

如果你重载operator []或operator(),你可以用C++“索引”一个类。 – 2011-04-23 23:44:48

回答

1

用于C++的等效。将过载operator[]

一个java当量将是一个方法,例如T get(int index){ ... }

AFAICS,索引器只是一个方便,让您使用括号代替getter方法od(即语法糖)。你真的需要他们吗?