From 483934811110f7bf6554a29e3de3d8f6353885d4 Mon Sep 17 00:00:00 2001 From: pgh_dd <1041315949@qq.com> Date: Tue, 25 Jul 2023 23:01:31 +0800 Subject: [PATCH] =?UTF-8?q?"cortex-m3=E5=93=88=E5=B8=8C=E8=A1=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APP_Framework/Applications/app_test/Kconfig | 4 + APP_Framework/Applications/app_test/Makefile | 3 + .../Applications/app_test/test_hash/Makefile | 12 ++ .../Applications/app_test/test_hash/hash.c | 112 ++++++++++++++++++ .../Applications/app_test/test_hash/hash.h | 48 ++++++++ .../app_test/test_hash/img/testHash.png | Bin 0 -> 3607 bytes .../app_test/test_hash/test_hash.c | 36 ++++++ 7 files changed, 215 insertions(+) create mode 100755 APP_Framework/Applications/app_test/test_hash/Makefile create mode 100644 APP_Framework/Applications/app_test/test_hash/hash.c create mode 100644 APP_Framework/Applications/app_test/test_hash/hash.h create mode 100644 APP_Framework/Applications/app_test/test_hash/img/testHash.png create mode 100644 APP_Framework/Applications/app_test/test_hash/test_hash.c 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 0000000000000000000000000000000000000000..6f267c5cd03e2674d8ff4ff5bcb9055e1f3a7f20 GIT binary patch literal 3607 zcmds4XEYqz8YL2Cm#p_QTkHLPzxFx%oU_(BYn^ZJ{lyy_>Hq;e02&$^pq{Rl z$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ZFJ=B#sXW(AZrubLEq~0G8g^Xtl9QMW^m&GoY z57}e89L~Wc7tR9lYx_^x|GXhETPR#f0-pF12l@G)2``s9!2NgzsyGK;N;%q+8sQ~1>&~F|#i!~e1KI@r&nk5I8toBFcGM8?>h&Mnng5L}ZIcQPi=V*hn?Sp`rnM2ACtcH- zOk6VBiNohUb)LTT@W4$LTc!*0ov0J`I9le1Z$6vvrinm;I9R63wB)@6apceh--SI+ zNo0|5gSt*LxcR0#7xpzFbhR$}Eh0|j=aACZb16n`a{r1#WoL9p;$kqpMemjKhL1+? MuAx?~hGWz}0Mv=(>i_@% literal 0 HcmV?d00001 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);