简体   繁体   English

什么是Ruby(1.8.7)与C#/。NET中的SortedDictionary类似?

[英]What is Ruby (1.8.7) analog to SortedDictionary in C#/.NET?

I need to hold values in sorted hash in ruby (1.8.7). 我需要将值保存在ruby(1.8.7)中的排序哈希中。 What data structed will fit the best? 构造什么样的数据最合适?

There is nothing in the core library or the standard library now, that will fit your bill. 现在,核心库或标准库中没有适合您的账单。

There is , however, a feature request to add a Red/Black-Tree implementation to Ruby 1.9.3/2.0. 然而, 一个功能请求,红/黑树实现添加到Ruby 1.9.3 / 2.0。

If you are able to force your users to only ever use XRuby or JRuby , you could just use one of the implementations of Java's java.util.SortedMap<K, V> such as java.util.TreeMap<K, V> . 如果能够强迫用户仅使用XRubyJRuby ,则可以只使用Java的java.util.SortedMap<K, V>一种实现,例如java.util.TreeMap<K, V>

If you are able to force your users to only ever use Ruby.NET or IronRuby , you could just use .NET's System.Collections.Generic.SortedDictionary<TKey, TValue> . 如果可以强制用户仅使用Ruby.NETIronRuby ,则可以使用.NET的System.Collections.Generic.SortedDictionary<TKey, TValue>

If you are able to force your users to only ever use MRI or YARV, you could use the Ruby/RBTree library. 如果您可以强制用户仅使用MRI或YARV,则可以使用Ruby/RBTree库。 It might also work on Rubinius or the not-yet-released JRuby 1.6 . 它也可以在Rubinius尚未发布的JRuby 1.6上运行 Note that there seem to be multiple independent updated forks of that library in the wild. 请注意,该库中似乎有多个独立的更新后的派生分支。 It is not obvious, which one of those is the most recent and/or best maintained one. 不明显的是,哪一个是最新的和/或维护得最好的。

The only solution I know of which is guaranteed to be portable, is Kanwei Li's Algorithms and Containers GSoC 2008 project , which actually contains two implementations of a sorted, key-indexed collection: Containers::RBTreeMap based on a Red/Black-Tree and Containers::SplayTreeMap based on a Splay Tree . 我所知道的唯一可以保证可移植的解决方案是李侃威(Kanwei Li)的Algorithms and Containers GSoC 2008项目 ,该项目实际上包含排序的,键索引的集合的两个实现: Containers::RBTreeMap基于Red / Black-Tree基于Containers::SplayTreeMap Tree的Containers::SplayTreeMap

You might have to roll this yourself, if no one else has a better suggestion. 如果没有其他人有更好的建议,则可能需要自己动手做。

class SortedHash
  def initialize
    @data = []
  end

  def [](k)
    @data.find {|kp,vp| kp == k}.last
  end

  def []=(k, v)
    @data.reject! {|kp,vp| kp == k}
    @data << [k, v]
    @data = @data.sort_by {|kp,vp| kp}
  end

  def each(&b)
    @data.each(&b)
  end
end

sh = SortedHash.new
sh[32] = "no"
sh[1] = "later"
sh[99] = "after"

sh.each do |k,v|
  p [k,v]
end

Output: 输出:

[1, "later"]
[32, "no"]
[99, "after"]

Array is sorted by keys, so they can be of any call and you just need to define comparison operators on them. 数组按键排序,因此它们可以是任意调用,您只需要在它们上定义比较运算符即可。

Use the same class in c# SortedDictionary : 在c#SortedDictionary中使用相同的类:

SortedDictionary keyValues = new SortedDictionary(); SortedDictionary keyValues = new SortedDictionary();

        keyValues.Add(5,"sample5");
        keyValues.Add(2, "sample2");
        keyValues.Add(6, "sample6");
        keyValues.Add(8, "sample8");
        keyValues.Add(9, "sample9");
        keyValues.Add(1, "sample1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM