Skip to content

Commit

Permalink
add tree base implement for ext
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjipeng committed Jan 26, 2025
1 parent 09d9368 commit c1cd9d0
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 15 deletions.
18 changes: 14 additions & 4 deletions ext/common/psx_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@

#include "psx_common.h"

struct list_hdr {
#ifdef __cplusplus
extern "C" {
#endif

/**
* Doubly linked list implementation.
* Must be the first field of a data struct
*/
typedef struct list_hdr {
struct list_hdr* next;
struct list_hdr* prev;
};

/* all function return 0 is success or true, -1 is fail or false. */
} list_hdr_t;

static INLINE void list_init(struct list_hdr* head)
{
Expand Down Expand Up @@ -89,4 +95,8 @@ static INLINE void list_remove_entry(struct list_hdr* entry)
#define list_for_each_start_with(head, start, iterator) \
for ((iterator) = ((struct list_hdr*)(start))->next; (iterator) != (head); (iterator) = (iterator)->next)

#ifdef __cplusplus
}
#endif

#endif /*_PSX_LIST_H_*/
89 changes: 89 additions & 0 deletions ext/common/psx_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2024, Zhang Ji Peng
* All rights reserved.
*
* 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 copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 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 HOLDER 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 _PSX_TREE_H_
#define _PSX_TREE_H_

#include "psx_common.h"

#define INIT_CAPACITY 4

class tree_node
{
public:
tree_node(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*));
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[m_parent->m_count - 1] = this;
m_index = m_parent->m_count;
}
}

virtual ~tree_node()
{
if (m_parent) {
if (m_parent->m_children[m_index - 1] == this) {
m_parent->m_children[m_index - 1] = NULL;
}
}
for (uint32_t i = 0; i < m_count; i++) {
if (m_children[i]) {
delete m_children[i];
}
}
free(m_children);
}

uint32_t child_count(void) const { return m_count; }

tree_node* get_child(uint32_t idx) const
{
if (idx >= m_count) {
return NULL;
}
return m_children[idx];
}

private:
tree_node* m_parent;
tree_node** m_children;
uint32_t m_count;
uint32_t m_capacity;
uint32_t m_index;
};

#endif /*_PSX_TREE_H_*/
35 changes: 24 additions & 11 deletions ext/image_coders/psx_color_convert.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
/* Picasso - a vector graphics library
/*
* Copyright (c) 2024, Zhang Ji Peng
* All rights reserved.
*
* Copyright (C) 2016 Zhang Ji Peng
* Contact: [email protected]
* 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 copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 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 HOLDER 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 _PSX_COLOR_CONVERT_H_
#define _PSX_COLOR_CONVERT_H_

#include "psx_common.h"
#include <stdlib.h>

#if defined(__GNUC__)
#define INLINE inline
#elif defined(_MSC_VER)
#define INLINE __inline
#else
#define INLINE
#endif

/* 32 bit color convert */
static INLINE void _argb_to_rgba(uint8_t* dst, const uint8_t* src, size_t len)
{
Expand Down
41 changes: 41 additions & 0 deletions unit_tests/ext_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "test.h"
#include "timeuse.h"

#include "psx_tree.h"

class test_tree : public tree_node {
public:
test_tree(test_tree * parent)
: tree_node(parent)
{
printf("create tree node [%p]\n", this);
}

virtual ~test_tree()
{
printf("delete tree node [%p]\n", this);
}
};

TEST(ExtTree, CreateAndDestroy)
{
test_tree * p1 = new test_tree(0);
test_tree * p2 = new test_tree(p1);
test_tree * p3 = new test_tree(p1);
test_tree * p4 = new test_tree(p2);

EXPECT_EQ(p1->child_count(), 2);
EXPECT_EQ(p2->child_count(), 1);

EXPECT_EQ(p1->get_child(1), p3);
EXPECT_EQ(p2->get_child(0), p4);

delete p3;

// p3 remove from parent
EXPECT_EQ(p1->get_child(1), nullptr);
EXPECT_EQ(p1->child_count(), 2);

delete p2;
delete p1;
}

0 comments on commit c1cd9d0

Please sign in to comment.