Untitled
unknown
plain_text
2 years ago
7.9 kB
5
Indexable
#define MAX_POST 50001 #define MAX_USER 10001 #define MAX_HASH1 10007 #define MAX_HASH2 50021 #define MAX_L 11 //String utils: void strCpy(char *t, char *s) { while (*t++ = *s++) ; } long long hash(char* name) { long long ret = 0; for (register int i = 0; name[i]; i++) ret += (long long)(name[i] - 96) << (5 * (9 - i)); return ret; } struct BasicInfo {//Common info of all objects long long id; //for users, info id is hash value of their name; for messages, comments, replies, info id is their id. int totalPoint; int objectIdx; //index of objects in pool arrays BasicInfo* next; //For hash map int heapIdx; //For heap } infos[MAX_POST + MAX_USER]; int objectCnt; struct User { char name[MAX_L]; BasicInfo* info; } users[MAX_USER]; int userNum; struct Post { //A post can be message, comment or reply int point; BasicInfo* info; Post* parent; User* user; //For linked list Post* next; Post* firstChild; } posts[MAX_POST]; int postNum; //Heaps BasicInfo* userInfoHeap[MAX_USER]; int userHeapSize; BasicInfo* postInfoHeap[MAX_POST]; int postHeapSize; bool isBetter(BasicInfo* info1, BasicInfo* info2) { return info1->totalPoint > info2->totalPoint || (info1->totalPoint == info2->totalPoint && info1->id < info2->id); } void upHeapify(BasicInfo* heap[], int pos) { register BasicInfo* curr = heap[pos]; for( ; pos > 1 && isBetter(curr, heap[pos >> 1]); pos >>= 1) { heap[pos] = heap[pos >> 1]; heap[pos]->heapIdx = pos; } heap[pos] = curr; heap[pos]->heapIdx = pos; } void downHeapify(BasicInfo* heap[], int size, int pos) { register BasicInfo* curr = heap[pos]; register int child; while ((child = pos << 1) <= size) { if (child < size && isBetter(heap[child + 1], heap[child])) child++; if (isBetter(heap[child], curr)) { heap[pos] = heap[child]; heap[pos]->heapIdx = pos; pos = child; } else break; } heap[pos] = curr; heap[pos]->heapIdx = pos; } void heapPush(BasicInfo* heap[], int &size, BasicInfo* info) { heap[++size] = info; upHeapify(heap, size); } void heapUpdate(BasicInfo* heap[], int size, BasicInfo* info) { register int pos = info->heapIdx; upHeapify(heap, pos); downHeapify(heap, size, pos); } void heapRemove(BasicInfo* heap[], int &size, BasicInfo* info) { register int pos = info->heapIdx; heap[pos] = heap[size--]; heap[pos]->heapIdx = pos; heapUpdate(heap, size, heap[pos]); } BasicInfo* heapPop(BasicInfo* heap[], int &size) { BasicInfo* ret = heap[1]; heapRemove(heap, size, heap[1]); return ret; } //HashMaps BasicInfo* userInfoMap[MAX_HASH1]; BasicInfo* postInfoMap[MAX_HASH2]; Post* findPost(int postId) { for (BasicInfo* info = postInfoMap[postId % MAX_HASH2]; info; info = info->next) if (postId == info->id) return &posts[info->objectIdx]; return nullptr; } User* findUser(char *name) { long long hashVal = hash(name); for (BasicInfo* info = userInfoMap[hashVal % MAX_HASH1]; info; info = info->next) if (hashVal == info->id) return &users[info->objectIdx]; //If user not exist, create new user and user info BasicInfo* info = &infos[objectCnt++]; info->id = hashVal; info->objectIdx = userNum; info->totalPoint = 0; //Add to hash map register int idx = hashVal % MAX_HASH1; info->next = userInfoMap[idx]; userInfoMap[idx] = info; //Add to end of heap, will be updated later userInfoHeap[++userHeapSize] = info; info->heapIdx = userHeapSize; //Create new user strCpy(users[userNum].name, name); users[userNum].info = info; return &users[userNum++]; } int addNewPost(User* user, int mID, int mPoint, Post* mParent) { user->info->totalPoint += mPoint; heapUpdate(userInfoHeap, userHeapSize, user->info); //Create new post info and add to hash map BasicInfo* postInfo = &infos[objectCnt++]; postInfo->id = mID; postInfo->totalPoint = mPoint; postInfo->objectIdx = postNum; register int idx = mID % MAX_HASH2; postInfo->next = postInfoMap[idx]; postInfoMap[idx] = postInfo; //Create new post Post* post = &posts[postNum++]; post->info = postInfo; post->point = mPoint; post->user = user; post->firstChild = nullptr; post->parent = mParent; if (mParent) { //this post is a comment or reply post->next = mParent->firstChild; mParent->firstChild = post; mParent->info->totalPoint += mPoint; if (mParent->parent) { //this is a reply mParent = mParent->parent; mParent->info->totalPoint += mPoint; } heapUpdate(postInfoHeap, postHeapSize, mParent->info); return mParent->info->totalPoint; } //this post is a message heapPush(postInfoHeap, postHeapSize, postInfo); post->next = nullptr; return user->info->totalPoint; } void init() { objectCnt = userNum = postNum = userHeapSize = postHeapSize = 0; for (register int i = 0; i < MAX_HASH1; i++) userInfoMap[i] = nullptr; for (register int i = 0; i < MAX_HASH2; i++) postInfoMap[i] = nullptr; } int writeMessage(char mUser[], int mID, int mPoint) { User* user = findUser(mUser); return addNewPost(user, mID, mPoint, nullptr); } int commentTo(char mUser[], int mID, int mTargetID, int mPoint) { User* user = findUser(mUser); Post* target = findPost(mTargetID); return addNewPost(user, mID, mPoint, target); } //When erasing post, traversal all descendants and update their user's point void updateUserPoint(Post* post) { post->user->info->totalPoint -= post->point; heapUpdate(userInfoHeap, userHeapSize, post->user->info); for (register Post* child = post->firstChild; child; child = child->next) updateUserPoint(child); } int erase(int mID) { Post* post = findPost(mID); updateUserPoint(post); Post* parent = post->parent; if (parent) {//this post is a comment or reply //delete from parent's list if (post == parent->firstChild) parent->firstChild = post->next; else { for (register Post* i = parent->firstChild; i->next; i = i->next) { if (post == i->next) { i->next = i->next->next; break; } } } parent->info->totalPoint -= post->info->totalPoint; if (parent->parent) { //this post is a reply parent = parent->parent; parent->info->totalPoint -= post->info->totalPoint; } heapUpdate(postInfoHeap, postHeapSize, parent->info); return parent->info->totalPoint; } //this is a message heapRemove(postInfoHeap, postHeapSize, post->info); return post->user->info->totalPoint; } BasicInfo* bestInfos[5]; void getBestMessages(int mBestMessageList[]) { for (register int i = 0; i < 5; i++) { bestInfos[i] = heapPop(postInfoHeap, postHeapSize); mBestMessageList[i] = bestInfos[i]->id; } for (register int i = 0; i < 5; i++) heapPush(postInfoHeap, postHeapSize, bestInfos[i]); } void getBestUsers(char mBestUserList[][MAX_L]) { for (register int i = 0; i < 5; i++) { bestInfos[i] = heapPop(userInfoHeap, userHeapSize); strCpy(mBestUserList[i], users[bestInfos[i]->objectIdx].name); } for (register int i = 0; i < 5; i++) heapPush(userInfoHeap, userHeapSize, bestInfos[i]); }
Editor is loading...