diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/litemap/examples | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/litemap/examples')
4 files changed, 218 insertions, 0 deletions
diff --git a/third_party/rust/litemap/examples/language_names_hash_map.rs b/third_party/rust/litemap/examples/language_names_hash_map.rs new file mode 100644 index 0000000000..cb0f750b81 --- /dev/null +++ b/third_party/rust/litemap/examples/language_names_hash_map.rs @@ -0,0 +1,46 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +// LiteMap is intended as a small and low-memory drop-in replacement for HashMap. +// +// This example does not use LiteMap. The reader may compare this HashMap example to the +// LiteMap example to showcase analogous operations between HashMap and LiteMap. + +#![no_main] // https://github.com/unicode-org/icu4x/issues/395 + +icu_benchmark_macros::static_setup!(); + +use icu_locid::subtags::{language, Language}; +use std::collections::HashMap; + +const DATA: [(Language, &str); 11] = [ + (language!("ar"), "Arabic"), + (language!("bn"), "Bangla"), + (language!("ccp"), "Chakma"), + (language!("en"), "English"), + (language!("es"), "Spanish"), + (language!("fr"), "French"), + (language!("ja"), "Japanese"), + (language!("ru"), "Russian"), + (language!("sr"), "Serbian"), + (language!("th"), "Thai"), + (language!("tr"), "Turkish"), +]; + +#[no_mangle] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + icu_benchmark_macros::main_setup!(); + + let mut map = HashMap::new(); + // https://github.com/rust-lang/rust/issues/62633 was declined :( + for (lang, name) in DATA.iter() { + map.insert(lang, name).ok_or(()).unwrap_err(); + } + + assert_eq!(11, map.len()); + assert_eq!(Some(&&"Thai"), map.get(&language!("th"))); + assert_eq!(None, map.get(&language!("de"))); + + 0 +} diff --git a/third_party/rust/litemap/examples/language_names_lite_map.rs b/third_party/rust/litemap/examples/language_names_lite_map.rs new file mode 100644 index 0000000000..a8641bf82d --- /dev/null +++ b/third_party/rust/litemap/examples/language_names_lite_map.rs @@ -0,0 +1,46 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +// LiteMap is intended as a small and low-memory drop-in replacement for HashMap. +// +// The reader may compare this LiteMap example with the HashMap example to see analogous +// operations between LiteMap and HashMap. + +#![no_main] // https://github.com/unicode-org/icu4x/issues/395 + +icu_benchmark_macros::static_setup!(); + +use icu_locid::subtags::{language, Language}; +use litemap::LiteMap; + +const DATA: [(Language, &str); 11] = [ + (language!("ar"), "Arabic"), + (language!("bn"), "Bangla"), + (language!("ccp"), "Chakma"), + (language!("en"), "English"), + (language!("es"), "Spanish"), + (language!("fr"), "French"), + (language!("ja"), "Japanese"), + (language!("ru"), "Russian"), + (language!("sr"), "Serbian"), + (language!("th"), "Thai"), + (language!("tr"), "Turkish"), +]; + +#[no_mangle] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + icu_benchmark_macros::main_setup!(); + + let mut map = LiteMap::new_vec(); + // https://github.com/rust-lang/rust/issues/62633 was declined :( + for (lang, name) in DATA.iter() { + map.try_append(lang, name).ok_or(()).unwrap_err(); + } + + assert_eq!(11, map.len()); + assert_eq!(Some(&&"Thai"), map.get(&language!("th"))); + assert_eq!(None, map.get(&language!("de"))); + + 0 +} diff --git a/third_party/rust/litemap/examples/litemap_bincode.rs b/third_party/rust/litemap/examples/litemap_bincode.rs new file mode 100644 index 0000000000..bb17aaba01 --- /dev/null +++ b/third_party/rust/litemap/examples/litemap_bincode.rs @@ -0,0 +1,66 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +// LiteMap is intended as a small and low-memory drop-in replacement for +// HashMap. This example demonstrates how it works with Serde. + +#![no_main] // https://github.com/unicode-org/icu4x/issues/395 + +icu_benchmark_macros::static_setup!(); + +use litemap::LiteMap; + +const DATA: [(&str, &str); 11] = [ + ("ar", "Arabic"), + ("bn", "Bangla"), + ("ccp", "Chakma"), + ("en", "English"), + ("es", "Spanish"), + ("fr", "French"), + ("ja", "Japanese"), + ("ru", "Russian"), + ("sr", "Serbian"), + ("th", "Thai"), + ("tr", "Turkish"), +]; + +#[allow(dead_code)] +const BINCODE: [u8; 278] = [ + 11, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 97, 114, 6, 0, 0, 0, 0, 0, 0, 0, 65, 114, 97, + 98, 105, 99, 2, 0, 0, 0, 0, 0, 0, 0, 98, 110, 6, 0, 0, 0, 0, 0, 0, 0, 66, 97, 110, 103, 108, + 97, 3, 0, 0, 0, 0, 0, 0, 0, 99, 99, 112, 6, 0, 0, 0, 0, 0, 0, 0, 67, 104, 97, 107, 109, 97, 2, + 0, 0, 0, 0, 0, 0, 0, 101, 110, 7, 0, 0, 0, 0, 0, 0, 0, 69, 110, 103, 108, 105, 115, 104, 2, 0, + 0, 0, 0, 0, 0, 0, 101, 115, 7, 0, 0, 0, 0, 0, 0, 0, 83, 112, 97, 110, 105, 115, 104, 2, 0, 0, + 0, 0, 0, 0, 0, 102, 114, 6, 0, 0, 0, 0, 0, 0, 0, 70, 114, 101, 110, 99, 104, 2, 0, 0, 0, 0, 0, + 0, 0, 106, 97, 8, 0, 0, 0, 0, 0, 0, 0, 74, 97, 112, 97, 110, 101, 115, 101, 2, 0, 0, 0, 0, 0, + 0, 0, 114, 117, 7, 0, 0, 0, 0, 0, 0, 0, 82, 117, 115, 115, 105, 97, 110, 2, 0, 0, 0, 0, 0, 0, + 0, 115, 114, 7, 0, 0, 0, 0, 0, 0, 0, 83, 101, 114, 98, 105, 97, 110, 2, 0, 0, 0, 0, 0, 0, 0, + 116, 104, 4, 0, 0, 0, 0, 0, 0, 0, 84, 104, 97, 105, 2, 0, 0, 0, 0, 0, 0, 0, 116, 114, 7, 0, 0, + 0, 0, 0, 0, 0, 84, 117, 114, 107, 105, 115, 104, +]; + +/// Run this function to print new data to the console. +#[allow(dead_code)] +fn generate() { + let mut map = LiteMap::new_vec(); + for (lang, name) in DATA.iter() { + map.try_append(lang, name).ok_or(()).unwrap_err(); + } + + let buf = bincode::serialize(&map).unwrap(); + println!("{buf:?}"); +} + +#[no_mangle] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + icu_benchmark_macros::main_setup!(); + + // Uncomment the following line to re-generate the binary data. + // generate(); + + let map: LiteMap<&str, &str> = bincode::deserialize(&BINCODE).unwrap(); + assert_eq!(map.get("tr"), Some(&"Turkish")); + + 0 +} diff --git a/third_party/rust/litemap/examples/litemap_postcard.rs b/third_party/rust/litemap/examples/litemap_postcard.rs new file mode 100644 index 0000000000..d7033c21c5 --- /dev/null +++ b/third_party/rust/litemap/examples/litemap_postcard.rs @@ -0,0 +1,60 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +// LiteMap is intended as a small and low-memory drop-in replacement for +// HashMap. This example demonstrates how it works with Serde. + +#![no_main] // https://github.com/unicode-org/icu4x/issues/395 + +icu_benchmark_macros::static_setup!(); + +use litemap::LiteMap; + +const DATA: [(&str, &str); 11] = [ + ("ar", "Arabic"), + ("bn", "Bangla"), + ("ccp", "Chakma"), + ("en", "English"), + ("es", "Spanish"), + ("fr", "French"), + ("ja", "Japanese"), + ("ru", "Russian"), + ("sr", "Serbian"), + ("th", "Thai"), + ("tr", "Turkish"), +]; + +const POSTCARD: [u8; 117] = [ + 11, 2, 97, 114, 6, 65, 114, 97, 98, 105, 99, 2, 98, 110, 6, 66, 97, 110, 103, 108, 97, 3, 99, + 99, 112, 6, 67, 104, 97, 107, 109, 97, 2, 101, 110, 7, 69, 110, 103, 108, 105, 115, 104, 2, + 101, 115, 7, 83, 112, 97, 110, 105, 115, 104, 2, 102, 114, 6, 70, 114, 101, 110, 99, 104, 2, + 106, 97, 8, 74, 97, 112, 97, 110, 101, 115, 101, 2, 114, 117, 7, 82, 117, 115, 115, 105, 97, + 110, 2, 115, 114, 7, 83, 101, 114, 98, 105, 97, 110, 2, 116, 104, 4, 84, 104, 97, 105, 2, 116, + 114, 7, 84, 117, 114, 107, 105, 115, 104, +]; + +/// Run this function to print new data to the console. +#[allow(dead_code)] +fn generate() { + let mut map = LiteMap::new_vec(); + for (lang, name) in DATA.iter() { + map.try_append(lang, name).ok_or(()).unwrap_err(); + } + + let buf = postcard::to_stdvec(&map).unwrap(); + println!("{buf:?}"); +} + +#[no_mangle] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + icu_benchmark_macros::main_setup!(); + + // Uncomment the following line to re-generate the binary data. + // generate(); + + let map: LiteMap<&str, &str> = postcard::from_bytes(&POSTCARD).unwrap(); + assert_eq!(map.get("tr"), Some(&"Turkish")); + + 0 +} |