diff --git a/APP_Framework/Applications/app_test/Kconfig b/APP_Framework/Applications/app_test/Kconfig index 3a103a6b3..2d3e927b9 100644 --- a/APP_Framework/Applications/app_test/Kconfig +++ b/APP_Framework/Applications/app_test/Kconfig @@ -240,5 +240,9 @@ menu "test app" bool "Config test soft timer" default n + menuconfig USER_TEST_HASHTABLE + bool "Config test hash" + default n + endif endmenu diff --git a/APP_Framework/Applications/app_test/Makefile b/APP_Framework/Applications/app_test/Makefile index 200e9e02a..78fb59993 100644 --- a/APP_Framework/Applications/app_test/Makefile +++ b/APP_Framework/Applications/app_test/Makefile @@ -101,5 +101,8 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y) SRC_FILES += test_timer.c endif + ifeq ($(CONFIG_USER_TEST_HASHTABLE),y) + SRC_DIR += test_hash + endif include $(KERNEL_ROOT)/compiler.mk endif diff --git a/APP_Framework/Applications/app_test/test_hash/Makefile b/APP_Framework/Applications/app_test/test_hash/Makefile new file mode 100755 index 000000000..80767743b --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/Makefile @@ -0,0 +1,12 @@ +ifeq ($(CONFIG_ADD_XIZI_FEATURES),y) +SRC_FILES := hash.c test_hash.c +include $(KERNEL_ROOT)/compiler.mk +endif + +include $(KERNEL_ROOT)/.config +ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y) + include $(APPDIR)/Make.defs + CSRCS += hash.c test_hash.c + include $(APPDIR)/Application.mk +endif + diff --git a/APP_Framework/Applications/app_test/test_hash/hash.c b/APP_Framework/Applications/app_test/test_hash/hash.c new file mode 100644 index 000000000..6f7ec4c63 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/hash.c @@ -0,0 +1,112 @@ +#include +#include +#include"hash.h" +// 通常是质数 + + +int Hash(int value) +{ + return value%REMAINDER; +} + +HashTableType* CreatTable(int table_size) +{ + HashTableType *h = (HashTableType *)malloc(sizeof(HashTableType)); + h->size = REMAINDER; + h->head = (Node *)malloc((h->size)*sizeof(Node)); + h->length = 0; + int i = 0; + for(i=0 ; isize ; i++) + { + h->head[i].next = NULL; + } + return h; +} + +Node *LookUp(HashTableType *h , int key) +{ + int i; + i = Hash(key); + Node * p = h->head[i].next; + while(p && key != p->data->key) + { + p = p->next; + } + + return p; +} + +void Insert(HashTableType *h , ElementType k) +{ + Node * p = LookUp(h,k.key); + if(!p) + { + Node *q = (Node *)malloc(sizeof(Node)); + q->data = (ElementType *)malloc(sizeof(ElementType)); + (q->data)->key = k.key; + (q->data)->value = k.value; + int position; + position = (q->data)->hash = Hash(k.key); + q->next = h->head[position].next; + h->head[position].next = q; + + h->length += 1; + return ; + } + else + { + printf("The keys is exist !\n"); + return ; + } +} + +void DestroyTable(HashTableType *h) +{ + int i; + Node *p , *q; + for(i=0 ; isize ; i++) + { + p = h->head[i].next; + while(p) + { + q=p->next; + free(p); + p=q; + } + } + free(h->head); + free(h); +} + +void PrintTable(HashTableType *h) +{ + int i = 0; + for (i = 0; i < h->size ; i++) + { + Node * p = h->head[i].next; + while (p) + { + printf("[%d-%d] ",p->data->key, p->data->value); + p = p->next; + } + printf("NULL\n"); + } +} + +void Test() +{ + ElementType a[]={{12,1},{2,2},{31,3},{45,4},{8,5}}; + + int n = sizeof(a)/sizeof(ElementType); + HashTableType *h = CreatTable(n); + int i = 0; + for(i = 0 ; idata->key); //查找key值为12的Element + printf("%d\n",h->length); //打印哈希表的元素个数 + DestroyTable(h); // 摧毁哈希表 +} + diff --git a/APP_Framework/Applications/app_test/test_hash/hash.h b/APP_Framework/Applications/app_test/test_hash/hash.h new file mode 100644 index 000000000..83785e9d7 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/hash.h @@ -0,0 +1,48 @@ +/* + * @Description: + * @Version: V1.0.0 + * @Author: 快乐小组 + * @Date: 2023-07-25 22:26:04 + * @LastEditors: pgh_dd 1041315949@qq.com + * @LastEditTime: 2023-07-25 22:39:54 + */ +#ifndef __HASH_H__ +#define __HASH_H__ +#include + +#define REMAINDER 11 +typedef struct ElementType +{ + int key; + int value; + int hash; +}ElementType; + +typedef struct Pnode +{ + ElementType *data; + struct Pnode *next; +}Node; + +typedef struct HashTable +{ + int size; + int length; + struct Pnode *head; +}HashTableType; + + +int Hash(int value); + +HashTableType* CreatTable(int table_size); + +Node *LookUp(HashTableType *h , int key); + +void Insert(HashTableType *h , ElementType k); + +void DestroyTable(HashTableType *h); + +void PrintTable(HashTableType *h); + +void Test(); +#endif \ No newline at end of file diff --git a/APP_Framework/Applications/app_test/test_hash/img/testHash.png b/APP_Framework/Applications/app_test/test_hash/img/testHash.png new file mode 100644 index 000000000..6f267c5cd Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/img/testHash.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/test_hash.c b/APP_Framework/Applications/app_test/test_hash/test_hash.c new file mode 100644 index 000000000..0ac24e240 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/test_hash.c @@ -0,0 +1,36 @@ +/* + * @Description: + * @Version: V1.0.0 + * @Author: 快乐小组 + * @Date: 2023-07-25 22:26:04 + * @LastEditors: pgh_dd 1041315949@qq.com + * @LastEditTime: 2023-07-25 22:40:28 + */ +/* +* 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_shell.c +* @brief: a application of shell function +* @version: 2.0 +* @author: AIIT XUOS Lab +* @date: 2022/9/26 +*/ +#include +#include "hash.h" + +void TestHash(void) +{ + Test(); +} + +PRIV_SHELL_CMD_FUNCTION(TestHash, a hash sample , PRIV_SHELL_CMD_MAIN_ATTR);