-
Notifications
You must be signed in to change notification settings - Fork 11
BDZ2_POPITKA_NOMER_3_NU_SEICHAS_TOCHNO_POLUCHITSYA #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
noziTuB
wants to merge
2
commits into
SergeyBalabaev:main
Choose a base branch
from
noziTuB:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,15 @@ | ||
| CC=gcc | ||
| CFLAGS=-Iinclude -Wall -g | ||
| LIB=build/librbmap.so | ||
|
|
||
| all: $(LIB) | ||
|
|
||
| $(LIB): src/rbmap.c | ||
| $(CC) $(CFLAGS) -fPIC -shared -o $(LIB) src/rbmap.c | ||
|
|
||
| test: $(LIB) | ||
| $(CC) -o build/test tests/test_rbmap.c tests/unity.c -Iinclude -Lbuild -lrbmap | ||
| LD_LIBRARY_PATH=build ./build/test | ||
|
|
||
| clean: | ||
| rm -rf build/* |
Binary file not shown.
Binary file not shown.
This file contains hidden or 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,23 @@ | ||
| #ifndef RBMAP_H | ||
| #define RBMAP_H | ||
|
|
||
| typedef enum { RED, BLACK } Color; | ||
|
|
||
| typedef struct RBNode { | ||
| void *key; | ||
| void *value; | ||
| Color color; | ||
| struct RBNode *left, *right, *parent; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Определение в ряд лучше не делать. Сделай их отдельными |
||
| } RBNode; | ||
|
|
||
| typedef struct { | ||
| RBNode *root; | ||
| int (*cmp)(void *, void *); | ||
| } RBMap; | ||
|
|
||
| RBMap *rbmap_create(int (*cmp)(const void *, const void *)); | ||
| void rbmap_insert(RBMap *map, void *key, void *value); | ||
| void *rbmap_get(RBMap *map, void *key); | ||
| void rbmap_destroy(RBMap *map, void (*free_func)(void *, void *)); | ||
|
|
||
| #endif // RBMAP_H | ||
This file contains hidden or 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,129 @@ | ||
| #include "rbmap.h" | ||
| #include <stdio.h> | ||
|
|
||
|
|
||
| static RBNode *create_node(void *key, void *value, Color color) { | ||
| RBNode *node = malloc(sizeof(RBNode)); | ||
| node->key = key; | ||
| node->value = value; | ||
| node->color = color; | ||
| node->left = node->right = node->parent = NULL; | ||
| return node; | ||
| } | ||
|
|
||
| static void left_rotate(RBMap *map, RBNode *x) { | ||
| RBNode *y = x->right; | ||
| x->right = y->left; | ||
| if (y->left) y->left->parent = x; | ||
| y->parent = x->parent; | ||
| if (!x->parent) map->root = y; | ||
| else if (x == x->parent->left) x->parent->left = y; | ||
| else x->parent->right = y; | ||
| y->left = x; | ||
| x->parent = y; | ||
| } | ||
|
|
||
| static void right_rotate(RBMap *map, RBNode *y) { | ||
| RBNode *x = y->left; | ||
| y->left = x->right; | ||
| if (x->right) x->right->parent = y; | ||
| x->parent = y->parent; | ||
| if (!y->parent) map->root = x; | ||
| else if (y == y->parent->right) y->parent->right = x; | ||
| else y->parent->left = x; | ||
| x->right = y; | ||
| y->parent = x; | ||
| } | ||
|
|
||
| static void insert_fixup(RBMap *map, RBNode *z) { | ||
| while (z->parent && z->parent->color == RED) { | ||
| if (z->parent == z->parent->parent->left) { | ||
| RBNode *y = z->parent->parent->right; | ||
| if (y && y->color == RED) { | ||
| z->parent->color = BLACK; | ||
| y->color = BLACK; | ||
| z->parent->parent->color = RED; | ||
| z = z->parent->parent; | ||
| } else { | ||
| if (z == z->parent->right) { | ||
| z = z->parent; | ||
| left_rotate(map, z); | ||
| } | ||
| z->parent->color = BLACK; | ||
| z->parent->parent->color = RED; | ||
| right_rotate(map, z->parent->parent); | ||
| } | ||
| } else { | ||
| RBNode *y = z->parent->parent->left; | ||
| if (y && y->color == RED) { | ||
| z->parent->color = BLACK; | ||
| y->color = BLACK; | ||
| z->parent->parent->color = RED; | ||
| z = z->parent->parent; | ||
| } else { | ||
| if (z == z->parent->left) { | ||
| z = z->parent; | ||
| right_rotate(map, z); | ||
| } | ||
| z->parent->color = BLACK; | ||
| z->parent->parent->color = RED; | ||
| left_rotate(map, z->parent->parent); | ||
| } | ||
| } | ||
| } | ||
| map->root->color = BLACK; | ||
| } | ||
|
|
||
| void rbmap_insert(RBMap *map, void *key, void *value) { | ||
| RBNode *z = create_node(key, value, RED); | ||
| RBNode *y = NULL; | ||
| RBNode *x = map->root; | ||
|
|
||
| while (x) { | ||
| y = x; | ||
| int cmp = map->cmp(key, x->key); | ||
| if (cmp == 0) { | ||
| x->value = value; // заменить значение | ||
| free(z); | ||
| return; | ||
| } | ||
| x = (cmp < 0) ? x->left : x->right; | ||
| } | ||
|
|
||
| z->parent = y; | ||
| if (!y) map->root = z; | ||
| else if (map->cmp(key, y->key) < 0) y->left = z; | ||
| else y->right = z; | ||
|
|
||
| insert_fixup(map, z); | ||
| } | ||
|
|
||
| void *rbmap_get(RBMap *map, void *key) { | ||
| RBNode *current = map->root; | ||
| while (current) { | ||
| int cmp = map->cmp(key, current->key); | ||
| if (cmp == 0) return current->value; | ||
| current = (cmp < 0) ? current->left : current->right; | ||
| } | ||
| return NULL; // ключ не найден | ||
| } | ||
|
|
||
| static void destroy_node(RBNode *node, void (*free_func)(void *, void *)) { | ||
| if (!node) return; | ||
| destroy_node(node->left, free_func); | ||
| destroy_node(node->right, free_func); | ||
| if (free_func) free_func(node->key, node->value); | ||
| free(node); | ||
| } | ||
|
|
||
| void rbmap_destroy(RBMap *map, void (*free_func)(void *, void *)) { | ||
| destroy_node(map->root, free_func); | ||
| free(map); | ||
| } | ||
|
|
||
| RBMap *rbmap_create(int (*cmp)(const void *, const void *)) { | ||
| RBMap *map = malloc(sizeof(RBMap)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. проверка на |
||
| map->root = NULL; | ||
| map->cmp = cmp; | ||
| return map; | ||
| } | ||
This file contains hidden or 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,49 @@ | ||
| #include "unity.h" | ||
| #include "rbmap.h" | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| int int_cmp(const void *a, const void *b) { | ||
| return (*(int *)a - *(int *)b); | ||
| } | ||
|
|
||
| void setUp(void) {} | ||
| void tearDown(void) {} | ||
|
|
||
| void test_create_map(void) { | ||
| RBMap *map = rbmap_create(int_cmp); | ||
| TEST_ASSERT_NOT_NULL(map); | ||
| rbmap_destroy(map, free); | ||
| } | ||
|
|
||
| void test_insert_and_get(void) { | ||
| RBMap *map = rbmap_create(int_cmp); | ||
| int *key = malloc(sizeof(int)); | ||
| int *val = malloc(sizeof(int)); | ||
| *key = 42; | ||
| *val = 100; | ||
| rbmap_insert(map, key, val); | ||
|
|
||
| int query = 42; | ||
| int *found = (int *)rbmap_get(map, &query); | ||
| TEST_ASSERT_NOT_NULL(found); | ||
| TEST_ASSERT_EQUAL_INT(100, *found); | ||
|
|
||
| rbmap_destroy(map, free); | ||
| } | ||
|
|
||
| void test_not_found(void) { | ||
| RBMap *map = rbmap_create(int_cmp); | ||
| int key = 123; | ||
| void *res = rbmap_get(map, &key); | ||
| TEST_ASSERT_NULL(res); | ||
| rbmap_destroy(map, free); | ||
| } | ||
|
|
||
| int main(void) { | ||
| UNITY_BEGIN(); | ||
| RUN_TEST(test_create_map); | ||
| RUN_TEST(test_insert_and_get); | ||
| RUN_TEST(test_not_found); | ||
| return UNITY_END(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не надо собранные файлы заливать на гит 👎