From 9abcd21e44f4d810c299da49a66ff86a30e96061 Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Sat, 23 Jun 2007 19:04:09 +0000 Subject: [PATCH] serious progress is cleaning up type safety of splay trees git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/personal/carm/dev-1.0@215 6778bc44-b910-0410-a7a0-be141de4315d --- src/projectM-engine/BuiltinFuncs.hpp | 2 +- src/projectM-engine/BuiltinParams.hpp | 2 +- src/projectM-engine/CMakeLists.txt | 2 +- src/projectM-engine/CustomShape.cpp | 2 +- src/projectM-engine/CustomShape.h | 14 +- src/projectM-engine/CustomWave.cpp | 2 +- src/projectM-engine/CustomWave.h | 20 +-- src/projectM-engine/Func.cpp | 2 +- src/projectM-engine/Func.h | 2 +- src/projectM-engine/InitCond.cpp | 14 +- src/projectM-engine/InitCond.h | 3 +- src/projectM-engine/Param.cpp | 2 +- src/projectM-engine/Param.h | 21 +-- src/projectM-engine/Parser.cpp | 2 +- src/projectM-engine/Parser.h | 2 +- src/projectM-engine/PerPixelEqn.cpp | 2 +- src/projectM-engine/PerPointEqn.cpp | 2 +- src/projectM-engine/Preset.hpp | 18 +-- src/projectM-engine/SplayNode.hpp | 35 ++--- src/projectM-engine/SplayTree.hpp | 179 +++++--------------------- src/projectM-engine/compare.h | 87 ++++++++++++- src/projectM-engine/menu.cpp | 2 +- src/projectM-engine/projectM.cpp | 2 +- src/projectM-engine/projectM.h | 2 +- 24 files changed, 171 insertions(+), 250 deletions(-) diff --git a/src/projectM-engine/BuiltinFuncs.hpp b/src/projectM-engine/BuiltinFuncs.hpp index e8309ff7e..cc46bb7b8 100644 --- a/src/projectM-engine/BuiltinFuncs.hpp +++ b/src/projectM-engine/BuiltinFuncs.hpp @@ -210,7 +210,7 @@ return (float)result; class BuiltinFuncs { public: - static SplayTree * builtin_func_tree; + static SplayTree * builtin_func_tree; static int init_builtin_func_db(); static int destroy_builtin_func_db(); static int load_all_builtin_func(); diff --git a/src/projectM-engine/BuiltinParams.hpp b/src/projectM-engine/BuiltinParams.hpp index 60dc3ca54..b19409cc3 100644 --- a/src/projectM-engine/BuiltinParams.hpp +++ b/src/projectM-engine/BuiltinParams.hpp @@ -66,6 +66,6 @@ public: private: static const bool BUILTIN_PARAMS_DEBUG = false; - SplayTree *builtin_param_tree; + SplayTree *builtin_param_tree; }; #endif diff --git a/src/projectM-engine/CMakeLists.txt b/src/projectM-engine/CMakeLists.txt index 7c823e528..849a3d37a 100644 --- a/src/projectM-engine/CMakeLists.txt +++ b/src/projectM-engine/CMakeLists.txt @@ -1,5 +1,5 @@ PROJECT(projectM) -ADD_LIBRARY(projectM SHARED projectM.cpp projectM.h pbuffer.cpp pbuffer.h InitCond.cpp InitCond.h console_interface.cpp Expr.cpp PCM.cpp Parser.cpp Preset.cpp common.h BeatDetect.cpp PCM.h PerPixelEqn.cpp Eval.h SplayTree.cpp Param.cpp CustomWave.cpp CustomShape.h CustomShape.cpp Param.h CustomWave.h BeatDetect.h menu.cpp console_interface.h Func.h Func.cpp Eval.cpp wipemalloc.h browser.cpp PerFrameEqn.cpp PerPointEqn.cpp editor.cpp fftsg.cpp glConsole.cpp CValue.h Expr.h timer.cpp wipemalloc.cpp PerFrameEqn.h PerPixelEqn.h PerPointEqn.h browser.h BuiltinFuncs.hpp BuiltinFuncs.cpp compare.h editor.h event.h fatal.h SplayTree.h fftsg.h glConsole.h menu.h timer.h SplayNode.cpp SplayNode.h BuiltinParams.hpp BuiltinParams.cpp Preset.hpp PresetSwitcher.hpp PresetSwitcher.cpp Renderer.cpp Renderer.hpp) +ADD_LIBRARY(projectM SHARED projectM.cpp projectM.h pbuffer.cpp pbuffer.h InitCond.cpp InitCond.h console_interface.cpp Expr.cpp PCM.cpp Parser.cpp Preset.cpp common.h BeatDetect.cpp PCM.h PerPixelEqn.cpp Eval.h Param.cpp CustomWave.cpp CustomShape.h CustomShape.cpp Param.h CustomWave.h BeatDetect.h menu.cpp console_interface.h Func.h Func.cpp Eval.cpp wipemalloc.h browser.cpp PerFrameEqn.cpp PerPointEqn.cpp editor.cpp fftsg.cpp glConsole.cpp CValue.h Expr.h timer.cpp wipemalloc.cpp PerFrameEqn.h PerPixelEqn.h PerPointEqn.h browser.h BuiltinFuncs.hpp BuiltinFuncs.cpp compare.h editor.h event.h fatal.h SplayTree.hpp fftsg.h glConsole.h menu.h timer.h SplayNode.hpp BuiltinParams.hpp BuiltinParams.cpp Preset.hpp PresetSwitcher.hpp PresetSwitcher.cpp Renderer.cpp Renderer.hpp) OPTION(USE_FTGL "Use FTGL for on-screen fonts" ON) diff --git a/src/projectM-engine/CustomShape.cpp b/src/projectM-engine/CustomShape.cpp index 79864dde0..84c18cc95 100755 --- a/src/projectM-engine/CustomShape.cpp +++ b/src/projectM-engine/CustomShape.cpp @@ -33,7 +33,7 @@ #include "Param.h" #include "PerFrameEqn.h" #include "Preset.hpp" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" diff --git a/src/projectM-engine/CustomShape.h b/src/projectM-engine/CustomShape.h index c507ea71d..78e5cb343 100755 --- a/src/projectM-engine/CustomShape.h +++ b/src/projectM-engine/CustomShape.h @@ -30,9 +30,13 @@ #define _CUSTOM_SHAPE_H #define CUSTOM_SHAPE_DEBUG 0 +#include "SplayTree.hpp" +#include "Param.h" +#include "PerFrameEqn.h" +#include "InitCond.h" class Preset; -class SplayTree; + class CustomShape { public: @@ -41,7 +45,7 @@ public: int per_frame_count; /* Parameter tree associated with this custom shape */ - SplayTree * param_tree; + SplayTree * param_tree; /* Engine variables */ int sides; @@ -84,9 +88,9 @@ public: float t8; /* Data structure to hold per frame / per frame init equations */ - SplayTree * init_cond_tree; - SplayTree * per_frame_eqn_tree; - SplayTree * per_frame_init_eqn_tree; + SplayTree * init_cond_tree; + SplayTree * per_frame_eqn_tree; + SplayTree * per_frame_init_eqn_tree; /* Denotes the index of the last character for each string buffer */ int per_frame_eqn_string_index; diff --git a/src/projectM-engine/CustomWave.cpp b/src/projectM-engine/CustomWave.cpp index c3df27c4b..bd8129775 100755 --- a/src/projectM-engine/CustomWave.cpp +++ b/src/projectM-engine/CustomWave.cpp @@ -36,7 +36,7 @@ #include "PerFrameEqn.h" #include "PerPointEqn.h" #include "Preset.hpp" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" diff --git a/src/projectM-engine/CustomWave.h b/src/projectM-engine/CustomWave.h index 039859ad3..a9ae47de6 100755 --- a/src/projectM-engine/CustomWave.h +++ b/src/projectM-engine/CustomWave.h @@ -47,7 +47,7 @@ class Preset; #include "common.h" #include "Param.h" -class SplayTree; +#include "SplayTree.hpp" class CustomWave { public: @@ -66,7 +66,7 @@ public: int per_frame_count; /* Parameter tree associated with this custom wave */ - SplayTree * param_tree; + SplayTree * param_tree; /* Engine variables */ float x; /* x position for per point equations */ @@ -109,10 +109,10 @@ public: float v1,v2; /* Data structure to hold per frame and per point equations */ - SplayTree * init_cond_tree; - SplayTree * per_frame_eqn_tree; - SplayTree * per_point_eqn_tree; - SplayTree * per_frame_init_eqn_tree; + SplayTree * init_cond_tree; + SplayTree * per_frame_eqn_tree; + SplayTree * per_point_eqn_tree; + SplayTree * per_frame_init_eqn_tree; /* Denotes the index of the last character for each string buffer */ int per_point_eqn_string_index; @@ -137,11 +137,11 @@ public: void load_unspec_init_cond() ; void eval_custom_wave_init_conds(); void load_unspec_init_cond(Param * param); - void destroy_per_point_eqn_tree(SplayTree * tree); - void destroy_param_db_tree(SplayTree * tree); - void destroy_per_frame_eqn_tree(SplayTree * tree); + void destroy_per_point_eqn_tree(SplayTree * tree); + void destroy_param_db_tree(SplayTree * tree); + void destroy_per_frame_eqn_tree(SplayTree * tree); void destroy_per_frame_init_eqn_tree(); - void destroy_init_cond_tree(SplayTree * tree); + void destroy_init_cond_tree(SplayTree * tree); void evalPerPointEqn(PerPointEqn * per_point_eqn); }; diff --git a/src/projectM-engine/Func.cpp b/src/projectM-engine/Func.cpp index a8f3b23d6..7f3a83f84 100755 --- a/src/projectM-engine/Func.cpp +++ b/src/projectM-engine/Func.cpp @@ -28,7 +28,7 @@ #include "fatal.h" #include "Func.h" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" diff --git a/src/projectM-engine/Func.h b/src/projectM-engine/Func.h index 4de82e89d..e49a49d62 100755 --- a/src/projectM-engine/Func.h +++ b/src/projectM-engine/Func.h @@ -32,7 +32,7 @@ #include "common.h" class Func; -class SplayTree; +#include "SplayTree.hpp" /* Function Type */ class Func { diff --git a/src/projectM-engine/InitCond.cpp b/src/projectM-engine/InitCond.cpp index 426ca2a7e..57133f3d1 100755 --- a/src/projectM-engine/InitCond.cpp +++ b/src/projectM-engine/InitCond.cpp @@ -30,7 +30,7 @@ #include "Expr.h" #include "InitCond.h" #include "Param.h" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" @@ -135,15 +135,3 @@ void InitCond::init_cond_to_string() { init_cond_string_buffer_index+= string_length + 1; } - -char *InitCond::create_init_cond_string_buffer( SplayTree * init_cond_tree ) { - - if (init_cond_tree == NULL) - return NULL; - - init_cond_string_buffer_index = 0; - - init_cond_tree->splay_traverse((void (*)(void*))init_cond_to_string_helper); - - return init_cond_string_buffer; -} diff --git a/src/projectM-engine/InitCond.h b/src/projectM-engine/InitCond.h index d1b157f56..e76b2aa7e 100755 --- a/src/projectM-engine/InitCond.h +++ b/src/projectM-engine/InitCond.h @@ -36,7 +36,7 @@ class InitCond; class Param; -class SplayTree; +#include "SplayTree.hpp" class InitCond { public: @@ -50,7 +50,6 @@ public: ~InitCond(); void eval_init_cond(); - static char * create_init_cond_string_buffer( SplayTree * init_cond_tree); void init_cond_to_string(); void write_init(); }; diff --git a/src/projectM-engine/Param.cpp b/src/projectM-engine/Param.cpp index fa99eb01e..1defda375 100755 --- a/src/projectM-engine/Param.cpp +++ b/src/projectM-engine/Param.cpp @@ -36,7 +36,7 @@ #include "InitCond.h" #include "Param.h" #include "Preset.hpp" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" diff --git a/src/projectM-engine/Param.h b/src/projectM-engine/Param.h index ef6cd7f19..97ecf89cb 100755 --- a/src/projectM-engine/Param.h +++ b/src/projectM-engine/Param.h @@ -55,7 +55,7 @@ class InitCond; class Param; class Preset; -class SplayTree; +#include "SplayTree.hpp" /* Parameter Type */ class Param { @@ -86,7 +86,7 @@ public: int remove_param(); static Param * find_param( char *name, Preset *preset, int flags ); - int insert_param( SplayTree *database ); + //int insert_param( SplayTree *database ); void load_init_cond(); void load_unspec_init_cond(); void load_unspec_init_cond_shape(); @@ -110,22 +110,5 @@ public: int upper_bound, int lower_bound, int init_val ); }; -/** Splaytree traversal helpers */ -inline void free_param_helper( void *param ) { - delete ((Param *)param); - } - -inline void load_init_cond_helper( void *param ) { - ((Param *)param)->load_init_cond(); - } - -inline void load_unspec_init_cond_wave_helper( void *param ) { - ((Param *)param)->load_unspec_init_cond(); -} - -inline void load_unspec_init_cond_shape_helper( void *param ) { - ((Param *)param)->load_unspec_init_cond_shape(); -} - #endif /** !_PARAM_TYPES_H */ diff --git a/src/projectM-engine/Parser.cpp b/src/projectM-engine/Parser.cpp index f0ca9c690..19f736a0f 100755 --- a/src/projectM-engine/Parser.cpp +++ b/src/projectM-engine/Parser.cpp @@ -39,7 +39,7 @@ #include "Parser.h" #include "PerFrameEqn.h" #include "PerPixelEqn.h" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" /* Grabs the next token from the file. The second argument points diff --git a/src/projectM-engine/Parser.h b/src/projectM-engine/Parser.h index 2e558ec18..c8aba4739 100755 --- a/src/projectM-engine/Parser.h +++ b/src/projectM-engine/Parser.h @@ -157,7 +157,7 @@ public: static int insert_infix_rec(InfixOp * infix_op, TreeExpr * root); static GenExpr * parse_gen_expr(FILE * fs, TreeExpr * tree_expr, Preset * preset); static PerFrameEqn * parse_implicit_per_frame_eqn(FILE * fs, char * param_string, int index, Preset * preset); - static InitCond * parse_per_frame_init_eqn(FILE * fs, Preset * preset, SplayTree * database); + static InitCond * parse_per_frame_init_eqn(FILE * fs, Preset * preset, SplayTree * database); static int parse_wavecode_prefix(char * token, int * id, char ** var_string); static int parse_wavecode(char * token, FILE * fs, Preset * preset); static int parse_wave_prefix(char * token, int * id, char ** eqn_string); diff --git a/src/projectM-engine/PerPixelEqn.cpp b/src/projectM-engine/PerPixelEqn.cpp index 6d3d4d113..db681f2b8 100755 --- a/src/projectM-engine/PerPixelEqn.cpp +++ b/src/projectM-engine/PerPixelEqn.cpp @@ -32,7 +32,7 @@ #include "Eval.h" #include "Param.h" #include "PerPixelEqn.h" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" diff --git a/src/projectM-engine/PerPointEqn.cpp b/src/projectM-engine/PerPointEqn.cpp index 7ef7cf433..b51d2dab6 100755 --- a/src/projectM-engine/PerPointEqn.cpp +++ b/src/projectM-engine/PerPointEqn.cpp @@ -34,7 +34,7 @@ #include "Param.h" #include "PerPixelEqn.h" #include "PerPointEqn.h" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "wipemalloc.h" diff --git a/src/projectM-engine/Preset.hpp b/src/projectM-engine/Preset.hpp index 4ed9de57d..b9024238d 100644 --- a/src/projectM-engine/Preset.hpp +++ b/src/projectM-engine/Preset.hpp @@ -42,14 +42,14 @@ #include "PerFrameEqn.h" #include "BuiltinParams.hpp" #include "PresetFrameIO.hpp" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "InitCond.h" #include #include "StaticArray.hpp" class CustomWave; class InitCond; -//class SplayTree; +//#include "SplayTree.hpp" class Preset { @@ -92,14 +92,14 @@ public: char per_frame_init_eqn_string_buffer[STRING_BUFFER_SIZE]; /* Data structures that contain equation and initial condition information */ - SplayTree * per_frame_eqn_tree; /* per frame equations */ - SplayTree * per_pixel_eqn_tree; /* per pixel equation tree */ + SplayTree * per_frame_eqn_tree; /* per frame equations */ + SplayTree * per_pixel_eqn_tree; /* per pixel equation tree */ GenExpr * per_pixel_eqn_array[NUM_OPS]; /* per pixel equation array */ - SplayTree * per_frame_init_eqn_tree; /* per frame initial equations */ - SplayTree * init_cond_tree; /* initial conditions */ - SplayTree * user_param_tree; /* user parameter splay tree */ - SplayTree * custom_wave_tree; /* custom wave forms for this preset */ - SplayTree * custom_shape_tree; /* custom shapes for this preset */ + SplayTree * per_frame_init_eqn_tree; /* per frame initial equations */ + SplayTree * init_cond_tree; /* initial conditions */ + SplayTree * user_param_tree; /* user parameter splay tree */ + SplayTree * custom_wave_tree; /* custom wave forms for this preset */ + SplayTree * custom_shape_tree; /* custom shapes for this preset */ int add_per_pixel_eqn( char *name, GenExpr *gen_expr ); int isPerPixelEqn( int op ); diff --git a/src/projectM-engine/SplayNode.hpp b/src/projectM-engine/SplayNode.hpp index b23894730..02e613225 100644 --- a/src/projectM-engine/SplayNode.hpp +++ b/src/projectM-engine/SplayNode.hpp @@ -26,10 +26,10 @@ * $Log$ */ -#ifndef _SPLAYNODE_H -#define _SPLAYNODE_H +#ifndef _SPLAYNODE_HPP +#define _SPLAYNODE_HPP -#include "projectM.h" +//#include "projectM.h" #include "compare.h" @@ -38,13 +38,13 @@ template class SplayNode { public: int type; - SplayTree *tree; + SplayNode *left, *right; Data *data; - void *key; - + void *key; + void (*free_key)(void*); SplayNode(); - SplayNode( int type, void *key, void *data, SplayTree *tree ); + SplayNode( int type, void *key, Data *data, void (*free_key)(void*)); ~SplayNode(); }; @@ -54,34 +54,24 @@ SplayNode::SplayNode() { this->data = NULL; this->type = -1; this->key = NULL; - this->tree = NULL; + this->free_key = free_key; } /* Create a new splay node type */ template -SplayNode::SplayNode(int type, void * key, Data * data, SplayTree *tree) { +SplayNode::SplayNode(int type, void * key, Data * data, void (*free_key)(void*)) { /* Creates the new splay node struct */ this->data = data; this->type = type; this->key = key; - this->tree = tree; + this->free_key = free_key; } /* Recursively free all the splaynodes */ template SplayNode::~SplayNode() { - if ( tree == NULL ) { - if ( key != NULL || data != NULL ) { - /** This shouldn't happen */ - printf( "~SplayNode: tree is NULL with non-NULL key/data!\n" ); - printf( "\tleft: %X\tright: %X\tdata: %X\tkey: %X\n", - left, right, data, key ); - } - return; - } - /* Ok if this happens, a recursive base case */ /* Free left node */ if ( left != NULL ) { @@ -95,10 +85,11 @@ SplayNode::~SplayNode() { /* Free this node's key */ // printf( "~SplayNode: %X\t%X\n", key, tree->free_key ); - tree->free_key(key); + + this->free_key(key); /* Note that the data pointers are not freed here. Should be freed with a splay traversal function */ } -#endif /** !_SPLAYTREE_H */ +#endif /** !_SPLAYNODE_HPP */ diff --git a/src/projectM-engine/SplayTree.hpp b/src/projectM-engine/SplayTree.hpp index 19a9a89d6..22776e926 100755 --- a/src/projectM-engine/SplayTree.hpp +++ b/src/projectM-engine/SplayTree.hpp @@ -26,8 +26,8 @@ * $Log$ */ -#ifndef _SPLAYTREE_H -#define _SPLAYTREE_H +#ifndef _SPLAYTREE_HPP +#define _SPLAYTREE_HPP #define REGULAR_NODE_TYPE 0 #define SYMBOLIC_NODE_TYPE 1 @@ -35,16 +35,16 @@ #define PERFECT_MATCH 0 #define CLOSEST_MATCH 1 -#include "projectM.h" #include "SplayNode.hpp" #include "compare.h" -#include "Param.h" +#include "fatal.h" -typedef void Object; - -template +template class SplayTree { + public: + static const int SPLAYTREE_FAILURE = PROJECTM_FAILURE; + static const int SPLAYTREE_SUCCESS = PROJECTM_SUCCESS; SplayNode *root; int (*compare)(void*,void*); void * (*copy_key)(void *); @@ -79,16 +79,12 @@ public: void *splay_find_below_max(void * key); void splay_find_below_max_helper( void *min_key, void **closest_key, SplayNode *root, int (*compare)(void *,void *)); - void *splay_find_min(); - void *splay_find_max(); + Data *splay_find_min(); + Data *splay_find_max(); template void traverse(Fun & functor); - /** Helper functions */ - int insert_param( Param *param ); - Param *find_param_db( char *name, int create_flag ); - private: void splay_traverse_helper (void (*func_ptr)(void*), SplayNode * splaynode); @@ -188,8 +184,9 @@ void SplayTree::splay_traverse_helper (void (*func_ptr)(void *), SplayNode /* Helper function to traverse the entire splaytree */ +template template -void SplayTree::traverseRec (Fun & fun, SplayNode * splaynode) { +void SplayTree::traverseRec (Fun & fun, SplayNode * splaynode) { /* Normal if this happens, its a base case of recursion */ if (splaynode == NULL) @@ -254,7 +251,8 @@ void * SplayTree::splay_find(void * key) { } /* Gets the splaynode that the given key points to */ - SplayNode * SplayTree::get_splaynode_of(void * key) { +template + SplayNode * SplayTree::get_splaynode_of(void * key) { SplayNode * splaynode; int match_type; @@ -340,13 +338,13 @@ int SplayTree::splay_delete(void * key) { /* Use helper function to delete the node and return the resulting tree */ if ((splaynode = splay_delete_helper(key, root, compare, free_key)) == NULL) - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; /* Set new splaytree root equal to the returned splaynode after deletion */ root = splaynode; /* Finished, no errors */ - return PROJECTM_SUCCESS; + return SPLAYTREE_SUCCESS; } /* Deletes a splay node */ @@ -399,14 +397,14 @@ int SplayTree::splay_insert_link(void * alias_key, void * orig_key) { /* Null arguments */ if (alias_key == NULL) - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; if (orig_key == NULL) - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; /* Find the splaynode corresponding to the original key */ if ((data_node = get_splaynode_of(orig_key)) == NULL) - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; /* Create a new splay node of symbolic link type */ if ((splaynode = new SplayNode(SYMBOLIC_NODE_TYPE, (key_clone = copy_key(alias_key)), data_node, this)) == NULL) { @@ -418,11 +416,11 @@ int SplayTree::splay_insert_link(void * alias_key, void * orig_key) { if ((splay_insert_node(splaynode)) < 0) { splaynode->left=splaynode->right = NULL; delete splaynode; - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; } /* Done, return success */ - return PROJECTM_SUCCESS; + return SPLAYTREE_SUCCESS; } /* Inserts 'data' into the 'splaytree' paired with the passed 'key' */ @@ -436,7 +434,7 @@ int SplayTree::splay_insert(Data * data, void * key) { /* Null argument checks */ if (key == NULL) { printf ("splay_insert: null key as argument, returning failure\n"); - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; } /* Clone the key argument */ key_clone = copy_key(key); @@ -453,11 +451,11 @@ int SplayTree::splay_insert(Data * data, void * key) { printf ("splay_insert: failed to insert node.\n"); splaynode->left=splaynode->right=NULL; delete splaynode; - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; } - return PROJECTM_SUCCESS; + return SPLAYTREE_SUCCESS; } /* Helper function to insert splaynodes into the splaytree */ @@ -470,7 +468,7 @@ int SplayTree::splay_insert_node(SplayNode * splaynode) { /* Null argument checks */ if (splaynode == NULL) - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; key = splaynode->key; @@ -481,7 +479,7 @@ int SplayTree::splay_insert_node(SplayNode * splaynode) { if (t == NULL) { splaynode->left = splaynode->right = NULL; root = splaynode; - return PROJECTM_SUCCESS; + return SPLAYTREE_SUCCESS; } @@ -492,7 +490,7 @@ int SplayTree::splay_insert_node(SplayNode * splaynode) { splaynode->right = t; t->left = NULL; root = splaynode; - return PROJECTM_SUCCESS; + return SPLAYTREE_SUCCESS; } @@ -501,13 +499,13 @@ int SplayTree::splay_insert_node(SplayNode * splaynode) { splaynode->left = t; t->right = NULL; root = splaynode; - return PROJECTM_SUCCESS; + return SPLAYTREE_SUCCESS; } /* Item already exists in tree, don't reinsert */ else { printf("splay_insert_node: duplicate key detected, ignoring...\n"); - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; } } @@ -629,7 +627,7 @@ void SplayTree::splay_find_above_min_helper(void * max_key, void ** closes /* Find the minimum entry of the splay tree */ template -void * SplayTree::splay_find_min() { +Data * SplayTree::splay_find_min() { SplayNode * splaynode; @@ -667,7 +665,7 @@ template int SplayTree::splay_size() { if ( root == NULL ) { - return PROJECTM_FAILURE; + return SPLAYTREE_FAILURE; } return splay_rec_size(root); } @@ -682,120 +680,5 @@ int SplayTree::splay_rec_size(SplayNode * splaynode) { } -/** tree_types.cpp */ -/* Compares integer value numbers in 32 bit range */ -int SplayKeyFunctions::compare_int(int * num1, int * num2) { - if ((*num1) < (*num2)) - return -1; - if ((*num1) > (*num2)) - return 1; - - return 0; -} - -/* Compares strings in lexographical order */ -int SplayKeyFunctions::compare_string(char * str1, char * str2) { - - // printf("comparing \"%s\" to \"%s\"\n", str1, str2); - //return strcmp(str1, str2); - return strncmp(str1, str2, MAX_TOKEN_SIZE-1); - -} - -/* Compares a string in version order. That is, file1 < file2 < file10 */ -int SplayKeyFunctions::compare_string_version(char * str1, char * str2) { - - return strcmp( str1, str2 ); -#ifdef PANTS - return strverscmp(str1, str2); -#endif -} - - -static void SplayKeyFunctions::free_int(int * num) { - free(num); -} - - - - void SplayKeyFunctions::free_string(char * string) { - free(string); -} - - - - void * SplayKeyFunctions::copy_int(int * num) { - - int * new_num; - - if ((new_num = (int*)wipemalloc(sizeof(int))) == NULL) - return NULL; - - *new_num = *num; - - return (void*)new_num; -} - - -void * SplayKeyFunctions::copy_string(char * string) { - - char * new_string; - - if ((new_string = (char*)wipemalloc(MAX_TOKEN_SIZE)) == NULL) - return NULL; - - strncpy(new_string, string, MAX_TOKEN_SIZE-1); - - return (void*)new_string; -} - -/* Inserts a parameter into the builtin database */ -template -int SplayTree::insert_param(Param * param) { - - if (param == NULL) - return PROJECTM_FAILURE; - - return splay_insert(param, param->name); -} - -/* Search for parameter 'name' in 'database', if create_flag is true, then generate the parameter - and insert it into 'database' */ -template -Param *SplayTree::find_param_db(char * name, int create_flag) { - - Param * param = NULL; - - /* Null argument checks */ - if (name == NULL) - return NULL; - - /* First look in the builtin database */ - param = (Param *)splay_find(name); - - - if (((param = (Param *)splay_find(name)) == NULL) && (create_flag == TRUE)) { - - /* Check if string is valid */ - if (!param->is_valid_param_string(name)) - return NULL; - - /* Now, create the user defined parameter given the passed name */ - if ((param = new Param(name)) == NULL) - return NULL; - - /* Finally, insert the new parameter into this preset's proper splaytree */ - if (splay_insert(param, param->name) < 0) { - delete param; - return NULL; - } - - } - - /* Return the found (or created) parameter. Note that this could be null */ - return param; - -} - -#endif /** !_SPLAYTREE_H */ +#endif /** !_SPLAYTREE_HPP */ diff --git a/src/projectM-engine/compare.h b/src/projectM-engine/compare.h index 384b62d8b..e8e64adc7 100755 --- a/src/projectM-engine/compare.h +++ b/src/projectM-engine/compare.h @@ -26,14 +26,87 @@ #ifndef _COMPARE_H #define _COMPARE_H +#include "wipemalloc.h" -int compare_int(int * num1, int * num2); -int compare_string(char * string1, char * string2); +/// @bug this will be ripped away when splaytree is more standardly written or its removed in favor of stl-esque data structure +class SplayKeyFunctions { +static int compare_int(int * num1, int * num2); +static int compare_string(char * string1, char * string2); -void free_int(int * num); -void free_string(char * string); -void * copy_int(int * num); -void * copy_string(char * string); -int compare_string_version(char * str1, char * str2); +static void free_int(int * num); +static void free_string(char * string); +static void * copy_int(int * num); +static void * copy_string(char * string); +static int compare_string_version(char * str1, char * str2); +}; + + +/** tree_types.cpp */ +/* Compares integer value numbers in 32 bit range */ +int SplayKeyFunctions::compare_int(int * num1, int * num2) { + + if ((*num1) < (*num2)) + return -1; + if ((*num1) > (*num2)) + return 1; + + return 0; +} + +/* Compares strings in lexographical order */ +int SplayKeyFunctions::compare_string(char * str1, char * str2) { + + // printf("comparing \"%s\" to \"%s\"\n", str1, str2); + //return strcmp(str1, str2); + return strncmp(str1, str2, MAX_TOKEN_SIZE-1); + +} + +/* Compares a string in version order. That is, file1 < file2 < file10 */ +int SplayKeyFunctions::compare_string_version(char * str1, char * str2) { + + return strcmp( str1, str2 ); +#ifdef PANTS + return strverscmp(str1, str2); +#endif +} + + +inline void SplayKeyFunctions::free_int(int * num) { + free(num); +} + + + + inline void SplayKeyFunctions::free_string(char * string) { + free(string); +} + + + + inline void * SplayKeyFunctions::copy_int(int * num) { + + int * new_num; + + if ((new_num = (int*)wipemalloc(sizeof(int))) == NULL) + return NULL; + + *new_num = *num; + + return (void*)new_num; +} + + +inline void * SplayKeyFunctions::copy_string(char * string) { + + char * new_string; + + if ((new_string = (char*)wipemalloc(MAX_TOKEN_SIZE)) == NULL) + return NULL; + + strncpy(new_string, string, MAX_TOKEN_SIZE-1); + + return (void*)new_string; +} #endif /** !_COMPARE_H */ diff --git a/src/projectM-engine/menu.cpp b/src/projectM-engine/menu.cpp index 5540677c3..1ac3d1a68 100755 --- a/src/projectM-engine/menu.cpp +++ b/src/projectM-engine/menu.cpp @@ -33,7 +33,7 @@ #include "common.h" #include "fatal.h" #include "Param.h" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "InitCond.h" diff --git a/src/projectM-engine/projectM.cpp b/src/projectM-engine/projectM.cpp index ccbcdfbd8..eaf00bb32 100755 --- a/src/projectM-engine/projectM.cpp +++ b/src/projectM-engine/projectM.cpp @@ -54,7 +54,7 @@ #include "PCM.h" //Sound data handler (buffering, FFT, etc.) #include "CustomWave.h" #include "CustomShape.h" -#include "SplayTree.h" +#include "SplayTree.hpp" #include "Renderer.hpp" #ifdef USE_FTGL diff --git a/src/projectM-engine/projectM.h b/src/projectM-engine/projectM.h index a07bf6650..7f75e2804 100755 --- a/src/projectM-engine/projectM.h +++ b/src/projectM-engine/projectM.h @@ -77,7 +77,7 @@ class BeatDetect; class Func; class Preset; -class SplayTree; +#include "SplayTree.hpp" #ifdef WIN32 #pragma warning (disable:4244)