summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2026-09-20 10:41:37 +0200
committerThomas Schmucker <ts@its1.de>2026-09-20 10:41:37 +0200
commite04525e6f7802be30481477b272e39c55230db83 (patch)
tree63ce1b5ab574efe599527be8c9c17a533eeeaaca
downloaduse-bsd-rbtree-master.tar.gz
use-bsd-rbtree-master.tar.bz2
use-bsd-rbtree-master.zip
initial importHEADmaster
-rw-r--r--.clang-format45
-rw-r--r--.gitignore2
-rw-r--r--main.c141
-rw-r--r--makefile12
4 files changed, 200 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..b32bad6
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,45 @@
1---
2AccessModifierOffset: -4
3AlignConsecutiveAssignments: 'true'
4AlignConsecutiveDeclarations: 'true'
5AlignEscapedNewlines: Left
6AlignTrailingComments: 'true'
7AlwaysBreakAfterReturnType: TopLevelDefinitions
8BreakBeforeBraces: Stroustrup
9BreakConstructorInitializers: BeforeComma
10BreakInheritanceList: BeforeComma
11ColumnLimit: '0'
12CompactNamespaces: 'false'
13Cpp11BracedListStyle: 'false'
14FixNamespaceComments: 'true'
15IncludeBlocks: Regroup
16IncludeCategories:
17 - Regex: '^.*(precomp|pch|stdafx)'
18 Priority: -1
19 - Regex: '^<.*>'
20 Priority: 1
21 - Regex: '^".*"'
22 Priority: 2
23 - Regex: '.*'
24 Priority: 3
25IndentCaseLabels: 'false'
26IndentPPDirectives: AfterHash
27IndentWidth: '4'
28IndentWrappedFunctionNames: 'false'
29KeepEmptyLinesAtTheStartOfBlocks: 'false'
30PointerAlignment: Right
31SortIncludes: 'true'
32SpaceAfterCStyleCast: 'true'
33SpaceAfterTemplateKeyword: 'false'
34SpaceBeforeAssignmentOperators: 'true'
35SpaceBeforeParens: ControlStatements
36SpaceBeforeRangeBasedForLoopColon: 'false'
37SpaceInEmptyParentheses: 'false'
38SpacesInAngles: 'false'
39SpacesInCStyleCastParentheses: 'false'
40SpacesInConditionalStatement: 'true'
41SpacesInParentheses: 'false'
42Standard: Auto
43TabWidth: '4'
44UseTab: ForIndentation
45...
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4d565bc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
1rbtest
2*.o
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..a176f89
--- /dev/null
+++ b/main.c
@@ -0,0 +1,141 @@
1// Standard C
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6// RB-Implementierung
7#include <sys/tree.h>
8
9// clang-format off
10struct address {
11 char name[41];
12 int age;
13
14 RB_ENTRY(address) links;
15};
16// clang-format on
17
18// type: struct address_tree { ... };
19RB_HEAD(address_tree, address);
20
21int
22address_compare(const struct address *lhs, const struct address *rhs)
23{
24 int res;
25
26 if ( (res = strcmp(lhs->name, rhs->name)) != 0 )
27 return res;
28
29 return (lhs->age < rhs->age) ? -1 : (lhs->age > rhs->age);
30}
31
32void
33address_init(struct address *address, const char *name, int age)
34{
35 strlcpy(address->name, name, sizeof(address->name));
36 address->age = age;
37}
38
39struct address *
40address_new(const char *name, int age)
41{
42 struct address *address;
43
44 if ( (address = malloc(sizeof *address)) != NULL ) {
45 address_init(address, name, age);
46 }
47 return address;
48}
49
50// Prototypen
51RB_PROTOTYPE_STATIC(address_tree, address, links, address_compare)
52
53// Implemtierungen
54RB_GENERATE_STATIC(address_tree, address, links, address_compare)
55
56void
57insert(struct address_tree *addresses, struct address *address)
58{
59 RB_INSERT(address_tree, addresses, address);
60}
61
62void
63iterate(struct address_tree *addresses)
64{
65 struct address *ptr;
66 for ( ptr = RB_MIN(address_tree, addresses);
67 ptr != NULL;
68 ptr = RB_NEXT(address_tree, addresses, ptr) ) {
69 printf("%s\n", ptr->name);
70 }
71}
72
73void
74iterate2(struct address_tree *addresses)
75{
76 struct address *ptr;
77 RB_FOREACH(ptr, address_tree, addresses)
78 {
79 printf("%s\n", ptr->name);
80 }
81}
82
83struct address *
84search(struct address_tree *addresses, const char *name, int age)
85{
86 struct address key;
87
88 address_init(&key, name, age);
89
90 return RB_FIND(address_tree, addresses, &key);
91}
92
93void
94delete_one(struct address_tree *addresses, struct address *address)
95{
96 if ( address != NULL ) {
97 RB_REMOVE(address_tree, addresses, address);
98 free(address);
99 }
100}
101
102void
103delete_all(struct address_tree *addresses)
104{
105 struct address *ptr;
106 while ( (ptr = RB_MIN(address_tree, addresses)) != NULL ) {
107 RB_REMOVE(address_tree, addresses, ptr);
108 free(ptr);
109 }
110}
111
112int
113main(void)
114{
115 struct address_tree addresses = RB_INITIALIZER(&addresses);
116
117 // struct address_tree addresses;
118 // RB_INIT(&addresses);
119
120 insert(&addresses, address_new("Thomas", 44));
121 insert(&addresses, address_new("Markus", 44));
122 insert(&addresses, address_new("Miriam", 44));
123 insert(&addresses, address_new("Albert", 44));
124
125 iterate(&addresses);
126
127 struct address *ptr_miriam = search(&addresses, "Miriam", 44);
128
129 if ( ptr_miriam != NULL )
130 printf("Gefunden: %s (%d)\n", ptr_miriam->name, ptr_miriam->age);
131 else
132 printf("Nicht gefunden\n");
133
134 delete_one(&addresses, ptr_miriam);
135
136 iterate2(&addresses);
137
138 delete_all(&addresses);
139
140 return EXIT_SUCCESS;
141}
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..a895dfe
--- /dev/null
+++ b/makefile
@@ -0,0 +1,12 @@
1all: rbtest
2
3CFLAGS = -Wall -Werror -pedantic -std=c99 -O2
4
5rbtest: main.o
6 cc $^ -o $@
7
8%.o: %.c
9 cc $(CFLAGS) -c $< -o $@
10
11clean:
12 rm -f rbtest *.o