star-line

Structure for accelerating line importance sampling
git clone git://git.meso-star.fr/star-line.git
Log | Files | Refs | README | LICENSE

commit 6c156c13f19227540059a4d003344cb7baf2509c
parent 797b216949b66854a8d1ddfb8483af15c9d7a6ae
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 27 Apr 2022 11:17:41 +0200

Add API functions to access data from tree nodes

Diffstat:
Msrc/sln.h | 29++++++++++++++++++++++++++++-
Msrc/sln_tree.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/sln_tree_build.c | 8++++++--
Msrc/sln_tree_c.h | 8++++----
4 files changed, 87 insertions(+), 7 deletions(-)

diff --git a/src/sln.h b/src/sln.h @@ -46,8 +46,9 @@ /* Forwar declaration of external data structures */ struct logger; struct mem_allocator; -struct shtr_lines_list; +struct shtr_line; struct shtr_isotope_metadata; +struct shtr_lines_list; enum sln_line_profile { SLN_LINE_PROFILE_VOIGT, @@ -113,6 +114,7 @@ static const struct sln_tree_create_args SLN_TREE_CREATE_ARGS_DEFAULT = /* Forward declarations of opaque data structures */ struct sln_device; struct sln_tree; +struct sln_node; /******************************************************************************* * Device API @@ -147,4 +149,29 @@ SLN_API res_T sln_tree_ref_put (struct sln_tree* tree); +SLN_API const struct sln_node* +sln_tree_get_root + (const struct sln_tree* tree); + +SLN_API int +sln_node_is_leaf + (const struct sln_node* node); + +/* Return NULL if the nade is a leaf */ +SLN_API const struct sln_node* +sln_node_get_child + (const struct sln_node* node, + const unsigned ichild); /* 0 or 1 */ + +SLN_API size_t +sln_node_get_lines_count + (const struct sln_node* node); + +SLN_API res_T +sln_leaf_get_line + (const struct sln_tree* tree, + const struct sln_node* leaf, + const size_t iline, + const struct shtr_line** line); + #endif /* SLN_H */ diff --git a/src/sln_tree.c b/src/sln_tree.c @@ -312,3 +312,52 @@ sln_tree_ref_put(struct sln_tree* tree) ref_put(&tree->ref, release_tree); return RES_OK; } + +const struct sln_node* +sln_tree_get_root(const struct sln_tree* tree) +{ + ASSERT(tree); + if(darray_node_size_get(&tree->nodes)) { + return darray_node_cdata_get(&tree->nodes); + } else { + return NULL; + } +} + +int +sln_node_is_leaf(const struct sln_node* node) +{ + ASSERT(node); + return node->offset <= 0; +} + +const struct sln_node* +sln_node_get_child(const struct sln_node* node, const unsigned ichild) +{ + ASSERT(node && ichild <= 1); + return node + node->offset + ichild; +} + +size_t +sln_node_get_lines_count(const struct sln_node* node) +{ + ASSERT(node); + return node->range[1] - node->range[0] + 1;/*Both boundaries are inclusives*/ +} + +res_T +sln_leaf_get_line + (const struct sln_tree* tree, + const struct sln_node* leaf, + const size_t iline, + const struct shtr_line** line) +{ + if(!tree + || !leaf + || iline > sln_node_get_lines_count(leaf) + || !sln_node_is_leaf(leaf)) + return RES_BAD_ARG; + + return shtr_lines_view_get_line + (tree->lines_view, leaf->range[0] + iline, line); +} diff --git a/src/sln_tree_build.c b/src/sln_tree_build.c @@ -20,6 +20,8 @@ #include "sln_tree_c.h" #include <star/shtr.h> +#define STACK_SIZE 64 + /******************************************************************************* * Local functions ******************************************************************************/ @@ -28,7 +30,7 @@ tree_build (struct sln_tree* tree, const struct sln_tree_create_args* args) { - size_t stack[64]; + size_t stack[STACK_SIZE]; size_t istack = 0; size_t inode; size_t nlines; @@ -39,7 +41,7 @@ tree_build #define NODE(Id) (darray_node_data_get(&tree->nodes) + (Id)) #define CREATE_NODE { \ - res = darray_node_push_back(&tree->nodes, &NODE_NULL); \ + res = darray_node_push_back(&tree->nodes, &SLN_NODE_NULL); \ if(res != RES_OK) goto error; \ } (void)0 @@ -86,6 +88,8 @@ tree_build NODE(ichildren+1)->range[1] = NODE(inode)->range[1]; inode = ichildren + 0; /* Make the left child current node */ + + ASSERT(istack < STACK_SIZE); stack[istack++] = ichildren + 1; /* Push the right child */ } } diff --git a/src/sln_tree_c.h b/src/sln_tree_c.h @@ -26,17 +26,17 @@ struct sln_device; struct sln_tree_create_args; struct shtr_lines_view; -struct node { +struct sln_node { /* Range of the line indices corresponding to the node. Both the lower and * upper indices are included into the node */ uint64_t range[2]; int64_t offset; /* Offset toward the node's children (left then right) */ }; -#define NODE_NULL__ {{0,0},0} -static const struct node NODE_NULL = NODE_NULL__; +#define SLN_NODE_NULL__ {{0,0},0} +static const struct sln_node SLN_NODE_NULL = SLN_NODE_NULL__; /* Generate the dynamic array of nodes */ -#define DARRAY_DATA struct node +#define DARRAY_DATA struct sln_node #define DARRAY_NAME node #include <rsys/dynamic_array.h>