2011-04-23 77 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(即語法糖)。你真的需要他們嗎?