From 33290c882e9cc551b633142bf65463e0724e78ca Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Sun, 24 Jun 2007 15:50:24 +0000 Subject: [PATCH] - added const correctness to keys in splaytree - fixed but haven't tested a new alias system in builtin params class git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/personal/carm/dev-1.0@222 6778bc44-b910-0410-a7a0-be141de4315d --- src/projectM-engine/BuiltinFuncs.cpp | 2 +- src/projectM-engine/BuiltinParams.cpp | 17 +++++++++-------- src/projectM-engine/BuiltinParams.hpp | 9 ++++----- src/projectM-engine/CustomShape.cpp | 8 ++++---- src/projectM-engine/CustomWave.cpp | 10 +++++----- src/projectM-engine/Preset.cpp | 18 +++++++++--------- src/projectM-engine/SplayTree.hpp | 18 +++++++++--------- src/projectM-engine/compare.h | 12 ++++++------ 8 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/projectM-engine/BuiltinFuncs.cpp b/src/projectM-engine/BuiltinFuncs.cpp index 19f8ddbff..ce02ec69f 100644 --- a/src/projectM-engine/BuiltinFuncs.cpp +++ b/src/projectM-engine/BuiltinFuncs.cpp @@ -127,7 +127,7 @@ int BuiltinFuncs::init_builtin_func_db() { int retval; builtin_func_tree = - SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string, (void(*)(void*))SplayKeyFunctions::free_string); + SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string, (void(*)(void*))SplayKeyFunctions::free_string); if (builtin_func_tree == NULL) return PROJECTM_OUTOFMEM_ERROR; diff --git a/src/projectM-engine/BuiltinParams.cpp b/src/projectM-engine/BuiltinParams.cpp index dd74e28ca..4f71da1c4 100644 --- a/src/projectM-engine/BuiltinParams.cpp +++ b/src/projectM-engine/BuiltinParams.cpp @@ -93,22 +93,23 @@ int BuiltinParams::destroy_builtin_param_db() { /* Insert a parameter into the database with an alternate name */ int BuiltinParams::insert_param_alt_name(Param *param, char * alt_name) { - if (alt_name == NULL) - return PROJECTM_ERROR; + assert(alt_name); - builtin_param_tree->splay_insert_link(alt_name, param->name); + aliasMap.insert(std::make_pair(std::string(alt_name), std::string(param->name))); return PROJECTM_SUCCESS; } Param * BuiltinParams::find_builtin_param(char * name) { - /* Null argument checks */ - if (name == NULL) - return NULL; + assert(name); - return (Param*)builtin_param_tree->splay_find(name); + AliasMap::iterator pos = aliasMap.find(std::string(name)); + if (pos == aliasMap.end()) + return builtin_param_tree->splay_find(name); + else + return builtin_param_tree->splay_find(pos->second.c_str()); } @@ -185,7 +186,7 @@ int BuiltinParams::insert_builtin_param( Param *param ) { int BuiltinParams::init_builtin_param_db(const PresetInputs & presetInputs, PresetOutputs & presetOutputs) { /* Create the builtin parameter splay tree (go Sleator...) */ - if ((this->builtin_param_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { + if ((this->builtin_param_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { if (BUILTIN_PARAMS_DEBUG) printf("init_builtin_param_db: failed to initialize database (FATAL)\n"); return PROJECTM_OUTOFMEM_ERROR; } diff --git a/src/projectM-engine/BuiltinParams.hpp b/src/projectM-engine/BuiltinParams.hpp index 08ebbf9e4..c032fee2e 100644 --- a/src/projectM-engine/BuiltinParams.hpp +++ b/src/projectM-engine/BuiltinParams.hpp @@ -29,11 +29,12 @@ #include "PresetFrameIO.hpp" #include "Param.h" - +#include class BuiltinParams { public: + typedef std::map AliasMap; /** Default constructor leaves database in an uninitialized state. */ BuiltinParams(); @@ -67,14 +68,12 @@ public: builtin_param_tree->traverse(fun); } -//void BuiltinParams::traverse(void (*func_ptr)(void*)) { -// builtin_param_tree->splay_traverse(func_ptr); -//} void traverse(void (*func_ptr)(void*)); private: static const bool BUILTIN_PARAMS_DEBUG = false; - SplayTree *builtin_param_tree; + AliasMap aliasMap; + SplayTree * builtin_param_tree; }; #endif diff --git a/src/projectM-engine/CustomShape.cpp b/src/projectM-engine/CustomShape.cpp index 81b8168c5..f569977e7 100755 --- a/src/projectM-engine/CustomShape.cpp +++ b/src/projectM-engine/CustomShape.cpp @@ -55,16 +55,16 @@ CustomShape::CustomShape( int id ) { /* Initialize tree data structures */ this->param_tree = - SplayTree::create_splaytree( (int (*)(void*,void*))SplayKeyFunctions::compare_string, (void* (*)(void*)) SplayKeyFunctions::copy_string,(void (*)(void*)) SplayKeyFunctions::free_string); + SplayTree::create_splaytree( (int (*)(const void*,const void*))SplayKeyFunctions::compare_string, (void* (*)(void*)) SplayKeyFunctions::copy_string,(void (*)(void*)) SplayKeyFunctions::free_string); this->per_frame_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int,(void (*)(void*))SplayKeyFunctions::free_int); + SplayTree::create_splaytree((int (*)(const void*, const void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int,(void (*)(void*))SplayKeyFunctions::free_int); this->init_cond_tree = - SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_string, (void* (*)(void*)) SplayKeyFunctions::copy_string,(void (*)(void*)) SplayKeyFunctions::free_string); + SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_string, (void* (*)(void*)) SplayKeyFunctions::copy_string,(void (*)(void*)) SplayKeyFunctions::free_string); this->per_frame_init_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*)) SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); + SplayTree::create_splaytree((int (*)(const void*, const void*)) SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); /* Start: Load custom shape parameters */ param = Param::new_param_float("r", P_FLAG_NONE, &this->r, NULL, 1.0, 0.0, 0.5); diff --git a/src/projectM-engine/CustomWave.cpp b/src/projectM-engine/CustomWave.cpp index 38325f3f8..a724b3be5 100755 --- a/src/projectM-engine/CustomWave.cpp +++ b/src/projectM-engine/CustomWave.cpp @@ -80,32 +80,32 @@ CustomWave::CustomWave(int id):id(id) /* Initialize tree data structures */ if ((this->param_tree = - SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { + SplayTree::create_splaytree((int (*)(const void*, const void*))SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { delete(this); abort(); } if ((this->per_point_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int)) == NULL) { + SplayTree::create_splaytree((int (*)(const void*, const void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int)) == NULL) { delete(this); abort(); } if ((this->per_frame_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int,(void (*)(void*)) SplayKeyFunctions::free_int)) == NULL) { + SplayTree::create_splaytree((int (*)(const void*, const void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int,(void (*)(void*)) SplayKeyFunctions::free_int)) == NULL) { delete(this); abort(); } if ((this->init_cond_tree = - SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string,(void (*)(void*)) SplayKeyFunctions::free_string)) == NULL) { + SplayTree::create_splaytree((int (*)(const void*, const void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string,(void (*)(void*)) SplayKeyFunctions::free_string)) == NULL) { delete(this); /// @bug make exception abort(); } if ((this->per_frame_init_eqn_tree = - SplayTree::create_splaytree((int (*)(void*, void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { + SplayTree::create_splaytree((int (*)(const void*, const void*))SplayKeyFunctions::compare_string, (void*(*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string)) == NULL) { delete(this); /// @bug make exception abort(); diff --git a/src/projectM-engine/Preset.cpp b/src/projectM-engine/Preset.cpp index f188412f7..8e91b8f6e 100755 --- a/src/projectM-engine/Preset.cpp +++ b/src/projectM-engine/Preset.cpp @@ -128,7 +128,7 @@ void Preset::reloadPerPixel(char *s) { per_pixel_eqn_tree->splay_traverse((void (*)(void*))free_per_pixel_eqn_helper); delete per_pixel_eqn_tree; per_pixel_eqn_tree = SplayTree::create_splaytree - ((int (*)(void*,void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); + ((int (*)(const void*,const void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); /* Convert string to a stream */ #if !defined(MACOS) && !defined(WIN32) @@ -180,7 +180,7 @@ void Preset::reloadPerFrame(char * s) { /* Clear previous per frame equations */ per_frame_eqn_tree->splay_traverse((void (*)(void*))free_per_frame_eqn_helper ); delete per_frame_eqn_tree; - per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); + per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); /* Convert string to a stream */ //FIXME @@ -225,13 +225,13 @@ printf( "reloadPerFrame()\n" ); void Preset::initialize(const std::string & pathname) { /* Initialize equation trees */ - init_cond_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); - this->user_param_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); - this->per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*)) SplayKeyFunctions::free_int); - this->per_pixel_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); - this->per_frame_init_eqn_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*)) SplayKeyFunctions::free_string); - this->custom_wave_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*)) SplayKeyFunctions::free_int); - this->custom_shape_tree = SplayTree::create_splaytree((int (*)(void*,void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); + init_cond_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_string, (void* (*)(void*))SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); + this->user_param_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*))SplayKeyFunctions::free_string); + this->per_frame_eqn_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*)) SplayKeyFunctions::free_int); + this->per_pixel_eqn_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_int,(void* (*)(void*)) SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); + this->per_frame_init_eqn_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_string,(void* (*)(void*)) SplayKeyFunctions::copy_string, (void (*)(void*)) SplayKeyFunctions::free_string); + this->custom_wave_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*)) SplayKeyFunctions::free_int); + this->custom_shape_tree = SplayTree::create_splaytree((int (*)(const void*,const void*))SplayKeyFunctions::compare_int, (void* (*)(void*))SplayKeyFunctions::copy_int, (void (*)(void*))SplayKeyFunctions::free_int); memset(this->per_pixel_flag, 0, sizeof(int)*NUM_OPS); diff --git a/src/projectM-engine/SplayTree.hpp b/src/projectM-engine/SplayTree.hpp index e1536ee28..f462a44a6 100755 --- a/src/projectM-engine/SplayTree.hpp +++ b/src/projectM-engine/SplayTree.hpp @@ -46,25 +46,25 @@ public: static const int SPLAYTREE_FAILURE = PROJECTM_FAILURE; static const int SPLAYTREE_SUCCESS = PROJECTM_SUCCESS; SplayNode *root; - int (*compare)(void*,void*); + int (*compare)(const void*,const void*); void * (*copy_key)(void *); void (*free_key)(void*); - static SplayTree *create_splaytree(int (*compare)(void*,void*), void * (*copy_key)(void*), void (*free_key)(void*)); + static SplayTree *create_splaytree(int (*compare)(const void*,const void*), void * (*copy_key)(void*), void (*free_key)(void*)); ~SplayTree(); - Data *splay_find(void * key); + Data *splay_find(const void * key); int splay_insert(Data * data, void * key); int splay_insert_node( SplayNode *node ); int splay_insert_link(void * alias_key, void * orig_key); int splay_delete(void * key); SplayNode *splay_delete_helper( void *key, SplayNode *node, - int (*compare)(void*,void*), + int (*compare)(const void *,const void*), void (*free_key)(void*) ); int splay_size(); int splay_rec_size( SplayNode *node ); - SplayNode *splay( void *key, SplayNode *t, int *match_type, int (*compare)(void *,void *) ); + SplayNode *splay( const void *key, SplayNode *t, int *match_type, int (*compare)(const void *,const void *) ); /** Traverses the entire splaytree in order given a function pointer * @deprecated Use the traverse method instead @@ -106,7 +106,7 @@ private: /* Creates a splay tree given a compare key function, copy key function, and free key function. Ah yes, the wonders of procedural programming */ template -SplayTree * SplayTree::create_splaytree(int (*compare)(void *,void*), void * (*copy_key)(void *), void (*free_key)(void*)) { +SplayTree * SplayTree::create_splaytree(int (*compare)(const void *,const void*), void * (*copy_key)(void *), void (*free_key)(void*)) { SplayTree * splaytree; @@ -215,7 +215,7 @@ void SplayTree::traverseRec (Fun & fun, SplayNode * splaynode) { } /* Find the node corresponding to the given key in splaytree, return its data pointer */ template -Data * SplayTree::splay_find(void * key) { +Data * SplayTree::splay_find(const void * key) { SplayNode * splaynode; int match_type; @@ -268,7 +268,7 @@ template /* Finds the desired node, and changes the tree such that it is the root */ template -SplayNode * SplayTree::splay (void * key, SplayNode * t, int * match_type, int (*compare)(void*,void*)) { +SplayNode * SplayTree::splay (const void * key, SplayNode * t, int * match_type, int (*compare)(const void*,const void*)) { /* Simple top down splay, not requiring key to be in the tree t. What it does is described above. */ @@ -341,7 +341,7 @@ int SplayTree::splay_delete(void * key) { /* Deletes a splay node */ template SplayNode * SplayTree::splay_delete_helper(void * key, SplayNode * splaynode, - int (*compare)(void*,void*), void (*free_key)(void*)) { + int (*compare)(const void*,const void*), void (*free_key)(void*)) { SplayNode * new_root; int match_type; diff --git a/src/projectM-engine/compare.h b/src/projectM-engine/compare.h index fa06657e4..d34da2262 100755 --- a/src/projectM-engine/compare.h +++ b/src/projectM-engine/compare.h @@ -31,20 +31,20 @@ /// @bug this will be ripped away when splaytree is more standardly written or its removed in favor of stl-esque data structure class SplayKeyFunctions { public: -static int compare_int(int * num1, int * num2); -static int compare_string(char * string1, char * string2); +static int compare_int(const int * num1, const int * num2); +static int compare_string(const char * string1, const char * string2); 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); +static int compare_string_version(const char * str1, const char * str2); }; /** tree_types.cpp */ /* Compares integer value numbers in 32 bit range */ -inline int SplayKeyFunctions::compare_int(int * num1, int * num2) { +inline int SplayKeyFunctions::compare_int(const int * num1, const int * num2) { if ((*num1) < (*num2)) return -1; @@ -55,7 +55,7 @@ inline int SplayKeyFunctions::compare_int(int * num1, int * num2) { } /* Compares strings in lexographical order */ -inline int SplayKeyFunctions::compare_string(char * str1, char * str2) { +inline int SplayKeyFunctions::compare_string(const char * str1, const char * str2) { // printf("comparing \"%s\" to \"%s\"\n", str1, str2); //return strcmp(str1, str2); @@ -64,7 +64,7 @@ inline int SplayKeyFunctions::compare_string(char * str1, char * str2) { } /* Compares a string in version order. That is, file1 < file2 < file10 */ -inline int SplayKeyFunctions::compare_string_version(char * str1, char * str2) { +inline int SplayKeyFunctions::compare_string_version(const char * str1, const char * str2) { return strcmp( str1, str2 ); #ifdef PANTS