在Java中,我LinkedHashMap用于此目的.Java的文档LinkedHashMap很清楚,它具有"可预测的迭代顺序",我在Scala中需要相同的东西.
Scala有ListMap和LinkedHashMap,但他们这样做究竟是什么文件很糟糕.
问题:Scala LinkedHashMap或ListMap实现是否用于此目的?如果没有,除了LinkedHashMap直接使用Java之外还有哪些其他选项?
这类似于How do I use a custom comparator function with BTreeSet? 但就我而言,直到运行时我才会知道排序标准。可能的标准很广泛,并且不能进行硬编码(想想像按到目标的距离排序或按有效负载中的特定字节或其组合排序)。创建地图/集合后,排序标准不会更改。
我看到的唯一替代方案是:
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) 我想要一个分步指南,如何在 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) Rust 的有序集是一个BTreeSet:
Run Code Online (Sandbox Code Playgroud)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");
有序映射是一个BTreeMap.
由于 set 和 map 是有序的,因此应该有一种方法可以获取包含的最大和最小元素。你怎么得到它们?
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)?
在的情况下,unordered_map我们定义了hash和pred每当我们使用仿函数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。如果发生冲突,那为什么不使用hash和pred函数unordered_map呢?我在这里想念什么吗?
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} 对呢?
ordered-map ×7
c++ ×3
ordered-set ×2
rust ×2
b-tree ×1
cython ×1
dictionary ×1
immutable.js ×1
javascript ×1
missing-data ×1
python ×1
scala ×1
scala-2.8 ×1
stl ×1