-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhangjipeng
committed
Jan 26, 2025
1 parent
09d9368
commit c1cd9d0
Showing
4 changed files
with
168 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |