commit 631c84414d0fe0204349f5c6d8336cf047fbccb9
parent 350e81af171467d07413abd4d400c8a3304720ec
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 17 May 2022 08:53:40 +0200
Simplifies mapping between an idx and its molecular metadata
Replace the hash table by an array whose size is fixed to
SHTR_MAX_MOLECULES_COUNT.
Diffstat:
1 file changed, 11 insertions(+), 24 deletions(-)
diff --git a/src/shtr_isotope_metadata.c b/src/shtr_isotope_metadata.c
@@ -94,12 +94,6 @@ molecule_copy_and_release(struct molecule* dst, struct molecule* src)
#define DARRAY_FUNCTOR_COPY_AND_RELEASE molecule_copy_and_release
#include <rsys/dynamic_array.h>
-/* Generate the hash table that map a global identifier to its local index */
-#define HTABLE_NAME molid2idx
-#define HTABLE_KEY int /* Unique identifier */
-#define HTABLE_DATA size_t /* Index of the corresponding registered data */
-#include <rsys/hash_table.h>
-
struct shtr_isotope_metadata {
/* List of molecules and isotopes */
struct darray_molecule molecules;
@@ -107,7 +101,7 @@ struct shtr_isotope_metadata {
/* Map the global identifier of a molecule to its correspond local index into
* the dynamic aray into which it is registered */
- struct htable_molid2idx molid2idx;
+ int molid2idx[SHTR_MAX_MOLECULES_COUNT];
struct shtr* shtr;
ref_T ref;
@@ -137,7 +131,7 @@ create_isotope_metadata
metadata->shtr = shtr;
darray_molecule_init(shtr->allocator, &metadata->molecules);
darray_isotope_init(shtr->allocator, &metadata->isotopes);
- htable_molid2idx_init(shtr->allocator, &metadata->molid2idx);
+ memset(metadata->molid2idx, 0xFF, sizeof(metadata->molid2idx));
exit:
*out_isotopes = metadata;
@@ -169,6 +163,7 @@ flush_molecule
/* Fetch the index where the molecule is going to be store */
ientry = darray_molecule_size_get(&metadata->molecules);
+ CHK(ientry < SHTR_MAX_MOLECULES_COUNT);
/* Store the molecule */
res = darray_molecule_push_back(&metadata->molecules, molecule);
@@ -180,8 +175,7 @@ flush_molecule
}
/* Register the molecule */
- pimolecule = htable_molid2idx_find(&metadata->molid2idx, &molecule->id);
- if(pimolecule) {
+ if(metadata->molid2idx[molecule->id] >= 0) {
const struct molecule* molecule2 = NULL;
molecule2 = darray_molecule_cdata_get(&metadata->molecules) + *pimolecule;
log_err(metadata->shtr,
@@ -194,21 +188,15 @@ flush_molecule
res = RES_OK;
goto error;
}
- res = htable_molid2idx_set(&metadata->molid2idx, &molecule->id, &ientry);
- if(res != RES_OK) {
- log_err(metadata->shtr,
- "%s: error registering the %s molecule -- %s.\n",
- txtrdr_get_name(txtrdr), str_cget(&molecule->name), res_to_cstr(res));
- goto error;
- }
-
+ ASSERT((size_t)((int)ientry) == ientry);
+ metadata->molid2idx[molecule->id] = (int)ientry;
molecule_clear(molecule);
exit:
return res;
error:
if(ientry != SIZE_MAX) darray_molecule_resize(&metadata->molecules, ientry);
- htable_molid2idx_erase(&metadata->molid2idx, &molecule->id);
+ metadata->molid2idx[molecule->id] = -1;
goto exit;
}
@@ -478,7 +466,6 @@ release_isotope_metadata(ref_T* ref)
shtr = metadata->shtr;
darray_molecule_release(&metadata->molecules);
darray_isotope_release(&metadata->isotopes);
- htable_molid2idx_release(&metadata->molid2idx);
MEM_RM(shtr->allocator, metadata);
SHTR(ref_put(shtr));
}
@@ -607,7 +594,7 @@ shtr_isotope_metadata_find_molecule
const int molecule_id,
struct shtr_molecule* out_molecule)
{
- size_t* pimolecule = NULL;
+ int imolecule = 0;
res_T res = RES_OK;
if(!metadata || !out_molecule) {
@@ -615,12 +602,12 @@ shtr_isotope_metadata_find_molecule
goto error;
}
- pimolecule = htable_molid2idx_find(&metadata->molid2idx, &molecule_id);
- if(!pimolecule) {
+ imolecule = metadata->molid2idx[molecule_id];
+ if(imolecule < 0) {
*out_molecule = SHTR_MOLECULE_NULL;
} else {
res = shtr_isotope_metadata_get_molecule
- (metadata, *pimolecule, out_molecule);
+ (metadata, (size_t)imolecule, out_molecule);
if(res != RES_OK) goto error;
}