/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4sto.ctr */ /* Copyright (C) 2011-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef DK4STO_H_INCLUDED /** Avoid multiple inclusions. */ #define DK4STO_H_INCLUDED 1 #line 10 "dk4sto.ctr" /** @file Sorted and unsorted data storage in AVL trees or double linked lists. CRT on Windows: Optional. */ #ifndef DK4CONF_H_INCLUDED #include "dk4conf.h" #endif #ifndef DK4TYPES_H_INCLUDED #include "dk4types.h" #endif #ifndef DK4ERROR_H_INCLUDED #include "dk4error.h" #endif /** Evaluation result for an object. */ typedef union { double d; /**< Evaluation to double. */ float f; /**< Evaluation to float. */ long l; /**< Evaluation to long. */ unsigned long ul; /**< Evaluation to unsigned long. */ int i; /**< Evaluation to int. */ unsigned int ui; /**< Evaluation to unsigned int. */ short s; /**< Evaluation to short. */ unsigned short us; /**< Evaluation to unsigned short. */ char c; /**< Evaluation to char. */ unsigned char uc; /**< Evaluation to unsigned char. */ } dk4_object_eval_res_t; /** Internal node for dk4_sto_t. This structure is used internally by the dksto module. */ struct dk4__sto__node { dk4_object_eval_res_t v; /**< Object evaluation of @a o. */ struct dk4__sto__node *p; /**< Parent node in tree. */ struct dk4__sto__node *l; /**< Left child (tree) or neighbour (list). */ struct dk4__sto__node *r; /**< Right child (tree) or neighbour (list). */ void *o; /**< Object to store. */ short b; /**< Balance flag (used in tree only). */ short w; /**< Direction to walk. */ }; /** Internal node for dk4_sto_t. */ typedef struct dk4__sto__node dk4_sto_node_t; /** Pointer to internal node for dk4_sto_t. */ typedef dk4_sto_node_t *dk4_sto_node_p; /** Evaluation functions. These function type are used to evaluate and compare objects stored in a sorted dk4_sto_t. */ /** Object evaluation function for sorted storage. */ typedef char dk4_fct_eval_c_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef unsigned char dk4_fct_eval_uc_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef short dk4_fct_eval_s_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef unsigned short dk4_fct_eval_us_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef int dk4_fct_eval_i_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef unsigned int dk4_fct_eval_ui_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef long dk4_fct_eval_l_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef unsigned long dk4_fct_eval_ul_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef float dk4_fct_eval_f_t(void const *obj, int crit); /** Object evaluation function for sorted storage. */ typedef double dk4_fct_eval_d_t(void const *obj, int crit); /** Object comparison function for sorted storage. When comparing to objects while inserting a new object into a storage both @a o1 and @a o2 are object pointers. When this function is called from dk4sto_it_find_like() the @a o1 argument is an object pointer from the storage, @a o2 is the pointer passed to dk4sto_it_find_like(). */ typedef int dk4_fct_comp_t(void const *o1, void const *o2, int crit); /** Choice for object evaluation function. */ typedef union { dk4_fct_eval_c_t *c; /**< Evaluate to char. */ dk4_fct_eval_uc_t *uc; /**< Evaluate to unsigned char. */ dk4_fct_eval_s_t *s; /**< Evaluate to short. */ dk4_fct_eval_us_t *us; /**< Evaluate to unsigned short. */ dk4_fct_eval_i_t *i; /**< Evaluate to int. */ dk4_fct_eval_ui_t *ui; /**< Evaluate to unsigned int. */ dk4_fct_eval_l_t *l; /**< Evaluate to long. */ dk4_fct_eval_ul_t*ul; /**< Evaluate to unsigned long. */ dk4_fct_eval_f_t *f; /**< Evaluate to float. */ dk4_fct_eval_d_t *d; /**< Evaluate to double. */ dk4_fct_comp_t *comp; /**< Compare two objects. */ } dk4_object_eval_fct_t; /** Object storage. A storage can be used to store pointers to objects. */ typedef struct dk4__sto__t { dk4_object_eval_fct_t e; /**< Comparison or evaluation function. */ dk4_sto_node_p *d; /**< Critical path for delete operations. */ dk4_sto_node_t *r; /**< Tree root or start of list. */ void *i; /**< Double-linked list of iterators. */ int h; /**< Selection for comparison or evaluation. */ int c; /**< Comparison or evaluation criteria. */ int t; /**< Flag: Use tree, 1=tree, 0=list. */ short l; /**< Path length of critical path. */ } dk4_sto_t; /** Storage iterator. This structure can be used to iterate through a dk4_sto_t. */ struct dk4__sto__iterator { struct dk4__sto__iterator *l; /**< Left neighbour (preceeding iterator). */ struct dk4__sto__iterator *r; /**< Right neighbour (following iterator). */ dk4_sto_t *s; /**< Owner storage. */ dk4_sto_node_t *c; /**< Current node, current position. */ }; /** Storage iterator. Can be used to iterate through a dk4_sto_t. */ typedef struct dk4__sto__iterator dk4_sto_it_t; #ifdef __cplusplus extern "C" { #endif /** Create new storage. @param erp Error report, may be NULL. @return Pointer to new storage on succcess, NULL on error. Use dk4sto_close() to destroy the storage when done with it. Error codes: - DK4_E_INVALID_ARGUMENTS
if elsize or nelem is 0, - DK4_E_MATH_OVERFLOW
on numeric overflow when calculating the product of elsize and nelem, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ dk4_sto_t * dk4sto_open(dk4_er_t *erp); /** Destroy storage, release memory. @param st Storage to destroy. */ void dk4sto_close(dk4_sto_t *st); /** Remove all pointers from a storage. @param st Storage. */ void dk4sto_remove_all(dk4_sto_t *st); /** Remove one pointer from storage. @param st Storage. @param o Object pointer to remove. @param erp Error report, may be NULL. @return 1 on success, 0 on error (not found). Error codes: - DK4_E_INVALID_ARGUMENTS
if st or o is NULL, - DK4_E_NOT_FOUND if o was not found in st. */ int dk4sto_remove(dk4_sto_t *st, void *o, dk4_er_t *erp); /** Add object pointer to storage. @param st Storage. @param o Object pointer to add. @param erp Error report, may be NULL. @return 1 on success, 0 on error (not enough memory). Error codes: - DK4_E_INVALID_ARGUMENTS
if st or os is NULL, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ int dk4sto_add(dk4_sto_t *st, void *o, dk4_er_t *erp); /** Create iterator for storage. @param st Storage. @param erp Error report, may be NULL. @return Pointer to new iterator on success, NULL on error. Use dk4sto_it_close() to destroy the iterator when done with it. When closing/destroying a storage all iterators for that storage are destroyed automatically. Error codes: - DK4_E_INVALID_ARGUMENTS
if st is NULL, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ dk4_sto_it_t * dk4sto_it_open(dk4_sto_t *st, dk4_er_t *erp); /** Destroy iterator. @param it Iterator to destroy. */ void dk4sto_it_close(dk4_sto_it_t *it); /** Reset iterator (next call to dk4sto_it_next() will return the first object pointer. @param it Iterator. */ void dk4sto_it_reset(dk4_sto_it_t *it); /** Return next object pointer. @param it Iterator. @return Pointer to next object on success, NULL on error (no more pointers available). */ void * dk4sto_it_next(dk4_sto_it_t *it); /** Find a pointer exactly. @param i Iterator. @param o Object pointer. @return Object pointer on success, NULL on error (object not found). The next calls to dk4sto_it_next() will return pointers to objects after the specified object \a o. */ void * dk4sto_it_find_exact(dk4_sto_it_t *i, void const *o); /** Find object pointer for object evaluating equally to \a o. @param i Iterator. @param o Object pointer. @param cr Comparison criteria. @return Object pointer on success, NULL on error (no such object). */ void * dk4sto_it_find_like(dk4_sto_it_t *i, void const *o, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to a char. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_c(dk4_sto_t *st, dk4_fct_eval_c_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to an unsigned char. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_uc(dk4_sto_t *st, dk4_fct_eval_uc_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to a short. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_s(dk4_sto_t *st, dk4_fct_eval_s_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to an unsigned short. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_us(dk4_sto_t *st, dk4_fct_eval_us_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to an int. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_i(dk4_sto_t *st, dk4_fct_eval_i_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to an unsigned int. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_ui(dk4_sto_t *st, dk4_fct_eval_ui_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to a long. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_l(dk4_sto_t *st, dk4_fct_eval_l_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to an unsigned long. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_ul(dk4_sto_t *st, dk4_fct_eval_ul_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to a float. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_f(dk4_sto_t *st, dk4_fct_eval_f_t *f, int cr); /** Set evaluation function. @param st Storage. @param f Function evaluating a pointer to a double. @param cr Evaluation criteria. @return 1 on success, 0 on error. */ int dk4sto_set_eval_d(dk4_sto_t *st, dk4_fct_eval_d_t *f, int cr); /** Set comparison function. @param st Storage. @param f Function comparing two object pointers. @param cr Comparison criteria. @return 1 on success, 0 on error. */ int dk4sto_set_comp(dk4_sto_t *st, dk4_fct_comp_t *f, int cr); /** Allow use of tree structures. This function must be called before adding any data. @param st Storage. @param ok Flag: Trees may be used. @return 1 on success, 0 on error. */ int dk4sto_use_trees(dk4_sto_t *st,int ok); /** Find object pointer at the trees root. @param s Storage. */ void * dk4sto_find_root(dk4_sto_t *s); /** Find object pointer for the parent node of the last found object. @param i Iterator. @return Object pointer on success, NULL on error. */ void * dk4sto_it_find_parent(dk4_sto_it_t *i); /** Find object pointer for node on left child of the last found object. @param i Iterator. @return Object pointer on success, NULL on error. */ void * dk4sto_it_find_left(dk4_sto_it_t *i); /** Find object pointer for node on right child of the last found object. @param i Iterator. @return Object pointer on success, NULL on error. */ void * dk4sto_it_find_right(dk4_sto_it_t *i); /** Find object pointer at the trees root. @param i Iterator. */ void * dk4sto_it_find_root(dk4_sto_it_t *i); #ifdef __cplusplus } #endif #endif