05-05 19:21
Recent Posts
Recent Comments
관리 메뉴

너와나의 관심사

hash 를 활용한 example code 본문

카테고리 없음

hash 를 활용한 example code

벤치마킹 2018. 7. 18. 01:14

hash 를 활용한 source code 

#include <stdio.h>

#include <iostream> //#define MAX_TABLE 20007 #define MAX_TABLE 10001 #define MAX_APP_NUM 100001 enum { REQUEST = 0, READ_ALL_NOTI, DELETE_NOTI, GET_UNREAD_NOTI, GET_APP_LOG_COUNT }; struct NOTILIST { int ids[10001]; int count; } lst; struct Appnoti { int notiId; char appName[13]; bool isDeleted; bool isRead; }; //struct Appnoti noti[100001]; struct App_info { int notiId; char appName[13]; bool isDeleted; bool isRead; }; struct App_info app_info[MAX_APP_NUM]; typedef struct Hash ListNode; struct Hash { char key[13]; int order; bool isRead; bool isDeleted; struct Hash *next; struct Hash *prev; }; struct Hash a[MAX_APP_NUM+1]; static int ac[MAX_TABLE]; struct Hash *appmgr[MAX_TABLE]; int app_id = -1; unsigned long hashf(const char *str) { unsigned long hash = 5381; int c; while (c = *str++) { hash = (((hash << 5) + hash) + c) % MAX_TABLE; } return hash % MAX_TABLE; } #if 1 int strcmp(const char *src, const char * dst) { int i =0; for( ; src[i] == dst[i]; i++) if(src[i] == '\0') break; return src[i] - dst[i]; } #else int strcmp(char *first, char* second) { int i = 0; for (; first[i] == second[i]; i++) { if (first[i] == '\0') break; } return first[i] - second[i]; } #endif ListNode* appendListNode(ListNode* list, ListNode* node) { if (list == 0) { return node; } else { ListNode* last = list->prev; last->next = node; list->prev = node; node->prev = last; node->next = list; return list; } } int startNum = 1; int endNum = 1; void init() { startNum = 1; endNum = 1; app_id = -1; for (int i = 0; i < 100001; i++) { app_info[i].appName[0] = '\0'; app_info[i].isRead = false; app_info[i].isDeleted = false; app_info[i].notiId = 0; a[i].next = 0; a[i].key[0] = '\0'; a[i].order = 0; a[i].isRead = false; a[i].isDeleted = false; a[i].next = &a[i]; a[i].prev = &a[i]; } for (int i = 0; i < MAX_TABLE; i++) { appmgr[i] = 0; ac[i] = 0; } } ListNode * init_arr(int order) { app_id++; // a[app_id].next = 0; a[app_id].key[0] = 0; a[app_id].order = order; a[app_id].isRead = false; a[app_id].isDeleted = false; a[app_id].next = &a[app_id]; a[app_id].prev = &a[app_id]; return &a[app_id]; } void insert(int hashVal, char *appname, int order) { struct Hash *h = init_arr(order); int i = 0; while (appname[i] != '\0'){ //for (int i = 0; i < 13; i++){ h->key[i] = appname[i]; i++; } h->key[i] = '\0'; appmgr[hashVal] = appendListNode(appmgr[hashVal], h); } void request(int notiID, char appName[], int order) { int i = 0; app_info[order].notiId = notiID; while (appName[i] != '\0'){ //for (int i = 0; i < 13; i++){ app_info[order].appName[i] = appName[i]; i++; } app_info[order].appName[i] = '\0'; int val = hashf(appName); insert(val, appName, order); endNum++; ac[val]++; } void readAllNoti(char appName[]) { int hashVal = hashf(appName); ListNode *h = appmgr[hashVal]; int i = 0; while (h && i < ac[hashVal]){ if (strcmp(h->key, appName) == 0) { h->isRead = true; app_info[h->order].isRead = true; } i++; h = h->next; } } void deleteNoti(int count) { for (int i = startNum; i < startNum + count; i++){ app_info[i].isDeleted = true; } startNum += count; } NOTILIST getUnreadNoti() { lst.count = 0; for (int i = 0; i < 10001; i++) lst.ids[i] = 0; int j = 0; for (int i = startNum; i < endNum; i++){ if (!app_info[i].isRead && !app_info[i].isDeleted){ lst.ids[j++] = app_info[i].notiId; lst.count++; } } return lst; } int getAppLogCount(char appName[]) { int val = hashf(appName); int cnt = 0, i = 0; ListNode *h = appmgr[val]; while (h && i < ac[val]) { if (strcmp(h->key, appName) == 0 && !app_info[h->order].isDeleted) //if (app_info[h->order].isDeleted) cnt++; i++; h = h->next; } return cnt; }


Comments