mirror of
https://git.do.srb2.org/KartKrew/Kart-Public.git
synced 2024-12-25 11:50:50 +00:00
Split AA trees code from m_misc.c/.h into m_aatree.c/.h
Also updated any relevant project files that I can think of to include the new files, as well as the makefile of course. Some of the other project files haven't been touched in years so I'll leave those alone ...unless someone objects
This commit is contained in:
parent
2e9607938d
commit
69f556d40a
12 changed files with 251 additions and 171 deletions
33
SRB2.cbp
33
SRB2.cbp
|
@ -2815,6 +2815,39 @@ HW3SOUND for 3D hardware sound support
|
|||
<Option target="Debug Mingw64/DirectX" />
|
||||
<Option target="Release Mingw64/DirectX" />
|
||||
</Unit>
|
||||
<Unit filename="src/m_aatree.c">
|
||||
<Option compilerVar="CC" />
|
||||
<Option target="Debug Native/SDL" />
|
||||
<Option target="Release Native/SDL" />
|
||||
<Option target="Debug Mingw/SDL" />
|
||||
<Option target="Release Mingw/SDL" />
|
||||
<Option target="Debug Mingw/DirectX" />
|
||||
<Option target="Release Mingw/DirectX" />
|
||||
<Option target="Debug Any/Dummy" />
|
||||
<Option target="Release Any/Dummy" />
|
||||
<Option target="Debug Linux/SDL" />
|
||||
<Option target="Release Linux/SDL" />
|
||||
<Option target="Debug Mingw64/SDL" />
|
||||
<Option target="Release Mingw64/SDL" />
|
||||
<Option target="Debug Mingw64/DirectX" />
|
||||
<Option target="Release Mingw64/DirectX" />
|
||||
</Unit>
|
||||
<Unit filename="src/m_aatree.h">
|
||||
<Option target="Debug Native/SDL" />
|
||||
<Option target="Release Native/SDL" />
|
||||
<Option target="Debug Mingw/SDL" />
|
||||
<Option target="Release Mingw/SDL" />
|
||||
<Option target="Debug Mingw/DirectX" />
|
||||
<Option target="Release Mingw/DirectX" />
|
||||
<Option target="Debug Any/Dummy" />
|
||||
<Option target="Release Any/Dummy" />
|
||||
<Option target="Debug Linux/SDL" />
|
||||
<Option target="Release Linux/SDL" />
|
||||
<Option target="Debug Mingw64/SDL" />
|
||||
<Option target="Release Mingw64/SDL" />
|
||||
<Option target="Debug Mingw64/DirectX" />
|
||||
<Option target="Release Mingw64/DirectX" />
|
||||
</Unit>
|
||||
<Unit filename="src/m_anigif.c">
|
||||
<Option compilerVar="CC" />
|
||||
<Option target="Debug Native/SDL" />
|
||||
|
|
|
@ -22,6 +22,7 @@ set(SRB2_CORE_SOURCES
|
|||
i_tcp.c
|
||||
info.c
|
||||
lzf.c
|
||||
m_aatree.c
|
||||
m_anigif.c
|
||||
m_argv.c
|
||||
m_bbox.c
|
||||
|
@ -83,6 +84,7 @@ set(SRB2_CORE_HEADERS
|
|||
info.h
|
||||
keys.h
|
||||
lzf.h
|
||||
m_aatree.h
|
||||
m_anigif.h
|
||||
m_argv.h
|
||||
m_bbox.h
|
||||
|
|
|
@ -429,6 +429,7 @@ OBJS:=$(i_main_o) \
|
|||
$(OBJDIR)/hu_stuff.o \
|
||||
$(OBJDIR)/y_inter.o \
|
||||
$(OBJDIR)/st_stuff.o \
|
||||
$(OBJDIR)/m_aatree.o \
|
||||
$(OBJDIR)/m_anigif.o \
|
||||
$(OBJDIR)/m_argv.o \
|
||||
$(OBJDIR)/m_bbox.o \
|
||||
|
|
167
src/m_aatree.c
Normal file
167
src/m_aatree.c
Normal file
|
@ -0,0 +1,167 @@
|
|||
// SONIC ROBO BLAST 2
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) 1993-1996 by id Software, Inc.
|
||||
// Copyright (C) 1998-2000 by DooM Legacy Team.
|
||||
// Copyright (C) 1999-2016 by Sonic Team Junior.
|
||||
//
|
||||
// This program is free software distributed under the
|
||||
// terms of the GNU General Public License, version 2.
|
||||
// See the 'LICENSE' file for more details.
|
||||
//-----------------------------------------------------------------------------
|
||||
/// \file m_aatree.h
|
||||
/// \brief AA trees code
|
||||
|
||||
#include "m_aatree.h"
|
||||
#include "z_zone.h"
|
||||
|
||||
// A partial implementation of AA trees,
|
||||
// according to the algorithms given on Wikipedia.
|
||||
// http://en.wikipedia.org/wiki/AA_tree
|
||||
|
||||
typedef struct aatree_node_s
|
||||
{
|
||||
INT32 level;
|
||||
INT32 key;
|
||||
void* value;
|
||||
|
||||
struct aatree_node_s *left, *right;
|
||||
} aatree_node_t;
|
||||
|
||||
struct aatree_s
|
||||
{
|
||||
aatree_node_t *root;
|
||||
UINT32 flags;
|
||||
};
|
||||
|
||||
aatree_t *M_AATreeAlloc(UINT32 flags)
|
||||
{
|
||||
aatree_t *aatree = Z_Malloc(sizeof (aatree_t), PU_STATIC, NULL);
|
||||
aatree->root = NULL;
|
||||
aatree->flags = flags;
|
||||
return aatree;
|
||||
}
|
||||
|
||||
static void M_AATreeFree_Node(aatree_node_t *node)
|
||||
{
|
||||
if (node->left) M_AATreeFree_Node(node->left);
|
||||
if (node->right) M_AATreeFree_Node(node->right);
|
||||
Z_Free(node);
|
||||
}
|
||||
|
||||
void M_AATreeFree(aatree_t *aatree)
|
||||
{
|
||||
if (aatree->root)
|
||||
M_AATreeFree_Node(aatree->root);
|
||||
|
||||
Z_Free(aatree);
|
||||
}
|
||||
|
||||
static aatree_node_t *M_AATreeSkew(aatree_node_t *node)
|
||||
{
|
||||
if (node && node->left && node->left->level == node->level)
|
||||
{
|
||||
// Not allowed: horizontal left-link. Reverse the
|
||||
// horizontal link and hook the orphan back in.
|
||||
aatree_node_t *oldleft = node->left;
|
||||
node->left = oldleft->right;
|
||||
oldleft->right = node;
|
||||
|
||||
return oldleft;
|
||||
}
|
||||
|
||||
// No change needed.
|
||||
return node;
|
||||
}
|
||||
|
||||
static aatree_node_t *M_AATreeSplit(aatree_node_t *node)
|
||||
{
|
||||
if (node && node->right && node->right->right && node->level == node->right->right->level)
|
||||
{
|
||||
// Not allowed: two consecutive horizontal right-links.
|
||||
// The middle one becomes the new root at this point,
|
||||
// with suitable adjustments below.
|
||||
|
||||
aatree_node_t *oldright = node->right;
|
||||
node->right = oldright->left;
|
||||
oldright->left = node;
|
||||
oldright->level++;
|
||||
|
||||
return oldright;
|
||||
}
|
||||
|
||||
// No change needed.
|
||||
return node;
|
||||
}
|
||||
|
||||
static aatree_node_t *M_AATreeSet_Node(aatree_node_t *node, UINT32 flags, INT32 key, void* value)
|
||||
{
|
||||
if (!node)
|
||||
{
|
||||
// Nothing here, so just add where we are
|
||||
|
||||
node = Z_Malloc(sizeof (aatree_node_t), PU_STATIC, NULL);
|
||||
node->level = 1;
|
||||
node->key = key;
|
||||
if (value && (flags & AATREE_ZUSER)) Z_SetUser(value, &node->value);
|
||||
else node->value = value;
|
||||
node->left = node->right = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (key < node->key)
|
||||
node->left = M_AATreeSet_Node(node->left, flags, key, value);
|
||||
else if (key > node->key)
|
||||
node->right = M_AATreeSet_Node(node->right, flags, key, value);
|
||||
else
|
||||
{
|
||||
if (value && (flags & AATREE_ZUSER)) Z_SetUser(value, &node->value);
|
||||
else node->value = value;
|
||||
}
|
||||
|
||||
node = M_AATreeSkew(node);
|
||||
node = M_AATreeSplit(node);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void M_AATreeSet(aatree_t *aatree, INT32 key, void* value)
|
||||
{
|
||||
aatree->root = M_AATreeSet_Node(aatree->root, aatree->flags, key, value);
|
||||
}
|
||||
|
||||
// Caveat: we don't distinguish between nodes that don't exists
|
||||
// and nodes with value == NULL.
|
||||
static void *M_AATreeGet_Node(aatree_node_t *node, INT32 key)
|
||||
{
|
||||
if (node)
|
||||
{
|
||||
if (node->key == key)
|
||||
return node->value;
|
||||
else if(node->key < key)
|
||||
return M_AATreeGet_Node(node->right, key);
|
||||
else
|
||||
return M_AATreeGet_Node(node->left, key);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *M_AATreeGet(aatree_t *aatree, INT32 key)
|
||||
{
|
||||
return M_AATreeGet_Node(aatree->root, key);
|
||||
}
|
||||
|
||||
|
||||
static void M_AATreeIterate_Node(aatree_node_t *node, aatree_iter_t callback)
|
||||
{
|
||||
if (node->left) M_AATreeIterate_Node(node->left, callback);
|
||||
callback(node->key, node->value);
|
||||
if (node->right) M_AATreeIterate_Node(node->right, callback);
|
||||
}
|
||||
|
||||
void M_AATreeIterate(aatree_t *aatree, aatree_iter_t callback)
|
||||
{
|
||||
if (aatree->root)
|
||||
M_AATreeIterate_Node(aatree->root, callback);
|
||||
}
|
31
src/m_aatree.h
Normal file
31
src/m_aatree.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
// SONIC ROBO BLAST 2
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) 1993-1996 by id Software, Inc.
|
||||
// Copyright (C) 1998-2000 by DooM Legacy Team.
|
||||
// Copyright (C) 1999-2016 by Sonic Team Junior.
|
||||
//
|
||||
// This program is free software distributed under the
|
||||
// terms of the GNU General Public License, version 2.
|
||||
// See the 'LICENSE' file for more details.
|
||||
//-----------------------------------------------------------------------------
|
||||
/// \file m_aatree.h
|
||||
/// \brief AA trees code
|
||||
|
||||
#ifndef __M_AATREE__
|
||||
#define __M_AATREE__
|
||||
|
||||
#include "doomtype.h"
|
||||
|
||||
// Flags for AA trees.
|
||||
#define AATREE_ZUSER 1 // Treat values as z_zone-allocated blocks and set their user fields
|
||||
|
||||
typedef struct aatree_s aatree_t;
|
||||
typedef void (*aatree_iter_t)(INT32 key, void *value);
|
||||
|
||||
aatree_t *M_AATreeAlloc(UINT32 flags);
|
||||
void M_AATreeFree(aatree_t *aatree);
|
||||
void M_AATreeSet(aatree_t *aatree, INT32 key, void* value);
|
||||
void *M_AATreeGet(aatree_t *aatree, INT32 key);
|
||||
void M_AATreeIterate(aatree_t *aatree, aatree_iter_t callback);
|
||||
|
||||
#endif
|
155
src/m_misc.c
155
src/m_misc.c
|
@ -2323,158 +2323,3 @@ void M_SetupMemcpy(void)
|
|||
M_Memcpy = cpu_cpy;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// A partial implementation of AA trees,
|
||||
// according to the algorithms given on Wikipedia.
|
||||
// http://en.wikipedia.org/wiki/AA_tree
|
||||
|
||||
|
||||
|
||||
typedef struct aatree_node_s
|
||||
{
|
||||
INT32 level;
|
||||
INT32 key;
|
||||
void* value;
|
||||
|
||||
struct aatree_node_s *left, *right;
|
||||
} aatree_node_t;
|
||||
|
||||
struct aatree_s
|
||||
{
|
||||
aatree_node_t *root;
|
||||
UINT32 flags;
|
||||
};
|
||||
|
||||
aatree_t *M_AATreeAlloc(UINT32 flags)
|
||||
{
|
||||
aatree_t *aatree = Z_Malloc(sizeof (aatree_t), PU_STATIC, NULL);
|
||||
aatree->root = NULL;
|
||||
aatree->flags = flags;
|
||||
return aatree;
|
||||
}
|
||||
|
||||
static void M_AATreeFree_Node(aatree_node_t *node)
|
||||
{
|
||||
if (node->left) M_AATreeFree_Node(node->left);
|
||||
if (node->right) M_AATreeFree_Node(node->right);
|
||||
Z_Free(node);
|
||||
}
|
||||
|
||||
void M_AATreeFree(aatree_t *aatree)
|
||||
{
|
||||
if (aatree->root)
|
||||
M_AATreeFree_Node(aatree->root);
|
||||
|
||||
Z_Free(aatree);
|
||||
}
|
||||
|
||||
static aatree_node_t *M_AATreeSkew(aatree_node_t *node)
|
||||
{
|
||||
if (node && node->left && node->left->level == node->level)
|
||||
{
|
||||
// Not allowed: horizontal left-link. Reverse the
|
||||
// horizontal link and hook the orphan back in.
|
||||
aatree_node_t *oldleft = node->left;
|
||||
node->left = oldleft->right;
|
||||
oldleft->right = node;
|
||||
|
||||
return oldleft;
|
||||
}
|
||||
|
||||
// No change needed.
|
||||
return node;
|
||||
}
|
||||
|
||||
static aatree_node_t *M_AATreeSplit(aatree_node_t *node)
|
||||
{
|
||||
if (node && node->right && node->right->right && node->level == node->right->right->level)
|
||||
{
|
||||
// Not allowed: two consecutive horizontal right-links.
|
||||
// The middle one becomes the new root at this point,
|
||||
// with suitable adjustments below.
|
||||
|
||||
aatree_node_t *oldright = node->right;
|
||||
node->right = oldright->left;
|
||||
oldright->left = node;
|
||||
oldright->level++;
|
||||
|
||||
return oldright;
|
||||
}
|
||||
|
||||
// No change needed.
|
||||
return node;
|
||||
}
|
||||
|
||||
static aatree_node_t *M_AATreeSet_Node(aatree_node_t *node, UINT32 flags, INT32 key, void* value)
|
||||
{
|
||||
if (!node)
|
||||
{
|
||||
// Nothing here, so just add where we are
|
||||
|
||||
node = Z_Malloc(sizeof (aatree_node_t), PU_STATIC, NULL);
|
||||
node->level = 1;
|
||||
node->key = key;
|
||||
if (value && (flags & AATREE_ZUSER)) Z_SetUser(value, &node->value);
|
||||
else node->value = value;
|
||||
node->left = node->right = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (key < node->key)
|
||||
node->left = M_AATreeSet_Node(node->left, flags, key, value);
|
||||
else if (key > node->key)
|
||||
node->right = M_AATreeSet_Node(node->right, flags, key, value);
|
||||
else
|
||||
{
|
||||
if (value && (flags & AATREE_ZUSER)) Z_SetUser(value, &node->value);
|
||||
else node->value = value;
|
||||
}
|
||||
|
||||
node = M_AATreeSkew(node);
|
||||
node = M_AATreeSplit(node);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void M_AATreeSet(aatree_t *aatree, INT32 key, void* value)
|
||||
{
|
||||
aatree->root = M_AATreeSet_Node(aatree->root, aatree->flags, key, value);
|
||||
}
|
||||
|
||||
// Caveat: we don't distinguish between nodes that don't exists
|
||||
// and nodes with value == NULL.
|
||||
static void *M_AATreeGet_Node(aatree_node_t *node, INT32 key)
|
||||
{
|
||||
if (node)
|
||||
{
|
||||
if (node->key == key)
|
||||
return node->value;
|
||||
else if(node->key < key)
|
||||
return M_AATreeGet_Node(node->right, key);
|
||||
else
|
||||
return M_AATreeGet_Node(node->left, key);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *M_AATreeGet(aatree_t *aatree, INT32 key)
|
||||
{
|
||||
return M_AATreeGet_Node(aatree->root, key);
|
||||
}
|
||||
|
||||
|
||||
static void M_AATreeIterate_Node(aatree_node_t *node, aatree_iter_t callback)
|
||||
{
|
||||
if (node->left) M_AATreeIterate_Node(node->left, callback);
|
||||
callback(node->key, node->value);
|
||||
if (node->right) M_AATreeIterate_Node(node->right, callback);
|
||||
}
|
||||
|
||||
void M_AATreeIterate(aatree_t *aatree, aatree_iter_t callback)
|
||||
{
|
||||
if (aatree->root)
|
||||
M_AATreeIterate_Node(aatree->root, callback);
|
||||
}
|
||||
|
|
13
src/m_misc.h
13
src/m_misc.h
|
@ -96,19 +96,6 @@ void M_SetupMemcpy(void);
|
|||
// counting bits, for weapon ammo code, usually
|
||||
FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size);
|
||||
|
||||
// Flags for AA trees.
|
||||
#define AATREE_ZUSER 1 // Treat values as z_zone-allocated blocks and set their user fields
|
||||
|
||||
typedef struct aatree_s aatree_t;
|
||||
typedef void (*aatree_iter_t)(INT32 key, void *value);
|
||||
|
||||
aatree_t *M_AATreeAlloc(UINT32 flags);
|
||||
void M_AATreeFree(aatree_t *aatree);
|
||||
void M_AATreeSet(aatree_t *aatree, INT32 key, void* value);
|
||||
void *M_AATreeGet(aatree_t *aatree, INT32 key);
|
||||
void M_AATreeIterate(aatree_t *aatree, aatree_iter_t callback);
|
||||
|
||||
// Nasty cyclic dependency workaround. This must come after aatree stuff.
|
||||
#include "w_wad.h"
|
||||
extern char configfile[MAX_WADPATH];
|
||||
|
||||
|
|
|
@ -167,6 +167,7 @@
|
|||
<ClInclude Include="..\lzf.h" />
|
||||
<ClInclude Include="..\md5.h" />
|
||||
<ClInclude Include="..\mserv.h" />
|
||||
<ClInclude Include="..\m_aatree.h" />
|
||||
<ClInclude Include="..\m_anigif.h" />
|
||||
<ClInclude Include="..\m_argv.h" />
|
||||
<ClInclude Include="..\m_bbox.h" />
|
||||
|
@ -308,6 +309,7 @@
|
|||
<ClCompile Include="..\lzf.c" />
|
||||
<ClCompile Include="..\md5.c" />
|
||||
<ClCompile Include="..\mserv.c" />
|
||||
<ClCompile Include="..\m_aatree.c" />
|
||||
<ClCompile Include="..\m_anigif.c" />
|
||||
<ClCompile Include="..\m_argv.c" />
|
||||
<ClCompile Include="..\m_bbox.c" />
|
||||
|
|
|
@ -294,6 +294,9 @@
|
|||
<ClInclude Include="..\md5.h">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\m_aatree.h">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\m_anigif.h">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClInclude>
|
||||
|
@ -666,6 +669,9 @@
|
|||
<ClCompile Include="..\md5.c">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\m_aatree.c">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\m_anigif.c">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClCompile>
|
||||
|
|
|
@ -54,10 +54,8 @@ typedef struct
|
|||
|
||||
#define lumpcache_t void *
|
||||
|
||||
// Annoying cyclic dependency workaround: this inlcusion must come after
|
||||
// the definition of MAX_WADPATH.
|
||||
#ifdef HWRENDER
|
||||
#include "m_misc.h"
|
||||
#include "m_aatree.h"
|
||||
#endif
|
||||
|
||||
typedef struct wadfile_s
|
||||
|
|
|
@ -145,6 +145,7 @@
|
|||
<ClCompile Include="..\lzf.c" />
|
||||
<ClCompile Include="..\md5.c" />
|
||||
<ClCompile Include="..\mserv.c" />
|
||||
<ClCompile Include="..\m_aatree.c" />
|
||||
<ClCompile Include="..\m_anigif.c" />
|
||||
<ClCompile Include="..\m_argv.c" />
|
||||
<ClCompile Include="..\m_bbox.c" />
|
||||
|
@ -300,6 +301,7 @@
|
|||
<ClInclude Include="..\lzf.h" />
|
||||
<ClInclude Include="..\md5.h" />
|
||||
<ClInclude Include="..\mserv.h" />
|
||||
<ClInclude Include="..\m_aatree.h" />
|
||||
<ClInclude Include="..\m_anigif.h" />
|
||||
<ClInclude Include="..\m_argv.h" />
|
||||
<ClInclude Include="..\m_bbox.h" />
|
||||
|
|
|
@ -255,6 +255,9 @@
|
|||
<ClCompile Include="..\lua_skinlib.c">
|
||||
<Filter>LUA</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\m_aatree.c">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\m_anigif.c">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClCompile>
|
||||
|
@ -662,6 +665,9 @@
|
|||
<ClInclude Include="..\md5.h">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\m_aatree.h">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\m_anigif.h">
|
||||
<Filter>M_Misc</Filter>
|
||||
</ClInclude>
|
||||
|
|
Loading…
Reference in a new issue