From e04525e6f7802be30481477b272e39c55230db83 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 20 Sep 2026 10:41:37 +0200 Subject: initial import --- .clang-format | 45 +++++++++++++++++++ .gitignore | 2 + main.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 12 +++++ 4 files changed, 200 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 main.c create mode 100644 makefile diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b32bad6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,45 @@ +--- +AccessModifierOffset: -4 +AlignConsecutiveAssignments: 'true' +AlignConsecutiveDeclarations: 'true' +AlignEscapedNewlines: Left +AlignTrailingComments: 'true' +AlwaysBreakAfterReturnType: TopLevelDefinitions +BreakBeforeBraces: Stroustrup +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +ColumnLimit: '0' +CompactNamespaces: 'false' +Cpp11BracedListStyle: 'false' +FixNamespaceComments: 'true' +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^.*(precomp|pch|stdafx)' + Priority: -1 + - Regex: '^<.*>' + Priority: 1 + - Regex: '^".*"' + Priority: 2 + - Regex: '.*' + Priority: 3 +IndentCaseLabels: 'false' +IndentPPDirectives: AfterHash +IndentWidth: '4' +IndentWrappedFunctionNames: 'false' +KeepEmptyLinesAtTheStartOfBlocks: 'false' +PointerAlignment: Right +SortIncludes: 'true' +SpaceAfterCStyleCast: 'true' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'false' +SpaceInEmptyParentheses: 'false' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInConditionalStatement: 'true' +SpacesInParentheses: 'false' +Standard: Auto +TabWidth: '4' +UseTab: ForIndentation +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d565bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +rbtest +*.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 @@ +// Standard C +#include +#include +#include + +// RB-Implementierung +#include + +// clang-format off +struct address { + char name[41]; + int age; + + RB_ENTRY(address) links; +}; +// clang-format on + +// type: struct address_tree { ... }; +RB_HEAD(address_tree, address); + +int +address_compare(const struct address *lhs, const struct address *rhs) +{ + int res; + + if ( (res = strcmp(lhs->name, rhs->name)) != 0 ) + return res; + + return (lhs->age < rhs->age) ? -1 : (lhs->age > rhs->age); +} + +void +address_init(struct address *address, const char *name, int age) +{ + strlcpy(address->name, name, sizeof(address->name)); + address->age = age; +} + +struct address * +address_new(const char *name, int age) +{ + struct address *address; + + if ( (address = malloc(sizeof *address)) != NULL ) { + address_init(address, name, age); + } + return address; +} + +// Prototypen +RB_PROTOTYPE_STATIC(address_tree, address, links, address_compare) + +// Implemtierungen +RB_GENERATE_STATIC(address_tree, address, links, address_compare) + +void +insert(struct address_tree *addresses, struct address *address) +{ + RB_INSERT(address_tree, addresses, address); +} + +void +iterate(struct address_tree *addresses) +{ + struct address *ptr; + for ( ptr = RB_MIN(address_tree, addresses); + ptr != NULL; + ptr = RB_NEXT(address_tree, addresses, ptr) ) { + printf("%s\n", ptr->name); + } +} + +void +iterate2(struct address_tree *addresses) +{ + struct address *ptr; + RB_FOREACH(ptr, address_tree, addresses) + { + printf("%s\n", ptr->name); + } +} + +struct address * +search(struct address_tree *addresses, const char *name, int age) +{ + struct address key; + + address_init(&key, name, age); + + return RB_FIND(address_tree, addresses, &key); +} + +void +delete_one(struct address_tree *addresses, struct address *address) +{ + if ( address != NULL ) { + RB_REMOVE(address_tree, addresses, address); + free(address); + } +} + +void +delete_all(struct address_tree *addresses) +{ + struct address *ptr; + while ( (ptr = RB_MIN(address_tree, addresses)) != NULL ) { + RB_REMOVE(address_tree, addresses, ptr); + free(ptr); + } +} + +int +main(void) +{ + struct address_tree addresses = RB_INITIALIZER(&addresses); + + // struct address_tree addresses; + // RB_INIT(&addresses); + + insert(&addresses, address_new("Thomas", 44)); + insert(&addresses, address_new("Markus", 44)); + insert(&addresses, address_new("Miriam", 44)); + insert(&addresses, address_new("Albert", 44)); + + iterate(&addresses); + + struct address *ptr_miriam = search(&addresses, "Miriam", 44); + + if ( ptr_miriam != NULL ) + printf("Gefunden: %s (%d)\n", ptr_miriam->name, ptr_miriam->age); + else + printf("Nicht gefunden\n"); + + delete_one(&addresses, ptr_miriam); + + iterate2(&addresses); + + delete_all(&addresses); + + return EXIT_SUCCESS; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..a895dfe --- /dev/null +++ b/makefile @@ -0,0 +1,12 @@ +all: rbtest + +CFLAGS = -Wall -Werror -pedantic -std=c99 -O2 + +rbtest: main.o + cc $^ -o $@ + +%.o: %.c + cc $(CFLAGS) -c $< -o $@ + +clean: + rm -f rbtest *.o -- cgit v1.3