#include<stdlib.h>
#include<stdio.h>
typedef struct node{
int id;
int fileSize;
int origSize;
int isFolder;
struct node *parent;
struct node *child;
struct node *next;
struct node *brother;
}node;
#define SIZE 10003
int flag = 1;
node HashPool[SIZE + 3];
node HashTable[SIZE + 3];
int HashDex = 0;
int realfile;
node *getNewNode()
{
return &HashPool[HashDex++];
}
void insertNewNode(int key, node *newNode)
{
newNode->next = HashTable[key].next;
HashTable[key].next = newNode;
}
void init()
{
realfile = 0;
HashDex = 0;
for (int i = 0; i <SIZE +3; i++){ //为啥 hash表 这里要初始化呀???????????????
HashTable[i].id = HashPool[i].id = -1;
HashTable[i].fileSize = HashPool[i].fileSize = 0;
HashTable[i].origSize = HashPool[i].origSize = 0;
HashTable[i].isFolder = HashPool[i].isFolder = 1;
HashTable[i].child = HashPool[i].child = NULL;
HashTable[i].parent = HashPool[i].parent = NULL;
HashTable[i].brother = HashPool[i].brother = NULL;
HashTable[i].next = HashPool[i].next = NULL;
}
HashTable[10000].id = 10000;
HashTable[10000].fileSize = 0;
HashTable[10000].origSize = 0;
HashTable[10000].isFolder = 1;
HashTable[10000].child = NULL;
HashTable[10000].parent = NULL;
HashTable[10000].brother = NULL;
HashTable[10000].next = NULL;
}
int issame(int a,int b)
{
if (a == b) return 1;
return 0;
}
node *searchNode(int key,int pid)
{
node *head = &HashTable[key];
while (head)
{
if (issame(head->id, pid))
return head;
head = head->next;
}
return NULL;
}
void updataparent(node *parent, int filesize)
{
while (parent)
{
parent->fileSize = parent->fileSize + filesize;
parent = parent->parent;
}
}
int add(int id, int pid, int fileSize)
{
//scanf("%d%d%d", &id, &pid, &fileSize);
int val = id%SIZE;
node *newNode = getNewNode();
insertNewNode(val, newNode);
int pval = pid%SIZE;
node *parent=searchNode(pval, pid);
newNode->brother = parent->child;
parent->child = newNode;
newNode->parent = parent;
newNode->id = id;
newNode->fileSize = fileSize;
newNode->origSize = fileSize;
if (fileSize != 0){
realfile++;
newNode->isFolder = 0;
updataparent(parent, fileSize);
}
return parent->fileSize;
}
int move(int id, int pid){
//scanf("%d%d", &id, &pid);
int val = id%SIZE;
int pval = pid%SIZE;
node *moveNode;
node *parentNode;
moveNode=searchNode(val, id);
parentNode = searchNode(pval, pid);
if (moveNode->parent->child->id == moveNode->id) //可以不加id
{
moveNode->parent->child = moveNode->brother;
}else{
node *firstChild= moveNode->parent->child;
while (firstChild)
{
if (firstChild->brother->id == moveNode->id)
{
break;
}
firstChild = firstChild->brother;
}
firstChild->brother = moveNode->brother;
}
updataparent(moveNode->parent, moveNode->fileSize*(-1));
moveNode->brother = parentNode->child;
parentNode->child = moveNode;
moveNode->parent = parentNode;
updataparent(parentNode, moveNode->fileSize);
return parentNode->fileSize;
}
void infectChild(node *infetNode, int size)
{
while (infetNode)
{
if (infetNode->isFolder)
{
infectChild(infetNode->child, size);
}else{
infetNode->fileSize = infetNode->fileSize + size;
updataparent(infetNode->parent, size);
}
infetNode = infetNode->brother;
}
}
int infect(int id){
//scanf("%d", &id);
node *infetNode = searchNode(id%SIZE, id);
if (realfile == 0)
{
return 0;
}
int sumSize = HashTable[10000].fileSize/realfile;
if (infetNode->isFolder){
infectChild(infetNode->child, sumSize);
}else{
infetNode->fileSize = infetNode->fileSize + sumSize;
updataparent(infetNode->parent, sumSize);
}
return infetNode->fileSize;
}
void recoverFolder(node *recoverNode)
{
while (recoverNode)
{
if (recoverNode->isFolder)
{
recoverFolder(recoverNode->child);
}
else{
int value = recoverNode->origSize - recoverNode->fileSize;
recoverNode->fileSize = recoverNode->origSize;
updataparent(recoverNode->parent, value);
}
recoverNode = recoverNode->brother;
}
}
int recover(int id){
//scanf("%d", &id);
node *recoverNode = searchNode(id%SIZE, id);
if (recoverNode->isFolder)
{
recoverFolder(recoverNode->child);
}else{
int value = recoverNode->origSize - recoverNode->fileSize;
recoverNode->fileSize = recoverNode->origSize;
updataparent(recoverNode->parent, value);
}
return recoverNode->fileSize;
}
void deleteNode(node *Node){
node *cur = &HashTable[Node->id % SIZE];
while(cur){
if(cur->next->id == Node->id){
cur->next = cur->next->next;
break;
}
cur = cur->next;
}
Node->id = -1;
Node->fileSize =0;
Node->origSize = 0;
Node->isFolder = 1;
Node->child = NULL;
Node->parent = NULL;
Node->brother = NULL;
Node->next = NULL;
}
void removeNode1(node *Node){
while (Node){
node *tem = Node->brother; // why z这不是个空吗
if (Node->isFolder){
removeNode1(Node->child);
}
else{
realfile--;
}
deleteNode(Node);
Node = tem;
}
}
int remove(int id){
//scanf("%d", &id);
node *removeNode = searchNode(id%SIZE, id);
int val = removeNode->fileSize;
if (id == 10000)
{
init();
}
else{
updataparent(removeNode->parent, val*(-1));
if (removeNode->parent->child->id == removeNode->id) //可以不加id ????????????? 直接比较两个node不行吗
{
removeNode->parent->child = removeNode->brother;
}
else{
node *firstChild = removeNode->parent->child;
while (firstChild)
{
if (firstChild->brother->id == removeNode->id)
{
break;
}
firstChild = firstChild->brother;
}
firstChild->brother = removeNode->brother;
}
removeNode->brother = NULL; //why
removeNode1(removeNode);
}
return val;
}