void *heapAlloc (heap_t *h, size_t size) { sector_t *s,*item,*so; item=(sector_t*)malloc(sizeof(sector_t)); item->dimen=size; char*tmp; if(h->dim<size) { printf("the sector is too big\n"); return NULL; } if(h->policy==first_fit)s=(sector_t*)ExtractElement(h->empty,compare_by_size,(void*)item); if(h->policy==best_fit) s=(sector_t*)ExtractMINElement(h->empty,compare_by_size,(void*)item); if(h->policy==wors_fit)s=(sector_t*)ExtractMAXElement(h->empty,compare_by_size,(void*)item); so=s; s->dimen-=size; if(s->dimen!=0) h->empty=OrderInsert(h->empty,(void*)s,compare_by_offset);//reinserisco avanzo so->offset=(int)h->base; so->offset=size; h->full=OrderInsert(h->full,(void*)so,compare_by_offset); tmp=(char*)h->base; tmp+=size; h->base=(void*)tmp; return(void*)(so->offset); return h->base; }
int main() { int result; poly p; poly b; Elem dat,dat1,dat2; createElem(&dat,0.0,0); createElem(&dat1,0.2,2); createElem(&dat2,0.3,3); create(&p); create(&b); //insertHead(p,dat1); //insertTail(p,dat2); OrderInsert(p,dat2,compareExpn); OrderInsert(p,dat1,compareExpn); traverse(p,print); LocElem(p, dat1, compareBoth,&result,"compareBoth"); createElem(&dat2,0.1,3); OrderInsert(p,dat2,compareExpn); traverse(p,print); createElem(&dat2,-0.1,2); OrderInsert(p,dat2,compareExpn); traverse(p,print); printf("---------------------------------------\n"); createElem(&dat2,-0.3,5); OrderInsert(p,dat2,compareExpn); traverse(p,print); printf("---------------------------------------\n"); //createElem(&dat2,0.5,1); createElem(&dat2,0.4,4); OrderInsert(b,dat2,compareExpn); createElem(&dat2,-0.1,2); OrderInsert(b,dat2,compareExpn); createElem(&dat2,-0.5,8); OrderInsert(b,dat2,compareExpn); createElem(&dat2,-0.9,7); OrderInsert(b,dat2,compareExpn); createElem(&dat2,-0.4,3); OrderInsert(b,dat2,compareExpn); traverse(b,print); printf("---------------------------------------\n"); AddPolyn(p,b); traverse(p,print); printf("---------------------------------------\n"); destroy(&p); destroy(&b); return OK; }
heap_t *heapCreate (size_t heapSize) { heap_t *new_heap; sector_t *s0; new_heap = (heap_t*)malloc(sizeof(heap_t)); if(new_heap == NULL) { printf("Error during heap allocation\n"); exit(EXIT_FAILURE); } new_heap->empty=ListInit(); new_heap->full=ListInit(); new_heap->dim=heapSize; new_heap->base=malloc(heapSize);//memoria dello heap allocata if(new_heap->base == NULL) { printf("Error during base allocation\n"); exit(EXIT_FAILURE); } s0=(sector_t*)malloc(sizeof(sector_t)); if(s0==NULL) return NULL; s0->offset=(int)new_heap->base; s0->dimen=heapSize; new_heap->empty = OrderInsert(new_heap->empty,(void*)s0,NULL);//inserisco in empty la partizione intera return(new_heap); }
void OpenForDay(int &TotalTime,int &CustomerNum,Event &en,EvenList &ev,LinkQueue *q){ //初始化 TotalTime=0;CustomerNum=0; InitList(ev); en.OccurTime=0; en.NType=0; OrderInsert(ev,en,cmp); for(int i=1;i<=4;++i)InitQueue(q[i]); }
void OpenForDay() { // 初始化操作 int i; InitList(ev); // 初始化事件链表为空 en.OccurTime=0; // 设定第一个客户到达事件 en.NType=Qu; // 到达 OrderInsert(ev,en,cmp); // 插入事件表 for(i=0;i<Qu;++i) // 置空队列 InitQueue(q[i]); }
//插入新的项并在链表里寻找与之指数相等的项,找到则相加并调用DeleteNext, 未找到则调用OrderInsert正常插入 void OrderInsertMerge(Linklist &L, term e, int (*comp)(term, term)){ Node pre, get; if (LocateElem(L, e, pre, get, comp)){ get -> data.coef += e.coef; if (!get -> data.coef) //若相加后的系数为0则将此节点删除 DeleteNext(L, pre); //用此节点的前驱指针删除此节点 } else OrderInsert(L, e, comp); }
void OpenForDay() { /* 初始化操作 */ int i; InitList(&ev); /* 初始化事件链表为空 */ en.OccurTime = 0; /* 设定第一个客户到达事件 */ en.NType = Qu; /* 到达 */ OrderInsert(&ev, en, cmp); /* 插入事件表 */ for (i = 0; i < Qu; ++i) /* 置空队列 */ InitQueue(&q[i]); }
void CustomerArrived() { // 处理客户到达事件,en.NType=Qu QElemType f; int durtime,intertime,i; ++CustomerNum; Random(durtime,intertime); // 生成随机数 et.OccurTime=en.OccurTime+intertime; // 下一客户到达时刻 et.NType=Qu; // 队列中只有一个客户到达事件 if(et.OccurTime<CloseTime) // 银行尚未关门,插入事件表 OrderInsert(ev,et,cmp); i=Minimum(q); // 求长度最短队列的序号,等长为最小的序号 f.ArrivalTime=en.OccurTime; f.Duration=durtime; EnQueue(q[i],f); if(QueueLength(q[i])==1) { et.OccurTime=en.OccurTime+durtime; et.NType=i; OrderInsert(ev,et,cmp); // 设定第i队列的一个离开事件并插入事件表 } }
void CustomerArrived() { /* 处理客户到达事件,en.NType=Qu */ QElemType f; int durtime, intertime, i; ++CustomerNum; Random(&durtime, &intertime); /* 生成随机数 */ et.OccurTime = en.OccurTime + intertime; /* 下一客户到达时刻 */ et.NType = Qu; /* 队列中只有一个客户到达事件 */ if (et.OccurTime < CloseTime) /* 银行尚未关门,插入事件表 */ OrderInsert(&ev, et, cmp); i = Minimum(q); /* 求长度最短队列的序号,等长为最小的序号 */ f.ArrivalTime = en.OccurTime; f.Duration = durtime; EnQueue(&q[i], f); if (QueueLength(q[i]) == 1) { et.OccurTime = en.OccurTime + durtime; et.NType = i; OrderInsert(&ev, et, cmp); /* 设定第i队列的一个离开事件并插入事件表 */ } }
void CustomerArrived(int CloseTime,int &CustomerNum,Event &en,EvenList &ev,LinkQueue *q){ //处理到达事件 en.NType=0 ++CustomerNum; Event t; int durtime,intertime; Random(durtime,intertime); t.OccurTime=en.OccurTime+intertime; t.NType=0; if(t.OccurTime<CloseTime)//插入事件表 OrderInsert(ev,t,cmp); int i=Minimum(q); QElemType t2; t2.ArrivaTime=en.OccurTime; t2.Duration=durtime; EnQueue(q[i],t2); t.OccurTime=en.OccurTime+durtime; t.NType=i; if(QueueLength(q[i])==1)//设定列队的离开事件 OrderInsert(ev,t,cmp); }
void CustomerDeparture() { // 处理客户离开事件,en.NTyPe<Qu int i; i=en.NType; DeQueue(q[i],customer); // 删除第i队列的排头客户 TotalTime+=en.OccurTime-customer.ArrivalTime; // 累计客户逗留时间 if(!QueueEmpty(q[i])) { // 设定第i队列的一个离开事件并插入事件表 GetHead(q[i],customer); et.OccurTime=en.OccurTime+customer.Duration; et.NType=i; OrderInsert(ev,et,cmp); } }
void CustomerDeparture() { /* 处理客户离开事件,en.NTyPe<Qu */ int i; i = en.NType; DeQueue(&q[i], &customer); /* 删除第i队列的排头客户 */ TotalTime += en.OccurTime - customer.ArrivalTime; /* 累计客户逗留时间 */ if (!QueueEmpty(q[i])) { /* 设定第i队列的一个离开事件并插入事件表 */ GetHead_Q(q[i], &customer); et.OccurTime = en.OccurTime + customer.Duration; et.NType = i; OrderInsert(&ev, et, cmp); } }
void CustomerDeparture(Event &en,EvenList &ev,LinkQueue *q,QElemType &customer,int &TotalTime){ //处理离开事件 int i=en.NType; DeQueue(q[i],customer);//删除排头的客户 TotalTime+=en.OccurTime-customer.ArrivaTime;//累计客户逗留时间 if(!QueueEmpty(q[i])){//设定第i列队的一个离开事件插入事件表 GetHead(q[i],customer); Event t; t.OccurTime=en.OccurTime+customer.ArrivaTime; t.NType=i; OrderInsert(ev,t,cmp); } }
void heapFree (heap_t *h, void *p) { sector_t*so,*item; sector_t*prev,*prox; item=(sector_t*)malloc(sizeof(sector_t)); item->offset=(int)p; so=ExtractEqual(h->full,compare_by_offset,(void*)item); if(so==NULL) exit(-1); h->empty=OrderInsert(h->full,(void*)so,compare_by_offset); /* * ora devo controllare che non ci siano partizioni libere contigue, nel caso ci fossero le devo fondere */ return; }
void AddPolyn(poly pa, poly pb) { poly qa = NULL; poly qb = NULL; poly tmp = NULL; poly tmp1 = NULL; poly prevA = NULL; //poly prevB = NULL; prevA = pa; qa = pa->next; qb = pb->next; int expnA = 0; int expnB = 0; while(qa && qb) { expnA = GetExpn(qa); expnB = GetExpn(qb); printf("expnA<%d> expnB<%d>\n",expnA,expnB); switch( cmp(expnA,expnB) ) { case 1: prevA = qa; qa = qa->next; break; case 0: printf("=========\n"); if( sum(qa->data,qb->data) != 0) { qa->data.coef += qb->data.coef; prevA = qa; qa = qa->next; } else { /* printf("prevA:"); print(prevA); printf("\n"); printf("qa:"); print(qa); printf("\n"); */ prevA->next = qa->next; tmp1 = qa->next; free(qa); qa = tmp1; } qb = qb->next; break; case -1: printf("+++++++++++\n"); tmp = (poly)malloc(sizeof(node)); tmp->data.coef = qb->data.coef; tmp->data.expn = qb->data.expn; tmp->next = qa; prevA->next = tmp; prevA = tmp; qb = qb->next; break; } } if(qb != NULL) { while(qb != NULL) { OrderInsert(pa,qb->data,compareExpn); qb = qb->next; } } }