From 7d8fffd0b3a08ed380ec647ee842e2a2d6ce5b5b Mon Sep 17 00:00:00 2001 From: LeviXubbbb Date: Thu, 27 Jul 2023 16:03:53 +0800 Subject: [PATCH] 2023_open_source_contest_warmup_1st_issue3 finish --- APP_Framework/Applications/app_test/Kconfig | 4 +- APP_Framework/Applications/app_test/Makefile | 4 + .../app_test/test_radix_tree/README.md | 67 ++++++++++++++ .../test_radix_tree/test_radix_tree.c | 63 +++++++++++++ .../test_radix_tree/test_radix_tree.h | 90 +++++++++++++++++++ 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 APP_Framework/Applications/app_test/test_radix_tree/README.md create mode 100644 APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.c create mode 100644 APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.h diff --git a/APP_Framework/Applications/app_test/Kconfig b/APP_Framework/Applications/app_test/Kconfig index 24ac99892..f7cb6ecad 100644 --- a/APP_Framework/Applications/app_test/Kconfig +++ b/APP_Framework/Applications/app_test/Kconfig @@ -247,6 +247,8 @@ menu "test app" menuconfig USER_TEST_RBTREE bool "Config test rbtree" default n - + menuconfig USER_TEST_RADIX_TREE + bool "Config test radix tree" + default n endif endmenu diff --git a/APP_Framework/Applications/app_test/Makefile b/APP_Framework/Applications/app_test/Makefile index 008345619..3ce7334b5 100644 --- a/APP_Framework/Applications/app_test/Makefile +++ b/APP_Framework/Applications/app_test/Makefile @@ -109,5 +109,9 @@ ifeq ($(CONFIG_ADD_XIZI_FETURES),y) SRC_FILES += test_rbtree/test_rbtree.c endif + ifeq ($(CONFIG_USER_TEST_RADIX_TREE),y) + SRC_FILES += test_radix_tree/test_radix_tree.c + endif + include $(KERNEL_ROOT)/compiler.mk endif diff --git a/APP_Framework/Applications/app_test/test_radix_tree/README.md b/APP_Framework/Applications/app_test/test_radix_tree/README.md new file mode 100644 index 000000000..b85ff00d1 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_radix_tree/README.md @@ -0,0 +1,67 @@ +# **基于k210-emulator实现基数树并测试验证** + +## 1. 简介 + +基于矽璓模拟器k210-emulator,实现基数树,并编写测试程序在shell终端打印结果 + +## 2. 数据结构设计说明 + +```c +struct RadixTreeNode{ + char key; + int value; + RadixTreeNode* children[26]; +}; +``` + +每个节点存储一个字符串中的一个字母,同时存储他的后一个字符的26种可能情况(在这里仅考虑小写英文字母) + +## 3. 测试程序说明 + +```c +void TestRadixTree(void) +{ + RadixTreeNode* root = (RadixTreeNode*)malloc(sizeof(RadixTreeNode)); + root->key = 0; + root->value = 0; + for(int i = 0; i < 26; i++) + root->children[i] = NULL; + printf("创建基数树并且插入以下结点\n"); + printf("key: apple, value: 1\n"); + printf("key: banana, value: 2\n"); + printf("key: orange, value: 3\n"); + printf("key: pear, value: 4\n"); + printf("key: peach, value: 5\n"); + printf("key: xiuos, value: 6\n"); + RadixTreeInsert(root, "apple", 1); + RadixTreeInsert(root, "banana", 2); + RadixTreeInsert(root, "orange", 3); + RadixTreeInsert(root, "pear", 4); + RadixTreeInsert(root, "peach", 5); + RadixTreeInsert(root, "xiuos", 6); + printf("插入创建完毕后查询基数树结果:\n"); + printf("若输入的内容是某些key的前缀则会返回value 0, 若输入的内容完全不匹配则返回value -1, 若输入的内容是某个key则会返回对应的value\n"); + printf("Value of key \"apple\" is %d\n", RadixTreeSearch(root, "apple")); + printf("Value of key \"banana\" is %d\n", RadixTreeSearch(root, "banana")); + printf("Value of key \"orange\" is %d\n", RadixTreeSearch(root, "orange")); + printf("Value of key \"pear\" is %d\n", RadixTreeSearch(root, "pear")); + printf("Value of key \"peach\" is %d\n", RadixTreeSearch(root, "peach")); + printf("Value of key \"xiuos\" is %d\n", RadixTreeSearch(root, "xiuos")); + printf("Value of key \"app\" is %d\n", RadixTreeSearch(root, "app")); + printf("Value of key \"xiuoss\" is %d\n", RadixTreeSearch(root, "xiuoss")); + printf("测试结束, 清空基数树\n"); + RadixTreeClear(root); +} +``` + +创建一个基数树并插入一些字符串作为key,以及它的相对应的value值(这里仅考虑value为int)。然后进行查找。若查找的key是某些key的前缀,则返回0,若查找的key不存在且不是某些key的前缀则返回-1. + +## 4. 运行结果(##需结合运行测试截图按步骤说明##) + +首先启动k210-emulator + +![2023_open_source_contest_warmup_1st_issue3_1](https://xubbbb-chartbed.oss-cn-shanghai.aliyuncs.com/img/markdown/2023_open_source_contest_warmup_1st_issue3_1.png) + +然后运行TestRadixTree指令进行测试 + +![2023_open_source_contest_warmup_1st_issue3_2](https://xubbbb-chartbed.oss-cn-shanghai.aliyuncs.com/img/markdown/2023_open_source_contest_warmup_1st_issue3_2.png) diff --git a/APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.c b/APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.c new file mode 100644 index 000000000..c77f2fb90 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.c @@ -0,0 +1,63 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: test_radix_tree.c +* @brief: a application to test radix tree and print result +* @version: 1.1 +* @author: LeviXubbbb(Bin Xu) +* @date: 2023/07/26 +*/ + +#include +#include +#include +#include +#include "test_radix_tree.h" +#ifdef ADD_XIZI_FETURES + +void TestRadixTree(void) +{ + RadixTreeNode* root = (RadixTreeNode*)malloc(sizeof(RadixTreeNode)); + root->key = 0; + root->value = 0; + for(int i = 0; i < 26; i++) + root->children[i] = NULL; + printf("创建基数树并且插入以下结点\n"); + printf("key: apple, value: 1\n"); + printf("key: banana, value: 2\n"); + printf("key: orange, value: 3\n"); + printf("key: pear, value: 4\n"); + printf("key: peach, value: 5\n"); + printf("key: xiuos, value: 6\n"); + RadixTreeInsert(root, "apple", 1); + RadixTreeInsert(root, "banana", 2); + RadixTreeInsert(root, "orange", 3); + RadixTreeInsert(root, "pear", 4); + RadixTreeInsert(root, "peach", 5); + RadixTreeInsert(root, "xiuos", 6); + printf("插入创建完毕后查询基数树结果:\n"); + printf("若输入的内容是某些key的前缀则会返回value 0, 若输入的内容完全不匹配则返回value -1, 若输入的内容是某个key则会返回对应的value\n"); + printf("Value of key \"apple\" is %d\n", RadixTreeSearch(root, "apple")); + printf("Value of key \"banana\" is %d\n", RadixTreeSearch(root, "banana")); + printf("Value of key \"orange\" is %d\n", RadixTreeSearch(root, "orange")); + printf("Value of key \"pear\" is %d\n", RadixTreeSearch(root, "pear")); + printf("Value of key \"peach\" is %d\n", RadixTreeSearch(root, "peach")); + printf("Value of key \"xiuos\" is %d\n", RadixTreeSearch(root, "xiuos")); + printf("Value of key \"app\" is %d\n", RadixTreeSearch(root, "app")); + printf("Value of key \"xiuoss\" is %d\n", RadixTreeSearch(root, "xiuoss")); + printf("测试结束, 清空基数树\n"); + RadixTreeClear(root); +} + +PRIV_SHELL_CMD_FUNCTION(TestRadixTree, a radix tree test sample, PRIV_SHELL_CMD_MAIN_ATTR); +#endif \ No newline at end of file diff --git a/APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.h b/APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.h new file mode 100644 index 000000000..a24863fa1 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_radix_tree/test_radix_tree.h @@ -0,0 +1,90 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: test_radix_tree.h +* @brief: a header file to implement radix tree +* @version: 1.1 +* @author: LeviXubbbb(Bin Xu) +* @date: 2023/07/26 +*/ + +#ifndef XIUOS_TEST_RADIX_TREE_H +#define XIUOS_TEST_RADIX_TREE_H + +#include +#include +#include + +typedef struct RadixTreeNode RadixTreeNode; + +struct RadixTreeNode{ + char key; + int value; + RadixTreeNode* children[26]; +}; + +static RadixTreeNode* createNode(char key, int value){ + RadixTreeNode* node = (RadixTreeNode*)malloc(sizeof(RadixTreeNode)); + node->key = key; + node->value = value; + for(int i = 0; i < 26; i++){ + node->children[i] = NULL; + } + return node; +} + +void RadixTreeInsert(RadixTreeNode* root, char* key, int value){ + RadixTreeNode* node = root; + int len = strlen(key); + for(int i = 0; i < len; i++){ + int index = key[i] - 'a'; + if(index < 0 || index > 25){ + printf("Invalid key! Please use lowercase letters!\n"); + return; + } + if(node->children[index] == NULL){ + node->children[index] = createNode(key[i], 0); + } + node = node->children[index]; + } + node->value = value; +} + +int RadixTreeSearch(RadixTreeNode* root, char* key){ + RadixTreeNode* node = root; + int len = strlen(key); + for(int i = 0; i < len; i++){ + int index = key[i] - 'a'; + if(index < 0 || index > 25){ + printf("Invalid key! Please use lowercase letters!\n"); + return -1; + } + if(node->children[index] == NULL){ + return -1; + } + node = node->children[index]; + } + return node->value; +} + +void RadixTreeClear(RadixTreeNode* root){ + if(root == NULL){ + return; + } + for(int i = 0; i < 26; i++){ + RadixTreeClear(root->children[i]); + } + free(root); +} + +#endif //XIUOS_TEST_RADIX_TREE_H \ No newline at end of file