Struct DoubleArray

Source
pub struct DoubleArray<T: Serialize + DeserializeOwned + Debug> { /* private fields */ }
Expand description

ダブル配列の実装。

§Examples

use std::fmt::Debug;
use dary::DoubleArray;
use dary::Trie;
use serde_derive::{Serialize, Deserialize};
 
fn main() {
  let key1 = String::from("foo");
  let key2 = String::from("bar");
  let key3 = String::from("baz");
 
  let sample1 = Sample { surface: key1.clone(), cost: 1 };
  let sample2 = Sample { surface: key1.clone(), cost: 2 };
  let sample3 = Sample { surface: key2.clone(), cost: 1 };
  let sample4 = Sample { surface: key3.clone(), cost: 1 };
 
  let mut trie: Trie<Sample> = Trie::new();
  trie.set(&key1, sample1.clone());
  trie.set(&key1, sample2.clone());
  trie.set(&key2, sample3.clone());
  trie.set(&key3, sample4.clone());
 
  let double_array = trie.to_double_array().ok().unwrap();
  assert_eq!(vec![sample1, sample2], double_array.get(&key1).unwrap());
  assert_eq!(vec![sample3]         , double_array.get(&key2).unwrap());
  assert_eq!(vec![sample4]         , double_array.get(&key3).unwrap());
}
 
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct Sample {
    surface: String,
    cost: usize,
}

Implementations§

Source§

impl<T: Serialize + DeserializeOwned + Debug> DoubleArray<T>

Source

pub fn from_arrays( base_arr: &[u32], check_arr: &[u32], data_bytes: &[u8], ) -> Result<Self, Error>

base配列, check配列, data配列からDoubleArrayインスタンスを生成する。

§Arguments
  • base_arr - base配列
  • check_arr - check配列
  • data_bytes - data配列
Source

pub fn from_slice(bytes: &[u8]) -> Result<Self, Error>

u8の配列からDoubleArrayインスタンスを生成する。

§Arguments
  • bytes - base配列, check配列, data配列を u8 の配列として連結させた配列
Source

pub fn from_file(dictionary_path: &str) -> Result<Self, Error>

ファイルからDoubleArrayインスタンスを生成する。

§Arguments
  • dictionary_path - 辞書ファイルパス
Source

pub fn dump(self, output_path: &str) -> Result<Self, Error>

DoubleArrayをファイルにダンプする

§Arguments
  • output_path - 辞書ファイルパス
Examples found in repository?
examples/benchmarks.rs (line 76)
58fn sub_1(keys: &[String]) {
59	// Trie木構築
60	let start = Instant::now();
61	let mut trie: Trie<MorphemeData> = Trie::new();
62	for (i, key) in keys.iter().enumerate() {
63		trie.set(&key, MorphemeData::new(key, i));
64	}
65	println!("build trie: {} sec", get_duration(start));
66
67	// DoubleArray構築
68	let start = Instant::now();
69	let double_array: DoubleArray<MorphemeData> = trie.to_double_array().unwrap();
70	println!("build double array: {} sec", get_duration(start));
71
72	// DoubleArrayダンプ
73	let start = Instant::now();
74	let mut path: PathBuf = env::current_dir().unwrap();
75	path.push("benchmarks_sub_1.dic");
76	let double_array = double_array.dump(path.to_str().unwrap()).unwrap();
77	println!("dump double array: {} sec", get_duration(start));
78
79	// 検索
80	let start = Instant::now();
81	for (i, key) in keys.iter().enumerate() {
82		assert!(double_array.get(&key).unwrap().contains(&MorphemeData::new(key, i)));
83	}
84	println!("get all data: {} sec", get_duration(start));
85
86	fs::remove_file(path).unwrap();
87}
88
89fn sub_2(keys: &[String]) {
90	// Trie木構築
91	let start = Instant::now();
92	let mut trie: Trie<u32> = Trie::new();
93	for (i, key) in keys.iter().enumerate() {
94		trie.set(&key, i as u32);
95	}
96	println!("build trie: {} sec", get_duration(start));
97
98	// DoubleArray構築
99	let start = Instant::now();
100	let double_array: DoubleArray<u32> = trie.to_double_array().unwrap();
101	println!("build double array: {} sec", get_duration(start));
102
103	// DoubleArrayダンプ
104	let start = Instant::now();
105	let mut path: PathBuf = env::current_dir().unwrap();
106	path.push("benchmarks_sub_2.dic");
107	let double_array = double_array.dump(path.to_str().unwrap()).unwrap();
108	println!("dump double array: {} sec", get_duration(start));
109
110	// 検索
111	let start = Instant::now();
112	for (i, key) in keys.iter().enumerate() {
113		assert!(double_array.get(&key).unwrap().contains(&(i as u32)));
114	}
115	println!("get all data: {} sec", get_duration(start));
116
117	fs::remove_file(path).unwrap();
118}
Source

pub fn get(&self, key: &str) -> Option<Vec<T>>

ダブル配列から指定されたkeyを探索する関数 途中で遷移できなくなった場合、data_arrに値が存在しない場合はNoneを返す 遷移ができて、data_arrに値が存在する場合はdata_arrのスライスを返す デバッグ用

§Arguments
  • key - 探索対象の文字列
Examples found in repository?
examples/benchmarks.rs (line 82)
58fn sub_1(keys: &[String]) {
59	// Trie木構築
60	let start = Instant::now();
61	let mut trie: Trie<MorphemeData> = Trie::new();
62	for (i, key) in keys.iter().enumerate() {
63		trie.set(&key, MorphemeData::new(key, i));
64	}
65	println!("build trie: {} sec", get_duration(start));
66
67	// DoubleArray構築
68	let start = Instant::now();
69	let double_array: DoubleArray<MorphemeData> = trie.to_double_array().unwrap();
70	println!("build double array: {} sec", get_duration(start));
71
72	// DoubleArrayダンプ
73	let start = Instant::now();
74	let mut path: PathBuf = env::current_dir().unwrap();
75	path.push("benchmarks_sub_1.dic");
76	let double_array = double_array.dump(path.to_str().unwrap()).unwrap();
77	println!("dump double array: {} sec", get_duration(start));
78
79	// 検索
80	let start = Instant::now();
81	for (i, key) in keys.iter().enumerate() {
82		assert!(double_array.get(&key).unwrap().contains(&MorphemeData::new(key, i)));
83	}
84	println!("get all data: {} sec", get_duration(start));
85
86	fs::remove_file(path).unwrap();
87}
88
89fn sub_2(keys: &[String]) {
90	// Trie木構築
91	let start = Instant::now();
92	let mut trie: Trie<u32> = Trie::new();
93	for (i, key) in keys.iter().enumerate() {
94		trie.set(&key, i as u32);
95	}
96	println!("build trie: {} sec", get_duration(start));
97
98	// DoubleArray構築
99	let start = Instant::now();
100	let double_array: DoubleArray<u32> = trie.to_double_array().unwrap();
101	println!("build double array: {} sec", get_duration(start));
102
103	// DoubleArrayダンプ
104	let start = Instant::now();
105	let mut path: PathBuf = env::current_dir().unwrap();
106	path.push("benchmarks_sub_2.dic");
107	let double_array = double_array.dump(path.to_str().unwrap()).unwrap();
108	println!("dump double array: {} sec", get_duration(start));
109
110	// 検索
111	let start = Instant::now();
112	for (i, key) in keys.iter().enumerate() {
113		assert!(double_array.get(&key).unwrap().contains(&(i as u32)));
114	}
115	println!("get all data: {} sec", get_duration(start));
116
117	fs::remove_file(path).unwrap();
118}

ダブル配列で共通接頭辞検索を行う デバッグ用

§Arguments
  • key - 探索対象の文字列
Source

pub fn prefix_search_iter<'a>(&'a self, key: &'a str) -> PrefixSearchIter<'a, T>

Trait Implementations§

Source§

impl<T: Debug + Serialize + DeserializeOwned + Debug> Debug for DoubleArray<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for DoubleArray<T>

§

impl<T> RefUnwindSafe for DoubleArray<T>
where T: RefUnwindSafe,

§

impl<T> Send for DoubleArray<T>
where T: Send,

§

impl<T> Sync for DoubleArray<T>
where T: Sync,

§

impl<T> Unpin for DoubleArray<T>
where T: Unpin,

§

impl<T> UnwindSafe for DoubleArray<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.