English 中文(简体)
C# non-evilspeup?
原标题:C# non-evil fast array lookup?

我愿有大量班级例子来回同样的数据领域,例如执行:

foreach (SomeClass sc in SomeClasses)
{
    System.Console.WriteLine(sc.GetData("1st field"));
    System.Console.WriteLine(sc.GetData("Another field"));
    System.Console.WriteLine(sc.GetData("and another"));
}

// ---- inside SomeClass:

Dictionary<string, string> myData;

public string GetData(string field)
{
    return myData[field];
}

我所不喜欢的是,比如说,必须反复地进行扼杀、寻找和配对(我假定假肢是如何运作的)。 我确实想找到更好的办法。

我从C界认为,把所有领域都指定为独一无二的集束钥匙,因此我可以改变成一个阵列:

// ---- inside SomeClass:

string[] MyData;

public string GetData(int field_key)
{
    return MyData[field_key];
}

现在,实地调查是有效的,但是,在这种“时代是邪恶”的时代,它只是一种感觉是正确的,而且很容易处理实地——主人——的混乱。

我不知道我是否在这里追逐业绩,我是否想找到一个既高效又干净的设计。

建议

最佳回答

由于在汇编时间时不知道这些田地,但动态和用户可以预测,因此,我会稍微修改你的榜样方案,以使用一系列财产。 然后,我主张采取与你相似的做法,但采用你自己的习俗类别(这里称为“我的财富”),而不是扼杀。 业绩至少与(也许比)扼制方法一样好,但好处是,这种做法使你具有更大的灵活性:如果你最终出于业绩原因决定你需要使用一个阵列或名单办法,你可以很容易地将一个阵列指数纳入你的“我”类别。 你们必须改变GetData的实施,而不是你们的号召。

public static void Test1() {
  SomeClass[] SomeClasses; //created somehow

  //in real life, this would be determined dynamically
  var properties=new[] {SomeClass.FirstField, SomeClass.AnotherField, SomeClass.AndAnother};

  foreach(var sc in SomeClasses) {
    foreach(var property in properties) {
      Console.WriteLine(sc.GetData(property));
    }
  }
}

public class SomeClass {
  public static readonly MyProperty FirstField=new MyProperty();
  public static readonly MyProperty AnotherField=new MyProperty();
  public static readonly MyProperty AndAnother=new MyProperty();

  private readonly Dictionary<MyProperty, string> myData=new Dictionary<MyProperty, string>();

  public string GetData(MyProperty property) {
    return myData[property];
  }
}

//default implementation of Equals and GetHashCode are fine here
public class MyProperty {}

HOWEVER, since your target application is really about collecting a set of dynamic and user configurable property getters, maybe you really want to make some Funcs? Code like the below will be very fast, and it still has the ability you want, namely it allows you to make a little dynamic, user-configurable list of property getters.

public static void Test2() {
  SomeClass[] SomeClasses; //created somehow

  //in real life, this would be determined dynamically
  var getters=new[] {SomeClass.FirstField, SomeClass.AnotherField, SomeClass.AndAnother};
  foreach(var sc in SomeClasses) {
    foreach(var getter in getters) {
      System.Console.WriteLine(getter(sc));
    }
  }
}

public class SomeClass {
  public static readonly Func<SomeClass, string> FirstField=sc => sc.field0;
  public static readonly Func<SomeClass, string> AnotherField=sc => sc.field1;
  public static readonly Func<SomeClass, string> AndAnother=sc => sc.field2;

  private string field0;
  private string field1;
  private string field2;
}
问题回答

Why don t you want a dictionary look-up? A very efficient implementation of a dictionary would be an index look up of the hash in an array. So the underlying implementation could boil down to the code in your second example. This would make it O(1)

使用假肢

如果你的情况相同,为什么不只使用财产?

foreach (SomeClass sc in SomeClasses)
{
    System.Console.WriteLine(sc.FirstField);
    System.Console.WriteLine(sc.AnotherField);
    System.Console.WriteLine(sc.AndAnother);
}

首先,如果你确信这实际上对你来说是一个实事求是的问题,那么,是,你正在追逐业绩的幽灵,而你目前的执行是罚款的。

但是,如果你在剖面时发现你确实需要使这一守则更快,那么你看来是罚款。 “宗教是邪恶”,在公众交往中尤其如此,但为了执行而使用这些疾病是很不错的。

我会改变你的代码,具体做法是:创建<条码>>。 不含<条码>的田地和使用。 它既迅速又可读。 如果在汇编时不知道这些领域,则使用<代码>int予以罚款。 如果你在汇编时间时确实知道一些领域,你可以为其使用静态财产。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签