diff options
| -rw-r--r-- | .clang-format | 45 | ||||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | main.c | 141 | ||||
| -rw-r--r-- | makefile | 12 |
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 | --- | ||
| 2 | AccessModifierOffset: -4 | ||
| 3 | AlignConsecutiveAssignments: 'true' | ||
| 4 | AlignConsecutiveDeclarations: 'true' | ||
| 5 | AlignEscapedNewlines: Left | ||
| 6 | AlignTrailingComments: 'true' | ||
| 7 | AlwaysBreakAfterReturnType: TopLevelDefinitions | ||
| 8 | BreakBeforeBraces: Stroustrup | ||
| 9 | BreakConstructorInitializers: BeforeComma | ||
| 10 | BreakInheritanceList: BeforeComma | ||
| 11 | ColumnLimit: '0' | ||
| 12 | CompactNamespaces: 'false' | ||
| 13 | Cpp11BracedListStyle: 'false' | ||
| 14 | FixNamespaceComments: 'true' | ||
| 15 | IncludeBlocks: Regroup | ||
| 16 | IncludeCategories: | ||
| 17 | - Regex: '^.*(precomp|pch|stdafx)' | ||
| 18 | Priority: -1 | ||
| 19 | - Regex: '^<.*>' | ||
| 20 | Priority: 1 | ||
| 21 | - Regex: '^".*"' | ||
| 22 | Priority: 2 | ||
| 23 | - Regex: '.*' | ||
| 24 | Priority: 3 | ||
| 25 | IndentCaseLabels: 'false' | ||
| 26 | IndentPPDirectives: AfterHash | ||
| 27 | IndentWidth: '4' | ||
| 28 | IndentWrappedFunctionNames: 'false' | ||
| 29 | KeepEmptyLinesAtTheStartOfBlocks: 'false' | ||
| 30 | PointerAlignment: Right | ||
| 31 | SortIncludes: 'true' | ||
| 32 | SpaceAfterCStyleCast: 'true' | ||
| 33 | SpaceAfterTemplateKeyword: 'false' | ||
| 34 | SpaceBeforeAssignmentOperators: 'true' | ||
| 35 | SpaceBeforeParens: ControlStatements | ||
| 36 | SpaceBeforeRangeBasedForLoopColon: 'false' | ||
| 37 | SpaceInEmptyParentheses: 'false' | ||
| 38 | SpacesInAngles: 'false' | ||
| 39 | SpacesInCStyleCastParentheses: 'false' | ||
| 40 | SpacesInConditionalStatement: 'true' | ||
| 41 | SpacesInParentheses: 'false' | ||
| 42 | Standard: Auto | ||
| 43 | TabWidth: '4' | ||
| 44 | UseTab: ForIndentation | ||
| 45 | ... | ||
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d565bc --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | rbtest | ||
| 2 | *.o | ||
| @@ -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 | ||
| 10 | struct 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 { ... }; | ||
| 19 | RB_HEAD(address_tree, address); | ||
| 20 | |||
| 21 | int | ||
| 22 | address_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 | |||
| 32 | void | ||
| 33 | address_init(struct address *address, const char *name, int age) | ||
| 34 | { | ||
| 35 | strlcpy(address->name, name, sizeof(address->name)); | ||
| 36 | address->age = age; | ||
| 37 | } | ||
| 38 | |||
| 39 | struct address * | ||
| 40 | address_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 | ||
| 51 | RB_PROTOTYPE_STATIC(address_tree, address, links, address_compare) | ||
| 52 | |||
| 53 | // Implemtierungen | ||
| 54 | RB_GENERATE_STATIC(address_tree, address, links, address_compare) | ||
| 55 | |||
| 56 | void | ||
| 57 | insert(struct address_tree *addresses, struct address *address) | ||
| 58 | { | ||
| 59 | RB_INSERT(address_tree, addresses, address); | ||
| 60 | } | ||
| 61 | |||
| 62 | void | ||
| 63 | iterate(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 | |||
| 73 | void | ||
| 74 | iterate2(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 | |||
| 83 | struct address * | ||
| 84 | search(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 | |||
| 93 | void | ||
| 94 | delete_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 | |||
| 102 | void | ||
| 103 | delete_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 | |||
| 112 | int | ||
| 113 | main(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 @@ | |||
| 1 | all: rbtest | ||
| 2 | |||
| 3 | CFLAGS = -Wall -Werror -pedantic -std=c99 -O2 | ||
| 4 | |||
| 5 | rbtest: main.o | ||
| 6 | cc $^ -o $@ | ||
| 7 | |||
| 8 | %.o: %.c | ||
| 9 | cc $(CFLAGS) -c $< -o $@ | ||
| 10 | |||
| 11 | clean: | ||
| 12 | rm -f rbtest *.o | ||
