Example #1
0
 void insertRect(TY** ppY,const TRect& rect){
     TY* pY=*ppY;
     if (rect.y1 <= pY->y0){  //之前
         insertAtFront(ppY,rect);
     }else if (rect.y0 >= pY->y1){ //之后
         insertRectNext(pY,rect);
     }else if (getIsCover(pY,rect)){ //包含
         return;
     }else{ //相交
         TRect nRect(rect);
         if (nRect.y1 > pY->y1){ //长出一截的部分交给下一个Y
             TRect backRect(nRect.x0,pY->y1,nRect.x1,nRect.y1);
             nRect.y1=pY->y1;
             insertRectNext(pY,backRect);
         }else if (nRect.y1< pY->y1){ //短一截,分裂Y
             clipY(pY,nRect.y1);
         }
         //nRect.y1==pY->y1
         
         if (nRect.y0 < pY->y0){ 
             TRect frontRect(nRect.x0,nRect.y0,nRect.x1,pY->y0);
             nRect.y0=pY->y0;
             YAddAX(pY,nRect.x0,nRect.x1);				
             insertAtFront(ppY,frontRect);
         }else if (nRect.y0< pY->y0){
             clipY(pY,nRect.y0);
             YAddAX(pY->nextY,nRect.x0,nRect.x1);
         }else{ //nRect.y0== pY->y0			
             YAddAX(pY,nRect.x0,nRect.x1);
         }
     }
 }
Example #2
0
int main(){
	/*make list and output file*/
    ListHndl TheList = newList();
    FILE *out = fopen("out.out", "w");
    
    /*test empty in empty case*/
    if(isEmpty(TheList)) printf("Empty\n");
    else printf("not Empty\n");
    printf("testing insert one number\n");
    insertAtFront(TheList,(unsigned long*)25728);
    printf("%lu\n",(unsigned long)getFirst(TheList));
    printf("%lu\n",(unsigned long)getLast(TheList));
    /*should have same value*/
    
    printf("testing list with three numbers\n");
    insertAtFront(TheList,(unsigned long*)1589458);
    insertAtBack(TheList,(unsigned long*)35762111234);
    printf("%lu\n",(unsigned long)getFirst(TheList));
    
    /*test empty in full case*/
    if(isEmpty(TheList)) printf("Empty\n");
    else printf("not Empty\n");
    
	/*test moving the current pointer around*/
    moveFirst(TheList);
    moveNext(TheList);
    printf("%lu\n",(unsigned long)getCurrent(TheList));
    moveLast(TheList);
    movePrev(TheList);
    printf("%lu\n",(unsigned long)getCurrent(TheList));
    
	/*test printList*/
    printList(out, TheList);
    
	/*test makeEmpty*/
    makeEmpty(TheList);
    if(isEmpty(TheList)) printf("Empty\n");
    else printf("not Empty\n");
    
	/*test inserting functions*/
    insertAtFront(TheList,(unsigned long*)2);
    insertAtFront(TheList,(unsigned long*)1);
    insertAtFront(TheList,(unsigned long*)4);
    insertAtBack(TheList,(unsigned long*)4);
    moveLast(TheList);
    insertBeforeCurrent(TheList,(unsigned long*)3);
    printList(out,TheList);
    deleteFirst(TheList);
    deleteCurrent(TheList);
    deleteLast(TheList);
    printList(out,TheList);
    
    makeEmpty(TheList);
    printList(out,TheList);
    
	/*free list and close output file*/
    freeList(&TheList);
    fclose(out);
    return 0;
}
Example #3
0
File: List.cpp Project: ombt/ombt
int
List<DataType>::insertUnique(const DataType &data)
{
	// invariant
	MustBeTrue((first != NULL || last == NULL) &&
		   (first == NULL || last != NULL));

	// search for correct place to insert new item
	ListItem<DataType> *pos;
	for (pos = first; pos != NULL && pos->data != data ; pos = pos->next) ;

	// check which case
	if (pos != NULL && pos->data == data)
	{
		// we found it, overwrite current data
		pos->data = data;
	}
	else
	{
		// insert at the beginning of the list
		return(insertAtFront(data));
	}

	// all done
	return(OK);
}
Example #4
0
File: List.cpp Project: ombt/ombt
int
List<DataType>::insertNth(int n, const DataType &data)
{
	// invariant
	MustBeTrue((first != NULL || last == NULL) &&
		   (first == NULL || last != NULL));

	// check for out of range
	if (n < 1)
		return(NOMATCH);

	// find location to insert new tuple
	if (isEmpty())
	{
		// add at the front
		return(insertAtFront(data));
	}
	else if (n > count)
	{
		// add at the end
		return(insertAtEnd(data));
	}

	// search for correct place to insert new item
	int i;
	ListItem<DataType> *pos;
	for (i = 1, pos = first; pos != NULL && i < n; pos = pos->next, i++) ;

	// check which case
	if (pos != NULL && i == n)
	{
		// allocate a new item
		ListItem<DataType> *pitem = 
			new ListItem<DataType>(data);
		if (pitem == NULL) return(NOTOK);

		// update links
		pitem->previous = pos->previous;
		pitem->next = pos;
		pos->previous = pitem;
		if (pitem->previous != NULL)
		{
			// previous link must point to new link
			pitem->previous->next = pitem;
		}
		else
		{
			// at beginning of list
			first = pitem;
		}

		// increment the counter
		count++;
		return(OK);
	}
	else
		return(NOMATCH);
}
Example #5
0
List* ListOrganizer::insertAfter(List *p, Node *w, std::string n) {
	Node *q;
	if (w == NULL || p->front == NULL)
		return insertAtFront(p, n);
	q = newNode(n);
	q->next = w->next;
	w->next = q;
	if (w == p->rear)
		p->rear = q;
	return p;
}
Example #6
0
// Return the List handle of paths to a destination.
ListHndl getPathTo(Graph *G, int destination) {
    int distance = getDistance(G, destination);
    if (distance == -1) return NULL;

    ListHndl tmp = newList(); // Freed by user.
    insertAtBack(tmp, destination);
    for (int i = 0; i < distance; ++i) {
        insertAtFront(tmp, G->parent[destination]);
        destination = G->parent[destination];
    }
    return tmp;
}
Example #7
0
int main() {
    ListHndl intList;
    intList = newList();

    int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
/*
 * Integer list testing
 *
 */

    printHeading(" INTEGER LIST ", '#', 80);
    printList(stdout, intList);
    accessorTest("isEmpty", 1, isEmpty(intList));
    
    for(int i = 0; i < 10; i++) {
        insertAtFront(intList, data[i]);
        mutatorTest("%s : data = %d", "insertAtFront", data[i]);
    }

    moveFirst(intList);

    for(int i = 0; i < 5; i++) {
        printList(stdout, intList);
        insertBeforeCurrent(intList, data[i]);
        mutatorTest("insertBeforeCurrent : data = %d", data[i]);
        moveNext(intList);
    }

    accessorTest("isEmpty", 0, isEmpty(intList));
    printList(stdout, intList);

    moveFirst(intList);
    while( !offEnd(intList) ) {
        printList(stdout, intList);
        moveNext(intList);
    }

    moveLast(intList);
    while( !offEnd(intList) ) {
        printList(stdout, intList);
        movePrev(intList);
    }

    makeEmpty(intList);
    mutatorTest("makeEmpty( intList)");

    printList(stdout, intList);
    accessorTest("isEmpty", 1, isEmpty(intList));

    freeList(intList);
    return 0;
}
void insertBook(BookListHndl L, char * title, int id)
{

	BookNodePtr tempNode;
	char * tempTitle;
	assert (L != NULL);
	tempTitle = malloc(sizeof(char) * 50);
	
	strcpy(tempTitle, title);
	
	if(L->first != NULL)
	{
		L->curr = L->first;
		while (L->curr != NULL)
		{
		/*printf("comparing %s and %s, which gives %d\n", L->curr->title, title, strcmp(L->curr->title, tempTitle) );*/
			if(strcmp(L->curr->title, tempTitle) == 0)
			{
				insertID(L, id);
				L->curr = L->first;
				return;
			}
			L->curr = L->curr->next;
		}
	}
	L->curr = L->first;
	
	tempNode = malloc ( sizeof(struct BookNodeStruct) );

	tempNode->title = malloc ( sizeof(char) * 50 );
	strcpy(tempNode->title, tempTitle);
	tempNode->bookIDs = NewList();
	insertAtFront(tempNode->bookIDs, id);
	tempNode->next = L->first;
	tempNode->prev = NULL;

	if(L->first == NULL)
	/*this means we're adding the first element, meaning L needs to have curr, first, & last assigned.*/
	{ 
		L->first = tempNode;
		L->last = tempNode;
		L->curr = tempNode;
	}
	else
	{
		L->first->prev = tempNode;
		L->first = tempNode;
	}
	L->curr = L->first;
	/*printf("Successfully inserted a new node in the front \n");*/
}
Example #9
0
ListHndl getPathTo(Graph G, int destination)
{
    assert(G->numVerts != 0);
    int num = destination;
    ListHndl L = newList();
    /*insertAtFront(L, num);*/
    while(num != NULL)
    {
        insertAtFront(L, num);
        num = G->parent[num];
        /*insertAtFront(L, num);*/
    }
    printList(L);
    return L;
}
Example #10
0
ListHndl getPathTo(GraphHndl g, int destination)
{
	/* Temporary list will accumulate the data we request*/
	ListHndl thePath = NewList();
	int currNode = destination;

	/*Follow the parent pointers and insert them into the list*/
	/*Stop at -1, where that node has no parent*/
	while(currNode != -1)
	{
		insertAtFront(thePath,currNode);
		currNode = g->parentPtr[currNode];
	}
	/* Return the list that accumulated the path */
	return thePath;
}
Example #11
0
int insertNode(List *list,int index,void* data){
    Node *head,*newNode;
    int i;
    if(index <= -1 || index > list->length)
        return 0;
    list->length++;
        head = list->head;
    for (i = 0; i < index; ++i){
        if(head->next != NULL)
            head = head->next;
    }
    newNode = malloc(sizeof(Node));
    newNode->next = NULL;
    newNode->data = data;
    if(1 == insertAtFront(newNode,list,index,head)) return 1;
    if(1 == insertAtEnd(newNode,list,index,head)) return 1;
    insertAtMiddle(newNode, head);
    return 1;
}
Example #12
0
/*
 * coalesce - boundary tag coalescing.
 * This function joins adjecent free blocks together by
 * finding the size (newsize) of the new (bigger) free block, removing the
 * free block(s) from the free list, and changing the header
 * and footer information to the newly coalesced free block
 * (size = newsize, allocated = 0)
 * Then, we add the new free block to the front of the free list.
 *
 * This function takes a block pointer to the newly freed block (around which
 * we must coalesce) and returns the block pointer to the coalesced free
 * block.
 * Return ptr to coalesced block
 */
static void *coalesce(void *bp)
{
	size_t prev_alloc;
	prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))) || PREV_BLKP(bp) == bp;
	size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
	size_t size = GET_SIZE(HDRP(bp));
    
	/* Case 1, extend the block leftward */
	if (prev_alloc && !next_alloc)
	{
		size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
		removeBlock(NEXT_BLKP(bp));
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size, 0));
	}
    
	/* Case 2, extend the block rightward */
	else if (!prev_alloc && next_alloc)
	{
		size += GET_SIZE(HDRP(PREV_BLKP(bp)));
		bp = PREV_BLKP(bp);
		removeBlock(bp);
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size, 0));
	}
    
	/* Case 3, extend the block in both directions */
	else if (!prev_alloc && !next_alloc)
	{
		size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
        GET_SIZE(HDRP(NEXT_BLKP(bp)));
		removeBlock(PREV_BLKP(bp));
		removeBlock(NEXT_BLKP(bp));
		bp = PREV_BLKP(bp);
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size, 0));
	}
	
	insertAtFront(bp);
	
	return bp;
}
Example #13
0
void Board::addToBoard(Bone b, char row) //Adds a bone to the board
{
	unsigned int lastValue = getLastValueByRow(row);
	if (!b.isDouble())
	{
		if (b.getValue2() == lastValue)
			b.swap();
	}

	if (toupper(row) == 'W' && !hasSpinner())
	{
		Bone tempBone = rightRow[0];
		tempBone.swap();
		rightRow[0] = tempBone;
		if (b.getValue2() == tempBone.getValue1())
			b.swap();
		insertAtFront(rightRow, b);
	}
	else
		getRow(row).push_back(b);
}
Example #14
0
File: List.cpp Project: ombt/ombt
int
List<DataType>::insert(const DataType &data)
{
	return(insertAtFront(data));
}
Example #15
0
/*
 * coalesce
 * Coalesce combines multiple free blocks into one single
 * free block using boundary tag coalescing. This prevents internal
 * fragmentation and also increases throughput since we do not have
 * to traverse multiple blocks.
 *
 * We take 4 cases.
 * Case 1: Previous AND next blocks allocated. Just return pointer.
 * Case 2: Previous free, Next allocated.
 * Case 3: Next free, Previous allocated.
 * Case 4: Previous and next free.
 *
 * The main part of the function first sets up the free block for
 * insertion into the free list. Once it is ready, we insert the free list
 * using a LIFO algorithm.
 */
static void *coalesce(void *bp)
{
  size_t prev_alloc = PREV_BLKP(bp) == bp || GET_ALLOC(HDRP(PREV_BLKP(bp)));
  size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
  size_t size;

  /* Case 1 */
  if(prev_alloc && next_alloc)
    {
      /* Insert new free block at front of free list. (LIFO) */
      insertAtFront(bp);
      return bp;
    }

  size = GET_SIZE(HDRP(bp));

  /* Case 2 */
  if (!prev_alloc && next_alloc)
    {
      /* Go to previous block and set size to
       * sum of sizes of current block and
       * previous block. Remove the block from
       * the free list and reset allocated bit.
       */
      bp = PREV_BLKP(bp);
      size += GET_SIZE(HDRP(bp));
      delBlkFromFreeList(bp);
      PUT(HDRP(bp), PACK(size, 0));
      PUT(FTRP(bp), PACK(size, 0));
    }

  /* Case 3 */
  if (prev_alloc && !next_alloc)
    {
      /* Get size.
       * Remove the next block from the free list
       * ready the free block.
       */
      size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
      delBlkFromFreeList(NEXT_BLKP(bp));
      PUT(HDRP(bp), PACK(size, 0));
      PUT(FTRP(bp), PACK(size, 0));
    }

  /* Case 4 */
  else if (!prev_alloc && !next_alloc)
    {
      /* We get the size and remove the next and previous
       * blocks from the free list. We then move the block
       * pointer to the previous block and ready the block
       * for insertion into the free list.
       */
      size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
	GET_SIZE(HDRP(NEXT_BLKP(bp)));
      delBlkFromFreeList(PREV_BLKP(bp));
      delBlkFromFreeList(NEXT_BLKP(bp));
      bp = PREV_BLKP(bp);
      PUT(HDRP(bp), PACK(size, 0));
      PUT(FTRP(bp), PACK(size, 0));
    }

  /* Insert the block pointer to by bp at the front of the free list. */
  insertAtFront(bp);

  return bp;
}
Example #16
0
int main (){

  /***Exercise List constructor***/
  ListHndl List;
  List = NULL;
  List = newList ();

  if(List){
    printf("List Created\n");
  }else{
    printf("List Not Created\n");
  }
  
  printf("isEmpty %d\n",isEmpty(List)); /*should print 1*/
  
  /***Populate with test data***/
  int i;
  for(i=0; i<=4; i++){
    insertAtFront(List,i);
  }
  
  printList(stdout, List);
  
  printf("isEmpty %d\n",isEmpty(List)); /*should print 0*/

  int j;
  for(j=5; j<=9; j++){
    insertAtBack(List,j);
  } 
  printList(stdout, List);
  
  /***Exercise all access functions***/
  printf("offEnd %d\n",offEnd(List));/*should print 0*/
  
  printf("atFirst %d\n",atFirst(List));/*should print 0*/
  
  printf("atLast %d\n",atLast(List));/*should print 0*/
  
  printf("getFirst %d\n", getFirst(List));/*should print 4*/
  
  printf("getLast %d\n", getLast(List));/*should print 9*/
  
  printf("getCurrent %d\n", getCurrent(List));/*should print 0*/
  
  /***Exercise all removal manipulation functions***/
  deleteLast(List);
  printList(stdout, List);
  printf("getLast %d\n", getLast(List));/*should print 8*/
  
  deleteFirst(List);
  printList(stdout, List);
  printf("getFirst 	\n", getFirst(List));/*should print 3*/
  
  deleteCurrent(List);
  printList(stdout, List);
  
  moveLast(List);
  printList(stdout, List);
  movePrev(List);
  printList(stdout, List);
  moveNext(List);
  printList(stdout, List);

  /***Exercise various edge cases***/  
  makeEmpty(List);
  insertAtFront(List, 40);
  moveFirst(List);
  deleteCurrent(List);
  
  insertAtFront(List, 41);
  insertAtBack(List, 42);
  moveFirst(List);
  insertBeforeCurrent(List, 43);
  printList(stdout, List);
  
  /***Exercise List destructors***/
  
  deleteCurrent(List);
  printList(stdout, List);
  
  makeEmpty(List);
  printf("offEnd %d\n",offEnd(List));/*should print 1*/
  

  freeList(&List);
  return(0);
}