Skip to content

Commit 03b90d1

Browse files
committed
New type to make internal index more evident
1 parent b79856a commit 03b90d1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/base.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ use crate::errors::{Error, ErrorKind, TaxonomyResult};
77
use crate::rank::TaxRank;
88
use crate::taxonomy::Taxonomy;
99

10+
pub type InternalIndex = usize;
11+
1012
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
1113
pub struct GeneralTaxonomy {
1214
pub tax_ids: Vec<String>,
13-
pub parent_ids: Vec<usize>,
15+
pub parent_ids: Vec<InternalIndex>,
1416
pub parent_distances: Vec<f32>,
1517
pub names: Vec<String>,
1618
pub ranks: Vec<TaxRank>,
1719
// Only used by the JSON format
1820
pub data: Vec<HashMap<String, Value>>,
1921

2022
// these are lookup tables that dramatically speed up some operations
21-
pub(crate) tax_id_lookup: HashMap<String, usize>,
22-
pub(crate) children_lookup: Vec<Vec<usize>>,
23+
pub(crate) tax_id_lookup: HashMap<String, InternalIndex>,
24+
pub(crate) children_lookup: Vec<Vec<InternalIndex>>,
2325
}
2426

2527
impl Default for GeneralTaxonomy {
@@ -104,15 +106,15 @@ impl GeneralTaxonomy {
104106
}
105107

106108
#[inline]
107-
pub(crate) fn to_internal_index(&self, tax_id: &str) -> TaxonomyResult<usize> {
109+
pub fn to_internal_index(&self, tax_id: &str) -> TaxonomyResult<InternalIndex> {
108110
self.tax_id_lookup.get(tax_id).map_or_else(
109111
|| Err(Error::new(ErrorKind::NoSuchTaxId(tax_id.to_owned()))),
110112
|t| Ok(*t),
111113
)
112114
}
113115

114116
#[inline]
115-
pub fn from_internal_index(&self, tax_id: usize) -> TaxonomyResult<&str> {
117+
pub fn from_internal_index(&self, tax_id: InternalIndex) -> TaxonomyResult<&str> {
116118
if tax_id >= self.tax_ids.len() {
117119
return Err(Error::new(ErrorKind::NoSuchTaxId(format!(
118120
"Internal ID: {}",
@@ -124,7 +126,7 @@ impl GeneralTaxonomy {
124126

125127
pub fn from_arrays(
126128
tax_ids: Vec<String>,
127-
parent_ids: Vec<usize>,
129+
parent_ids: Vec<InternalIndex>,
128130
names: Option<Vec<String>>,
129131
ranks: Option<Vec<TaxRank>>,
130132
distances: Option<Vec<f32>>,

0 commit comments

Comments
 (0)