Skip to content

Commit d8e8811

Browse files
Merge pull request #74 from dreamer-coding/main
Add binary tree structure
2 parents 8174c39 + 485abf9 commit d8e8811

File tree

9 files changed

+640
-30
lines changed

9 files changed

+640
-30
lines changed

code/logic/fossil/tofu/framework.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "vector.h"
3333
#include "tuple.h"
3434
#include "array.h"
35-
// TODO: Add matrix implementation when available
3635
#include "arraylist.h"
3736

3837
// queue family
@@ -47,22 +46,13 @@
4746
// key-value family
4847
#include "setof.h"
4948
#include "mapof.h"
50-
// TODO: Add trie implementation when available
51-
// TODO: Add bloom filter implementation when available
5249

5350
// linked list family
5451
#include "clist.h"
5552
#include "flist.h"
5653
#include "dlist.h"
57-
//
58-
// TODO: Add cross-list implementation when available
59-
// TODO: Add skip-list implementation when available
60-
// TODO: Add xor-linked list implementation when available
61-
// TODO: Add intrusive list implementation when available
62-
// TODO: Add unrolled linked list implementation when available
63-
//
6454

6555
// tree family
66-
// TODO add tree family headers when implemented
56+
#include "tree.h"
6757

6858
#endif /* FOSSIL_TOFU_FRAMEWORK_H */

code/logic/fossil/tofu/tofu.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ char* fossil_tofu_serialize(const fossil_tofu_t *tofu);
311311
*/
312312
fossil_tofu_t* fossil_tofu_parse(const char *serialized);
313313

314+
/**
315+
* @brief Dumps the internal state of a tofu object for debugging.
316+
*
317+
* @param tofu The tofu object to dump.
318+
*/
319+
void fossil_tofu_dump(const fossil_tofu_t *tofu);
320+
314321
/**
315322
* @brief Gets the value as a string or returns a default if NULL.
316323
*/

code/logic/fossil/tofu/tree.h

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
/**
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop
6+
* high-performance, cross-platform applications and libraries. The code
7+
* contained herein is licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License. You may obtain
9+
* a copy of the License at:
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* Author: Michael Gene Brockus (Dreamer)
20+
* Date: 04/05/2014
21+
*
22+
* Copyright (C) 2014-2025 Fossil Logic. All rights reserved.
23+
* -----------------------------------------------------------------------------
24+
*/
25+
#ifndef FOSSIL_TOFU_TREE_H
26+
#define FOSSIL_TOFU_TREE_H
27+
28+
#include "tofu.h"
29+
30+
#ifdef __cplusplus
31+
extern "C"
32+
{
33+
#endif
34+
35+
// *****************************************************************************
36+
// Type Definitions
37+
// *****************************************************************************
38+
39+
typedef struct fossil_tree_node_t {
40+
fossil_tofu_t *value;
41+
struct fossil_tree_node_t *left;
42+
struct fossil_tree_node_t *right;
43+
} fossil_tree_node_t;
44+
45+
typedef struct {
46+
fossil_tree_node_t *root;
47+
char *type;
48+
size_t size;
49+
} fossil_tree_t;
50+
51+
/**
52+
* -----------------------------------------------------------------------------
53+
* Tree Management Functions
54+
* -----------------------------------------------------------------------------
55+
*/
56+
57+
/**
58+
* @brief Creates a new tree structure.
59+
*
60+
* @param type The type of values the tree will hold.
61+
*
62+
* @return Pointer to the newly created fossil_tree_t structure.
63+
*/
64+
fossil_tree_t* fossil_tree_create(char *type);
65+
66+
/**
67+
* @brief Destroys the specified tree and releases its resources.
68+
*
69+
* @param tree Pointer to the tree to be destroyed.
70+
*/
71+
void fossil_tree_destroy(fossil_tree_t *tree);
72+
73+
/**
74+
* -----------------------------------------------------------------------------
75+
* Node Operations
76+
* -----------------------------------------------------------------------------
77+
*/
78+
79+
/**
80+
* @brief Creates a new tree node with the given value.
81+
*
82+
* @param value Pointer to the value to store in the node.
83+
* @return Pointer to the newly created fossil_tree_node_t structure.
84+
*/
85+
fossil_tree_node_t* fossil_tree_create_node(fossil_tofu_t *value);
86+
87+
/**
88+
* @brief Inserts a value into the tree.
89+
*
90+
* @param tree Pointer to the tree where the value will be inserted.
91+
* @param value Pointer to the value to insert.
92+
* @return 0 on success, non-zero on failure.
93+
*/
94+
int fossil_tree_insert(fossil_tree_t *tree, fossil_tofu_t *value);
95+
96+
/**
97+
* @brief Searches for a node with the specified value in the tree.
98+
*
99+
* @param tree Pointer to the tree to search.
100+
* @param value Pointer to the value to search for.
101+
* @return Pointer to the found node, or NULL if not found.
102+
*/
103+
fossil_tree_node_t* fossil_tree_search(fossil_tree_t *tree, const fossil_tofu_t *value);
104+
105+
/**
106+
* -----------------------------------------------------------------------------
107+
* Traversal Functions
108+
* -----------------------------------------------------------------------------
109+
*/
110+
111+
/**
112+
* @brief Function pointer type for visiting tree nodes during traversal.
113+
*
114+
* @param value Pointer to the value in the node being visited.
115+
*/
116+
typedef void (*fossil_tree_visit_fn)(fossil_tofu_t *value);
117+
118+
/**
119+
* @brief Traverses the tree using the specified visit function.
120+
*
121+
* @param tree Pointer to the tree to traverse.
122+
* @param visit Function to call for each node's value.
123+
*/
124+
void fossil_tree_traverse(fossil_tree_t *tree, fossil_tree_visit_fn visit);
125+
126+
/**
127+
* @brief Performs an inorder traversal of the tree nodes.
128+
*
129+
* @param node Pointer to the current node.
130+
* @param visit Function to call for each node's value.
131+
*/
132+
void fossil_tree_traverse_inorder(fossil_tree_node_t *node, fossil_tree_visit_fn visit);
133+
134+
/**
135+
* @brief Performs a preorder traversal of the tree nodes.
136+
*
137+
* @param node Pointer to the current node.
138+
* @param visit Function to call for each node's value.
139+
*/
140+
void fossil_tree_traverse_preorder(fossil_tree_node_t *node, fossil_tree_visit_fn visit);
141+
142+
/**
143+
* @brief Performs a postorder traversal of the tree nodes.
144+
*
145+
* @param node Pointer to the current node.
146+
* @param visit Function to call for each node's value.
147+
*/
148+
void fossil_tree_traverse_postorder(fossil_tree_node_t *node, fossil_tree_visit_fn visit);
149+
150+
#ifdef __cplusplus
151+
}
152+
#include <stdexcept>
153+
#include <string>
154+
155+
namespace fossil {
156+
157+
namespace tofu {
158+
159+
/**
160+
* @class Tree
161+
* @brief A C++ wrapper class for managing a binary tree using the Fossil Logic library and Tofu objects.
162+
*/
163+
class Tree {
164+
public:
165+
/**
166+
* @brief Constructs a new Tree object.
167+
* @throws std::runtime_error If the tree cannot be created.
168+
*/
169+
explicit Tree(const std::string& type) {
170+
tree_ = fossil_tree_create(const_cast<char*>(type.c_str()));
171+
if (!tree_) {
172+
throw std::runtime_error("Failed to create tree.");
173+
}
174+
}
175+
176+
/**
177+
* @brief Destroys the Tree object and releases its resources.
178+
*/
179+
~Tree() {
180+
fossil_tree_destroy(tree_);
181+
}
182+
183+
/**
184+
* @brief Inserts a new Tofu value into the tree.
185+
* @param value The Tofu object to insert.
186+
* @throws std::runtime_error If the insertion fails.
187+
*/
188+
void insert(const Tofu& value) {
189+
fossil_tofu_t tofu_copy = value.get_c_struct();
190+
if (fossil_tree_insert(tree_, &tofu_copy) != 0) {
191+
throw std::runtime_error("Failed to insert value into tree.");
192+
}
193+
}
194+
195+
/**
196+
* @brief Searches for a node with the specified Tofu value in the tree.
197+
* @param value The Tofu object to search for.
198+
* @return Pointer to the found node, or nullptr if not found.
199+
*/
200+
fossil_tree_node_t* search(const Tofu& value) const {
201+
return fossil_tree_search(tree_, &value.get_c_struct());
202+
}
203+
204+
/**
205+
* @brief Gets the number of nodes in the tree.
206+
* @return The number of nodes in the tree.
207+
*/
208+
size_t size() const {
209+
return tree_->size;
210+
}
211+
212+
/**
213+
* @brief Checks if the tree is empty.
214+
* @return True if the tree is empty, false otherwise.
215+
*/
216+
bool is_empty() const {
217+
return tree_->size == 0;
218+
}
219+
220+
/**
221+
* @brief Traverses the tree using the specified visit function.
222+
* @param visit Function to call for each node's value.
223+
*/
224+
void traverse(fossil_tree_visit_fn visit) {
225+
fossil_tree_traverse(tree_, visit);
226+
}
227+
228+
/**
229+
* @brief Performs an inorder traversal of the tree nodes.
230+
* @param visit Function to call for each node's value.
231+
*/
232+
void traverse_inorder(fossil_tree_visit_fn visit) {
233+
fossil_tree_traverse_inorder(tree_->root, visit);
234+
}
235+
236+
/**
237+
* @brief Performs a preorder traversal of the tree nodes.
238+
* @param visit Function to call for each node's value.
239+
*/
240+
void traverse_preorder(fossil_tree_visit_fn visit) {
241+
fossil_tree_traverse_preorder(tree_->root, visit);
242+
}
243+
244+
/**
245+
* @brief Performs a postorder traversal of the tree nodes.
246+
* @param visit Function to call for each node's value.
247+
*/
248+
void traverse_postorder(fossil_tree_visit_fn visit) {
249+
fossil_tree_traverse_postorder(tree_->root, visit);
250+
}
251+
252+
private:
253+
fossil_tree_t* tree_; /**< Pointer to the underlying C tree structure. */
254+
};
255+
256+
} // namespace tofu
257+
258+
} // namespace fossil
259+
260+
#endif
261+
262+
#endif /* FOSSIL_TOFU_FRAMEWORK_H */

code/logic/meson.build

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
dir = include_directories('.')
2+
cc = meson.get_compiler('c')
23
add_project_arguments('-D_POSIX_C_SOURCE=200112L', language: 'c')
34
add_project_arguments('-D_POSIX_C_SOURCE=200112L', language: 'cpp')
45

56
fossil_tofu_lib = library('fossil_tofu',
6-
files('dlist.c',
7-
'flist.c',
8-
'clist.c',
9-
'dqueue.c',
10-
'pqueue.c',
11-
'queue.c',
12-
'cqueue.c',
13-
'setof.c',
14-
'tuple.c',
15-
'mapof.c',
16-
'stack.c',
17-
'vector.c',
18-
'array.c',
19-
'arraylist.c',
20-
'tofu.c'),
7+
files(
8+
'dlist.c',
9+
'flist.c',
10+
'clist.c',
11+
'dqueue.c',
12+
'pqueue.c',
13+
'queue.c',
14+
'cqueue.c',
15+
'setof.c',
16+
'tuple.c',
17+
'mapof.c',
18+
'stack.c',
19+
'vector.c',
20+
'array.c',
21+
'arraylist.c',
22+
'tree.c',
23+
'tofu.c'
24+
),
2125
install: true,
22-
include_directories: dir)
26+
include_directories: dir,
27+
dependencies: [cc.find_library('m', required: false)])
2328

2429
fossil_tofu_dep = declare_dependency(
2530
link_with: [fossil_tofu_lib],

code/logic/tofu.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,23 @@ fossil_tofu_t* fossil_tofu_parse(const char *fson_text) {
635635
return tofu_ptr;
636636
}
637637

638+
void fossil_tofu_dump(const fossil_tofu_t *tofu) {
639+
if (tofu == NULL) {
640+
printf("Tofu: cnull\n");
641+
return;
642+
}
643+
644+
printf("Tofu Dump:\n");
645+
printf(" Type: %s (%d)\n", fossil_tofu_type_name(tofu->type), tofu->type);
646+
printf(" Value: %s\n", tofu->value.data ? tofu->value.data : "(null)");
647+
printf(" Value Hash: 0x%016llx\n", (unsigned long long)tofu->value.hash);
648+
printf(" Mutable: %s\n", tofu->value.mutable_flag ? "true" : "false");
649+
printf(" Attribute Name: %s\n", tofu->attribute.name ? tofu->attribute.name : "(null)");
650+
printf(" Description: %s\n", tofu->attribute.description ? tofu->attribute.description : "(null)");
651+
printf(" ID: %s\n", tofu->attribute.id ? tofu->attribute.id : "(null)");
652+
printf(" Required: %s\n", tofu->attribute.required ? "true" : "false");
653+
}
654+
638655
const char* fossil_tofu_get_value_or_default(const fossil_tofu_t *tofu, const char *default_value) {
639656
if (!tofu || !tofu->value.data) return default_value;
640657
return tofu->value.data;

0 commit comments

Comments
 (0)