Skip to content

Commit

Permalink
fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjipeng committed Feb 7, 2025
1 parent d5ce546 commit 8808081
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 73 deletions.
40 changes: 0 additions & 40 deletions ext/common/psx_array.h

This file was deleted.

22 changes: 11 additions & 11 deletions ext/common/psx_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@

#define INIT_CAPACITY 4

class tree_node
class psx_tree_node
{
public:
tree_node(tree_node* parent)
psx_tree_node(psx_tree_node* parent)
: m_parent(parent)
, m_children(NULL)
, m_count(0)
, m_capacity(INIT_CAPACITY)
, m_index(0)
{
m_children = (tree_node**)calloc(m_capacity, sizeof(tree_node*));
m_children = (psx_tree_node**)calloc(m_capacity, sizeof(psx_tree_node*));
if (m_parent) {
m_parent->m_count++;
if (m_parent->m_count == m_parent->m_capacity) {
m_parent->m_capacity <<= 1;
m_parent->m_children = (tree_node**)realloc(m_parent->m_children, sizeof(tree_node*) * m_parent->m_capacity);
m_parent->m_children = (psx_tree_node**)realloc(m_parent->m_children, sizeof(psx_tree_node*) * m_parent->m_capacity);
}
m_parent->m_children[m_parent->m_count - 1] = this;
m_index = m_parent->m_count;
}
}

virtual ~tree_node()
virtual ~psx_tree_node()
{
if (m_parent) {
if (m_parent->m_children[m_index - 1] == this) {
Expand All @@ -70,9 +70,9 @@ class tree_node

uint32_t child_count(void) const { return m_count; }

tree_node* parent(void) const { return m_parent; }
psx_tree_node* parent(void) const { return m_parent; }

tree_node* get_child(uint32_t idx) const
psx_tree_node* get_child(uint32_t idx) const
{
if (idx >= m_count) {
return NULL;
Expand All @@ -81,8 +81,8 @@ class tree_node
}

private:
tree_node* m_parent;
tree_node** m_children;
psx_tree_node* m_parent;
psx_tree_node** m_children;
uint32_t m_count : 16;
uint32_t m_capacity : 16;
uint32_t m_index : 16;
Expand All @@ -93,10 +93,10 @@ typedef enum {
PSX_TREE_WALK_POST_ORDER,
} PSX_TREE_WALK_MODE;

typedef bool (*psx_tree_traversal_callback)(const tree_node* node, void* ctx);
typedef bool (*psx_tree_traversal_callback)(const psx_tree_node* node, void* ctx);

template <PSX_TREE_WALK_MODE mode>
static bool psx_tree_traversal(const tree_node* tree_head, void* ctx, psx_tree_traversal_callback cb,
static bool psx_tree_traversal(const psx_tree_node* tree_head, void* ctx, psx_tree_traversal_callback cb,
psx_tree_traversal_callback before = NULL,
psx_tree_traversal_callback after = NULL)
{
Expand Down
14 changes: 7 additions & 7 deletions src/include/data_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ class pod_array

uint32_t size(void) const { return m_size; }

const T& operator [] (unsigned i) const { return m_array[i]; }
T& operator [] (unsigned i) { return m_array[i]; }
const T& operator [] (uint32_t i) const { return m_array[i]; }
T& operator [] (uint32_t i) { return m_array[i]; }

const T& at(unsigned i) const { return m_array[i]; }
T& at(unsigned i) { return m_array[i]; }
const T& at(uint32_t i) const { return m_array[i]; }
T& at(uint32_t i) { return m_array[i]; }

T value_at(unsigned i) const { return m_array[i]; }
T value_at(uint32_t i) const { return m_array[i]; }

const T* data() const { return m_array; }
T* data(void) { return m_array; }
Expand Down Expand Up @@ -505,7 +505,7 @@ class block_allocator

public:

block_allocator(unsigned block_size, unsigned block_ptr_inc = 256 - 8)
block_allocator(uint32_t block_size, uint32_t block_ptr_inc = 256 - 8)
: m_block_size(block_size)
, m_block_ptr_inc(block_ptr_inc)
, m_num_blocks(0)
Expand Down Expand Up @@ -747,7 +747,7 @@ void quick_sort(Array& array, LessFunc less)
// Remove duplicates from a sorted array. It doesn't cut the
// tail of the array, it just returns the number of remaining elements.
template <typename Array, typename EqualFunc>
unsigned remove_duplicates(Array& array, EqualFunc equal)
uint32_t remove_duplicates(Array& array, EqualFunc equal)
{
if (array.size() < 2) {
return array.size();
Expand Down
24 changes: 12 additions & 12 deletions unit_tests/ext_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include "psx_tree.h"

class test_tree : public tree_node {
class test_tree : public psx_tree_node {
public:
test_tree(test_tree * parent)
: tree_node(parent)
: psx_tree_node(parent)
{
printf("create tree node [%p] : size [%zd]\n", this, sizeof(test_tree));
}
Expand Down Expand Up @@ -41,43 +41,43 @@ TEST(ExtTree, CreateAndDestroy)
}


static inline bool b_work(const tree_node* node, void * data)
static inline bool b_work(const psx_tree_node* node, void * data)
{
tree_node ** p = (tree_node**)data;
*p = (tree_node*)node;
psx_tree_node ** p = (psx_tree_node**)data;
*p = (psx_tree_node*)node;
return true;
}

static inline bool tree_work(const tree_node* node, void * data)
static inline bool tree_work(const psx_tree_node* node, void * data)
{
tree_node ** p = (tree_node**)data;
psx_tree_node ** p = (psx_tree_node**)data;
printf("node access : %p === %p\n", *p, node);
if (node == *p) {
return true;
}
return false;
}

static inline bool a_work(const tree_node* node, void * data)
static inline bool a_work(const psx_tree_node* node, void * data)
{
tree_node ** p = (tree_node**)data;
psx_tree_node ** p = (psx_tree_node**)data;
*p = nullptr;
return true;
}

static inline bool b_work2(const tree_node* node, void * data)
static inline bool b_work2(const psx_tree_node* node, void * data)
{
return true;
}

static inline bool tree_work2(const tree_node* node, void * data)
static inline bool tree_work2(const psx_tree_node* node, void * data)
{
uint32_t * p = (uint32_t*)data;
(*p)++;
return true;
}

static inline bool a_work2(const tree_node* node, void * data)
static inline bool a_work2(const psx_tree_node* node, void * data)
{
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions unit_tests/pod_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ struct data_test
TEST(Pod_Vector, CreateAndDestroy)
{
pod_vector<unsigned int> iv;

pod_vector<unsigned int> sv;
pod_vector<unsigned int> sv = iv;

EXPECT_EQ(0, (int)iv.size());
EXPECT_EQ(0, (int)iv.capacity());
Expand All @@ -36,7 +35,7 @@ TEST(Pod_Vector, CreateAndDestroy)
TEST(Pod_Vector, PushAndInsert)
{
pod_vector<unsigned int> iv;
pod_vector<unsigned int> sv;
pod_vector<unsigned int> sv = iv;

iv.resize(3);
sv.resize(5);
Expand Down

0 comments on commit 8808081

Please sign in to comment.