| 1 |
use std::mem::MaybeUninit; |
| 2 |
|
| 3 |
use libc::c_char; |
| 4 |
|
| 5 |
#[repr(C)] |
| 6 |
pub struct HashTable<K, V, const IS_MAP: bool> { |
| 7 |
num_buckets: usize, |
| 8 |
capacity: usize, |
| 9 |
occupied: *mut u8, |
| 10 |
keys: *mut MaybeUninit<K>, |
| 11 |
vals: *mut MaybeUninit<V>, |
| 12 |
} |
| 13 |
|
| 14 |
type Str = *const c_char; |
| 15 |
pub type HashMap<K, V> = HashTable<K, V, true>; |
| 16 |
pub type HashSet<K> = HashTable<K, u8, false>; |
| 17 |
|
| 18 |
impl<K, V, const IS_MAP: bool> HashTable<K, V, IS_MAP> |
| 19 |
{ |
| 20 |
pub fn new() -> Self { |
| 21 |
HashTable { |
| 22 |
num_buckets: 0, |
| 23 |
capacity: 0, |
| 24 |
occupied: std::ptr::null_mut(), |
| 25 |
keys: std::ptr::null_mut(), |
| 26 |
vals: std::ptr::null_mut(), |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// with alias |
| 32 |
type MySet = HashTable<Str, c_char, false>; |
| 33 |
|
| 34 |
#[no_mangle] |
| 35 |
pub extern "C" fn new_set() -> *mut MySet { |
| 36 |
Box::into_raw(Box::new(HashSet::new())) |
| 37 |
} |
| 38 |
|
| 39 |
type SetCallback = unsafe extern "C" fn(key: Str); |
| 40 |
|
| 41 |
#[no_mangle] |
| 42 |
pub unsafe extern "C" fn set_for_each(set: *const MySet, callback: SetCallback) { |
| 43 |
todo!(); |
| 44 |
} |
| 45 |
|
| 46 |
// without alias |
| 47 |
#[no_mangle] |
| 48 |
pub extern "C" fn new_map() -> *mut HashTable<Str, u64, true> { |
| 49 |
Box::into_raw(Box::new(HashMap::new())) |
| 50 |
} |
| 51 |
|
| 52 |
type MapCallback = unsafe extern "C" fn(key: Str, val: u64); |
| 53 |
|
| 54 |
#[no_mangle] |
| 55 |
pub unsafe extern "C" fn map_for_each(map: *const HashTable<Str, u64, true>, callback: MapCallback) { |
| 56 |
todo!(); |
| 57 |
} |