标签: ordered-map

Scala Map实现按插入顺序保留条目?

在Java中,我LinkedHashMap用于此目的.Java的文档LinkedHashMap很清楚,它具有"可预测的迭代顺序",我在Scala中需要相同的东西.

Scala有ListMapLinkedHashMap,但他们这样做究竟是什么文件很糟糕.

问题:Scala LinkedHashMapListMap实现是否用于此目的?如果没有,除了LinkedHashMap直接使用Java之外还有哪些其他选项?

scala scala-2.8 scala-collections ordered-map

39
推荐指数
3
解决办法
2万
查看次数

如何使用运行时定义的比较器定义有序映射/集?

这类似于How do I use a custom comparator function with BTreeSet? 但就我而言,直到运行时我才会知道排序标准。可能的标准很广泛,并且不能进行硬编码(想想像按到目标的距离排序按有效负载中的特定字节或其组合排序)。创建地图/集合后,排序标准不会更改。

我看到的唯一替代方案是:

  • 使用 a Vec,但 log(n) 插入和删除至关重要
  • 用排序标准(直接或间接)包装每个元素,但这似乎很浪费

这对于标准 C++ 容器std::map/是可能的,但对于 Rust 的/std::set似乎不可能。标准库或其他板条箱中是否有替代方案可以做到这一点?或者我必须自己实施这个?BTreeMapBTreeSet


我的用例是一个类似数据库的系统,其中集合中的元素由模式定义,例如:

Element {
    FIELD x: f32
    FIELD y: f32
    FIELD z: i64

    ORDERBY z
}
Run Code Online (Sandbox Code Playgroud)

但由于模式是用户在运行时定义的,因此元素存储在一组字节 ( BTreeSet<Vec<u8>>) 中。同样,元素的顺序是用户定义的。所以我会给的比较器BTreeSet看起来像|a, b| schema.cmp(a, b)。硬编码后,上面的示例可能类似于:

fn cmp(a: &Vec<u8>, b: &Vec<u8>) -> Ordering {
    let a_field = self.get_field(a, 2).as_i64();
    let b_field = self.get_field(b, 2).as_i64(); …
Run Code Online (Sandbox Code Playgroud)

rust ordered-map ordered-set

9
推荐指数
1
解决办法
803
查看次数

如何在cython中使用unordered_map?

我想要一个分步指南,如何在 cython 中使用 unordered_map。

我已经从https://gist.github.com/ikuyamada/3265267将文件 unordered_map.pxd 包含到 Cython/Includes/libcpp 中,并使用了其他 3 个文件:

主要.py:

import pyximport;
pyximport.install()
from foo import F

F()
Run Code Online (Sandbox Code Playgroud)

foo.pyx:

from libcpp.unordered_map cimport unordered_map


def F():
    cdef unordered_map[int, int] my_map
    my_map[1]=11
    my_map[2]=12
    print my_map[1],my_map[2]
Run Code Online (Sandbox Code Playgroud)

foo.pyxbld: (将 foo.pyx 编译成 C++)

def make_ext(modname, pyxfilename):
    from distutils.extension import Extension
    return Extension(name=modname,
                     sources=[pyxfilename],
                     language='C++')
Run Code Online (Sandbox Code Playgroud)

当我运行 test.py 时,出现错误:

foo.cpp
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
C:\Users\kitov\.pyxbld\temp.win-amd64-2.7\Release\pyrex\foo.cpp(316) : …
Run Code Online (Sandbox Code Playgroud)

c++ python cython missing-data ordered-map

5
推荐指数
1
解决办法
4691
查看次数

如何获取有序集/有序映射的最大值和最小值?

Rust 的有序集是一个BTreeSet

use std::collections::BTreeSet;

// Type inference lets us omit an explicit type signature (which
// would be `BTreeSet<&str>` in this example).
let mut books = BTreeSet::new();

// Add some books.
books.insert("A Dance With Dragons");
books.insert("To Kill a Mockingbird");
books.insert("The Odyssey");
books.insert("The Great Gatsby");
Run Code Online (Sandbox Code Playgroud)

有序映射是一个BTreeMap.

由于 set 和 map 是有序的,因此应该有一种方法可以获取包含的最大和最小元素。你怎么得到它们?

b-tree rust ordered-map ordered-set

5
推荐指数
1
解决办法
876
查看次数

Immutable JS OrderedMap: Insert a new entry after a given Key

I have an Immutable OrderedMap as follows:

pairs: Immutable.OrderedMap({"Key1":"Value1","Key2":"Value2","Key4":"Value4"})
Run Code Online (Sandbox Code Playgroud)

I need to insert ["Key3":"Value3"] after ["Key2":"Value2"] dynamically.

I thought

pairs.MergeIn(['Key2'],OrderedMap({"Key3":"Value3"}))  
Run Code Online (Sandbox Code Playgroud)

will serve the purpose but not working.

I tried

const newPairs=Immutable.OrderedMap();
newPairs.forEach(function(value,key,map)){
   if(key=="Key4")
     newPairs.set('Key3','Value3')
   newPairs.set(key,value')
});     
Run Code Online (Sandbox Code Playgroud)

But I know it's a stupid code which won't work as newPairs is immutable and newPairs will be still empty. So is there any Immutable way of OrderedMap.addBefore(Key,key,value)?

javascript ordered-map immutable.js

1
推荐指数
1
解决办法
2856
查看次数

为什么我们没有地图的哈希和捕食函子?

在的情况下,unordered_map我们定义了hashpred每当我们使用仿函数user-defined键。

地图的模板语法如下:

template < class Key,                                     // map::key_type
       class T,                                           // map::mapped_type
       class Compare = less<Key>,                         // map::key_compare
       class Alloc = allocator<pair<const Key,T> >       // map::allocator_type
       > class map;
Run Code Online (Sandbox Code Playgroud)

如果是map,则没有hashand predfunctors选项。我们永远不会发生碰撞的情况map。如果发生冲突,那为什么不使用hashpred函数unordered_map呢?我在这里想念什么吗?

c++ unordered-map ordered-map

1
推荐指数
2
解决办法
54
查看次数

c++ 中 m.erase() 函数的奇怪行为?

int main(){
    map<int, int> m;
    m.insert({1,2});
    m.insert({2,3});
    m.insert({5,10});
    m.erase(m.find(3));
    for(auto &x: m){
        cout<<x.first<<" "<<x.second<<nl;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

1 2
5 10
Run Code Online (Sandbox Code Playgroud)

据我所知,m.find(3)将迭代器返回到m.end()if 键未找到。那为什么要删除 {2,3} 对呢?

c++ dictionary stl ordered-map

0
推荐指数
1
解决办法
70
查看次数