New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Rust HashMap test #1

Merged
merged 1 commit into from Jul 25, 2018
Jump to file or symbol
Failed to load files and symbols.
+115 −1
Diff settings

Always

Just for now

Add a Rust HashMap test

  • Loading branch information...
cuviper committed Jul 25, 2018
commit bcec90c5d782333537bf150a87fe25d1180bb764
View
@@ -1,4 +1,4 @@
all: build/treadmill build/glib_hash_table build/stl_unordered_map build/boost_unordered_map build/google_sparse_hash_map build/google_dense_hash_map build/qt_qhash build/python2_dict build/python3_dict build/ruby_hash build/khash
all: build/treadmill build/glib_hash_table build/stl_unordered_map build/boost_unordered_map build/google_sparse_hash_map build/google_dense_hash_map build/qt_qhash build/python2_dict build/python3_dict build/ruby_hash build/rust_hash_map build/khash
COMMON=Makefile build/.dummy
@@ -38,5 +38,9 @@ build/python3_dict: $(COMMON) src/python3_dict.c src/template.c
build/ruby_hash: $(COMMON) src/ruby_hash.c src/template.c
gcc -O2 -g -lm -I/usr/include/ruby-2.5.0 -I/usr/include/ruby-2.5.0/x86_64-linux-gnu -lruby2.5 src/ruby_hash.c -o build/ruby_hash
build/rust_hash_map: $(COMMON) src/rust_hash_map.c src/rust_hash_map.h src/rust_hash_map.rs src/template.c
rustc -O -g src/rust_hash_map.rs -o build/librust_hash_map.a
gcc -O2 -g -lm -lpthread -ldl src/rust_hash_map.c build/librust_hash_map.a -o build/rust_hash_map
build/khash: $(COMMON) src/khash.c src/template.c
gcc -O2 -g -lm src/khash.c -o build/khash
View
1 README
@@ -10,6 +10,7 @@ First, some prerequisites are:
* google sparsehash
* qt
* gnuplot
* rustc
Now, run:
View
@@ -9,6 +9,7 @@ DESC_stl_unordered_map="GCC 7.2 std::unordered_map"
DESC_python3_dict="Python 3.6.5 (C API) dict"
DESC_qt_qhash="Qt 5.10 QHash"
DESC_ruby_hash="Ruby 2.5 (C API) hash"
DESC_rust_hash_map="Rust 1.27.2 HashMap"
UNITS=" \
boost_unordered_map \
@@ -19,6 +20,7 @@ khash \
python3_dict \
qt_qhash \
ruby_hash \
rust_hash_map \
stl_unordered_map \
"
View
@@ -0,0 +1,11 @@
#include "rust_hash_map.h"
#define SETUP IntHash * hash = new_int_hash(); \
StrHash * str_hash = new_str_hash();
#define INSERT_INT_INTO_HASH(key, value) insert_int_into_hash(hash, key, value)
#define DELETE_INT_FROM_HASH(key) delete_int_from_hash(hash, key)
#define INSERT_STR_INTO_HASH(key, value) insert_str_into_hash(str_hash, key, value)
#define DELETE_STR_FROM_HASH(key) delete_str_from_hash(str_hash, key)
#define LOOKUP_INT(key) \
do { volatile const int64_t * v = lookup_int(hash, key); } while (0);
#include "template.c"
View
@@ -0,0 +1,21 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct IntHash IntHash;
typedef struct StrHash StrHash;
void delete_int_from_hash(IntHash *hash, int64_t key);
void delete_str_from_hash(StrHash *hash, const char *key);
void insert_int_into_hash(IntHash *hash, int64_t key, int64_t value);
void insert_str_into_hash(StrHash *hash, const char *key, int64_t value);
const int64_t *lookup_int(const IntHash *hash, int64_t key);
IntHash *new_int_hash(void);
StrHash *new_str_hash(void);
View
@@ -0,0 +1,75 @@
//! Rust HashMap wrapped in an unsafe C API
//!
//! Generate `rust_hash_map.h` from this using:
//!
//! cbindgen --lang c rust_hash_map.rs
//!
#![crate_type = "staticlib"]
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::ptr;
pub struct IntHash {
map: HashMap<i64, i64>,
}
pub struct StrHash {
map: HashMap<CString, i64>,
}
#[no_mangle]
pub extern "C" fn new_int_hash() -> *mut IntHash {
let hash = Box::new(IntHash {
map: HashMap::new(),
});
Box::leak(hash)
}
#[no_mangle]
pub extern "C" fn insert_int_into_hash(hash: *mut IntHash, key: i64, value: i64) {
unsafe {
(*hash).map.insert(key, value);
}
}
#[no_mangle]
pub extern "C" fn delete_int_from_hash(hash: *mut IntHash, key: i64) {
unsafe {
(*hash).map.remove(&key);
}
}
#[no_mangle]
pub extern "C" fn lookup_int(hash: *const IntHash, key: i64) -> *const i64 {
unsafe {
match (*hash).map.get(&key) {
Some(value) => value,
None => ptr::null(),
}
}
}
#[no_mangle]
pub extern "C" fn new_str_hash() -> *mut StrHash {
let hash = Box::new(StrHash {
map: HashMap::new(),
});
Box::leak(hash)
}
#[no_mangle]
pub extern "C" fn insert_str_into_hash(hash: *mut StrHash, key: *const c_char, value: i64) {
unsafe {
(*hash).map.insert(CStr::from_ptr(key).to_owned(), value);
}
}
#[no_mangle]
pub extern "C" fn delete_str_from_hash(hash: *mut StrHash, key: *const c_char) {
unsafe {
(*hash).map.remove(CStr::from_ptr(key));
}
}
ProTip! Use n and p to navigate between commits in a pull request.