Exemplo n.º 1
0
void	Core::HandleDirection()
{
  t_pos	head;
  t_pos tmp;

  head = getHead();
  tmp = getHead();
  switch (this->_direction)
    {
    case LEFT :
      moveLeft(&head);
      break;
    case RIGHT :
      moveRight(&head);
      break;
    default :
      break;
    }
  if (isDead(getHead()) == true)
    return ;
  else
    {
      this->_snake.push_front(head);
      this->_snake.pop_back();
    }
  newPos(&tmp);
}
Exemplo n.º 2
0
void StartTask(Task* p) { // modif quand CMD.wd
	if (empty(&(p->q))) return;
	if (p->exec==1) {
		fprintf(stderr, "*** task %s already running\n", p->id);
		return;
	}
	if (sha->running == MAX_RUNNING) {
		fprintf(stderr, "*** too much tasks already running (max. %d)\n", MAX_RUNNING);
		return;
	}
	sha->running++;
	p->exec=1;
	char line[1024];
	char wd[1024];
	char command[128][128];
	while (!empty(&(p->q))) {
		while (p->demand) sleep(1);
		if (p->exec==0) break;
		sprintf(line, "%s", &(getHead(&(p->q)).cmd[0]));
		Parse(line, (char**)command);
		sprintf(wd, "%s", &(getHead(&(p->q)).wd[0]));
		subHead(&(p->q));
		Execute((char**)command, (char*)wd);
	}
	p->exec=0;
	sha->running--;
}
Exemplo n.º 3
0
bool Snake::selfColission(){
    int xh = getHead()->x();
    int yh = getHead()->y();
    for(int i = 3; i < parts.count(); i++){
        int xi = parts.at(i)->x();
        int yi = parts.at(i)->y();
        switch(direction()){
        case UP:
            if(yh - partSize == yi && xh == xi)
                return true;
            break;
        case DOWN:
            if(yh + partSize == yi && xh == xi)
                return true;
            break;
        case LEFT:
            if(xh - partSize == xi && yh == yi)
                return true;
            break;
        case RIGHT:
            if(xh + partSize == xi && yh == yi)
                return true;
        }
    }
    return false;
}
Exemplo n.º 4
0
struct segwrap *
seghash_get (struct segwrap **hash_table, size_t table_size, seq_t key)
{
    int i;
    struct segwrap *head;
    struct segwrap *cur;
    struct segwrap *selected;

    assert (hash_table != NULL);
    assert (table_size > 0);

    i = hash_fun (key, table_size);
    assert (i >= 0);
    assert (i < table_size);

    selected = NULL;
    head = getHead (hash_table[i]);
    if (head != NULL) {
        cur = head;
        do {
            if (key == seg_seq (cur->sw_seg))
                selected = cur;
            else
                cur = getNext (cur);
        } while (selected == NULL && cur != getHead (hash_table[i]));
    }

    return selected;

}
Exemplo n.º 5
0
int MainWindow::getRandomStep(QPoint p)
{
    int m[MAX_SIZE][MAX_SIZE];
    memcpy(m,map,sizeof(m));
    QPoint p_a[4];
    p_a[to_left] = QPoint(p.x() - 1, p.y());
    p_a[to_right] = QPoint(p.x() + 1, p.y());
    p_a[to_up] = QPoint(p.x(), p.y() - 1);
    p_a[to_down] = QPoint(p.x(), p.y() + 1);
    int p_l[4];
    int i = 0;
    int p_mn = -1;
    int p_max = -1;
    for(; i < 4; i++)
    {
        if(is_in(p_a[i]) && m[p_a[i].x()][p_a[i].y()] == none)
        {
            p_l[i] = calcH(point(getHead().x(),getHead().y()), food_pos);
            if(p_l[i] > p_max)
            {
                p_max = p_l[i];
                p_mn = i;
            }
        }
    }
    return p_mn;
}
bool DisjointSetInt::inSameSet(int idNode1, int idNode2){
	int head1 = getHead(idNode1);
	int head2 = getHead(idNode2);
	if (head1 == -1 || head2 == -1){
		cerr << "access invalid node in disjointSet" << endl;
		return false;
	}
	return (head1 == head2);
}
Exemplo n.º 7
0
void Snake::update()
{
    if(getIsDead())
    {
        emit snakeIsDead();

        return;
    }

    if(getHead().x() <= -1 || getHead().x() >= grid.x() || getHead().y() <= -1 || getHead().y() >= grid.y())
    {
        emit killedSelf();

        return;
    }

    for(int i = 0; i < body.count(); ++i)
    {
        if(i == 0)
        {
            last = body.first();

            switch(direction)
            {
                case Top:
                    body.first().setY(body.first().y() - 1);
                    break;

                case Bottom:
                    body.first().setY(body.first().y() + 1);
                    break;

                case Left:
                    body.first().setX(body.first().x() - 1);
                    break;

                case Right:
                    body.first().setX(body.first().x() + 1);
                    break;
            }
        }
        else
        {
            QPoint now = body[i];

            if(body[i] == body.first())
                emit killedSelf();

            body[i] = last;

            last = now;
        }
    }

    dirChanged = false;
}
Exemplo n.º 8
0
/*
* Routine: void EvalTextExpr(expr_t *pExpr)
* Purpose: set the values to be used to replace a given tag for this query generation
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
*/
int 
EvalTextExpr(expr_t *pExpr, Expr_Val_t *pBuf, Expr_Val_t *pParams, int bIsParam)
{
	int i,
		nWeightTotal = 0,
		nModifierArg;
	expr_t *pReplacement;
	
	switch (pExpr->nFlags & (EXPR_FL_LIST | EXPR_FL_RANGE))
	{
	case EXPR_FL_LIST:	/* return a set of values */
		if (bIsParam)
			ReportError(QERR_MODIFIED_PARAM, NULL, 1);
		nModifierArg = (int)pParams[1].nValue;
		pExpr->pPermute = makePermutation(pExpr->pPermute, length(pExpr->ArgList) - 1, 0);
		for (i=0; i < nModifierArg; i++)
		{
			pReplacement = getItem(pExpr->ArgList, getPermutationEntry(pExpr->pPermute, i + 1));
			AddBuffer(pBuf[i].pBuf, GetBuffer(pReplacement->Value.pBuf));
			pBuf[i].bUseInt = 0;
		}
		break;
	case EXPR_FL_RANGE:
	case EXPR_FL_RANGE|EXPR_FL_LIST: /* only list() is permissible here */
		ReportError(QERR_RANGE_LIST, NULL, 1);
		break;
	default:
		/* 1. find a substitution from the weighted distribtuion */
		for (pReplacement = (expr_t *)getHead(pExpr->ArgList); 
		pReplacement; 
		pReplacement = (expr_t *)getNext(pExpr->ArgList))
			nWeightTotal += (int)pReplacement->Value.nValue;
		i = genrand_integer(NULL, DIST_UNIFORM, 1, nWeightTotal, 0, 0);
		for (pReplacement = (expr_t *)getHead(pExpr->ArgList); 
		pReplacement; 
		pReplacement = (expr_t *)getNext(pExpr->ArgList))
		{
			if (i <= (int)pReplacement->Value.nValue)
				break;
			i -= (int)pReplacement->Value.nValue;
		}
		
		/* 2. get the appropraite string */
		AddBuffer(pBuf->pBuf, GetBuffer(pReplacement->Value.pBuf));
		break;
	}
	
	return(DT_STR);
}
Exemplo n.º 9
0
/* Simulate how the CPU works. */
void cpu_exec(volatile uint32_t n) {
	if(nemu_state == END) {
		printf("Program execution has ended. To restart the program, exit NEMU and run again.\n");
		return;
	}
	nemu_state = RUNNING;

#ifdef DEBUG
	volatile uint32_t n_temp = n;
#endif

	setjmp(jbuf);

	for(; n > 0; n --) {
#ifdef DEBUG
		swaddr_t eip_temp = cpu.eip;
		if((n & 0xffff) == 0) {
			/* Output some dots while executing the program. */
			fputc('.', stderr);
		}
#endif

		/* Execute one instruction, including instruction fetch,
		 * instruction decode, and the actual execution. */
		int instr_len = exec(cpu.eip);

		cpu.eip += instr_len;

#ifdef DEBUG
		print_bin_instr(eip_temp, instr_len);
		strcat(asm_buf, assembly);
		Log_write("%s\n", asm_buf);
		if(n_temp < MAX_INSTR_TO_PRINT) {
			printf("%s\n", asm_buf);
		}
#endif

		/* TODO: check watchpoints here. */
		WP *tail=getHead();
		uint32_t result;
		bool f;
		while(tail){
			result=expr(tail->expr,&f);
			if(f==false) return;
			if(result!=tail->result){
				nemu_state=STOP;
				printf("监视点%d:  %s\n",tail->NO,tail->expr);
				printf("原值 result=%d\n",tail->result);
				printf("新值 result=%d\n",result);
				tail->result=result;
			}
			tail=tail->next;
		}


		if(nemu_state != RUNNING) { return; }
	}

	if(nemu_state == RUNNING) { nemu_state = STOP; }
}
Exemplo n.º 10
0
void MainWindow::move()
{
    auto s_head = getHead();
    auto s_food = QPoint(food_pos.x(), food_pos.y());
    auto s_foot = getFoot();
    state_now = find_way(s_head, s_food);
    if(state_now == -1){
        state_now = find_way(s_head, s_foot);
    }
    if(state_now == to_left)
    {
        go_left();
    }else if(state_now == to_right){
        go_right();
    }else if(state_now == to_up){
        go_up();
    }else if(state_now == to_down){
        go_down();
    }else{
        state_now = getRandomStep(s_head);
        if(state_now == -1){
            QMessageBox::information(0, "", "end");
            gameover();
        }else if(state_now == to_left){
            go_left();
        }else if(state_now == to_right){
            go_right();
        }else if(state_now == to_up){
            go_up();
        }else if(state_now == to_down){
            go_down();
        }
    }
    update();
}
Exemplo n.º 11
0
int pagefault_handler(int pid, int pageNo, char type) {
	struct Node *head = getHead();
	if (allocated < NUM_FRAME) {
		frame_table[allocated].page = pageNo;
		pageTable.entry[pid][pageNo].valid = true;
		int ret = list_insert_tail(head, allocated);
		allocated++;
		return allocated;
	} else {
		int ret = page_replacement(allocated);
		list_insert_tail(head, allocated);
		frame_table[allocated].page = pageNo;
		pageTable.entry[pid][pageNo].valid = true;
		int victimPid = frame_table[ret].pid;
		int victimPageNo = frame_table[ret].page;
		pageTable.entry[victimPid][victimPageNo].valid = false;
		if (pageTable.entry[victimPid][victimPageNo].dirty) {
			disk_write(allocated, pid, pageNo);
			pageTable.entry[victimPid][victimPageNo].dirty = false;
		}
		clockHelper(allocated);
		allocated++;
		return allocated;
	}
}
Exemplo n.º 12
0
static Void local drawClassRelations(HDC hDC)
{
  Class cls;
  for(cls=CLASSMIN; cls<classMax(); cls++) {
    List supers;
    for(supers=cclass(cls).supers; nonNull(supers); supers=tl(supers)) {
      Class parent = getHead(hd(supers));
      if (isClass(parent)) {
	if (parent == cls) {     /* child of itself - draw an arc */
	  Class source = findClassInNodes(cls);
	  Arc(hDC, Nodes[source].Pos.right-5,  Nodes[source].Pos.bottom-5,
		   Nodes[source].Pos.right+15, Nodes[source].Pos.bottom+20,
		   Nodes[source].Pos.right-5,  Nodes[source].Pos.bottom-5,
		   Nodes[source].Pos.right-4,  Nodes[source].Pos.bottom-4);
	} else { 	               /* Join the two classes with a line */
	  Class source = findClassInNodes(parent);
	  Class target = findClassInNodes(cls);

	  INT sx = Nodes[source].Pos.right + 4;
	  INT sy = Nodes[source].Pos.top
		   + (Nodes[source].Pos.bottom - Nodes[source].Pos.top)/2;
	  INT tx = Nodes[target].Pos.left  - 4;
	  INT ty = Nodes[target].Pos.top
		   + (Nodes[target].Pos.bottom - Nodes[target].Pos.top)/2;

	  MoveToEx(hDC, sx, sy,NULL);
	  LineTo(hDC, tx, ty);
	}
      }
    }
  }
}
Exemplo n.º 13
0
/**
 * deep copy of this list (allocates links)
 */
Link* List::copy() const
{
    if (0 == getHead()) {
        return 0;
    } else {
        Link *head = new Link(*getHead());
        Link *next = head;
        // @inv: next points to last node in new list
        // origPtr points to node in original list that is not yet copied
        for (Link *origPtr = getHead()->getNext(); 0 != origPtr; origPtr = origPtr->getNext()) {
            next->setNext(new Link(*origPtr));
            next = next->getNext();
        }
        return head;
    }
}
Exemplo n.º 14
0
void HwcmScorer::extractHeadWordChain(TreePointer tree, vector<string> & history, vector<map<string, int> > & hwc) {

  if (tree->GetLength() > 0) {
    string head = getHead(tree);

    if (head.empty()) {
      for (std::vector<TreePointer>::const_iterator it = tree->GetChildren().begin(); it != tree->GetChildren().end(); ++it) {
        extractHeadWordChain(*it, history, hwc);
      }
    }
    else {
      vector<string> new_history(kHwcmOrder);
      new_history[0] = head;
      hwc[0][head]++;
      for (size_t hist_idx = 0; hist_idx < kHwcmOrder-1; hist_idx++) {
        if (!history[hist_idx].empty()) {
          string chain = history[hist_idx] + " " + head;
          hwc[hist_idx+1][chain]++;
          if (hist_idx+2 < kHwcmOrder) {
            new_history[hist_idx+1] = chain;
          }
        }
      }
      for (std::vector<TreePointer>::const_iterator it = tree->GetChildren().begin(); it != tree->GetChildren().end(); ++it) {
        extractHeadWordChain(*it, new_history, hwc);
      }
    }
  }
}
Exemplo n.º 15
0
boolean RingBuffer::read(uint8_t page[], size_t size) {
	
	uint16_t head = getHead();	
	uint16_t tail = getTail();	
	uint16_t pageSize = getPageSize();
	
	boolean retVal = false;
	if (getFlipMarker() == 0) {
		if (tail < head) {
			readPage(tail,pageSize,page,size);
			retVal = true;
		} 
	} else {
		if (tail + pageSize > bootSectorStart) {
			tail = startData;
			setTail(tail);
			setFlipMarker(0x00);
			if (tail < head) {
				readPage(tail,pageSize,page,size);
				retVal = true;
			} 
		} else {
			readPage(tail,pageSize,page,size);
			retVal = true;
		}
	}
	return retVal;
}
Exemplo n.º 16
0
boolean RingBuffer::write(uint8_t page[], size_t size) {
	
	uint16_t head = getHead();	
	uint16_t tail = getTail();	
	uint16_t pageSize = getPageSize();
	
	boolean retVal = false;
	// check, if we wrapped 
	if (getFlipMarker() == 0) {
		if (head + pageSize > bootSectorStart) {
			// we reached the end of the memory, go to start and flip marker
			head = startData;
			setHead(head);
			setFlipMarker(0xFF);
			if (head + pageSize < tail) {
				// there is some space, write the page
				writePage(head,pageSize,page,size);
				retVal = true;
			} 
		} else {
			// there is some space, write the page
			writePage(head,pageSize,page,size);
			retVal = true;
		}
	} else {
		if (head < tail) {
			// we have some sapace left
			writePage(head,pageSize,page,size);
			retVal = true;
		} 
	}	
	return retVal;
}
void GDL_RelationalSentence::buildName(){
    name = getHead()->toString();
    switch(body.size()){
    case 0:
        qDebug() << "Relation with arity 0 : " << name;
        break;
    case 1:
        name = QString('(') + name + ' ' + body[0]->toString() + ')';
        break;
    default:
        name = QString('(') + name + " (" + body[0]->toString();
        for(int i=1; i<body.size(); ++i){
            name = name + " " + body[i]->toString();
        }
        name = name + "))";
        break;
    }


    if(type==GDL::NEXT){
        name = QString("next ") + name;
    }
    if(type==GDL::BASE){
        name = QString("base ") + name;
    }
    if(type==GDL::TRUE){
        name = QString("true ") + name;
    }
    if(type==GDL::INIT){
        name = QString("init ") + name;
    }
}
Exemplo n.º 18
0
void Snake::setDirection(int arg){
    this->dir = arg;
    SnakePart* h = getHead();
    if(h != 0){
        h->setDirection(arg);
    }
}
Exemplo n.º 19
0
// Inherited
void Logic_Relation::buildName(){
    name = getHead()->toString();
    switch(body.size()){
    case 0:
        break;
    default:
        name = QString('(') + name + " " + body[0]->toString();
        for(int i=1; i<body.size(); ++i){
            name = name + " " + body[i]->toString();
        }
        name = name + ")";
        break;
    }

    switch(qualifier){
    case BASE:
        name = QString("(base ") + name + ")";
        break;
    case TRUE:
        name = QString("(true ") + name + ")";
        break;
    case INIT:
        name = QString("(init ") + name + ")";
        break;
    default:
        break;
    }

    if(negation){
        name = QString("(not ") + name + ")";
    }

    buildNameWithNoQualifier();
}
Exemplo n.º 20
0
RingBufferBlock* RingBuffer::searchFreeBlock(int size)
{
	int alignLength = ALIGN(sizeof(struct RingBufferBlock) + size);
	int freeSpace = 0;

	// 从head开始依次往后查找空闲block,直到拼接起来的空闲block长度大于传入的size
	struct RingBufferBlock *block = getHead();
	do {
		// 如果该块当前仍在被使用,则中止搜索
		if (block->m_link != NULL && block->m_length >= sizeof(struct RingBufferBlock)) {
			return NULL;
		}

		freeSpace += ALIGN(block->m_length) + sizeof(RingBufferBlock);

		// 检测长度是否已足够分配
		if (freeSpace > alignLength + sizeof(RingBufferBlock)) {
			// 将找到的这几块空闲block切割为2块block,并返回第一块block的地址
			return splitBlock(freeSpace , size);
		}

		block = getNextBlock(block);
	} while(block);

	return NULL;
}
int main()
{
	char str[300];
	while (getHead(str))
	{
		int len, i;
		char c, code[10];
		while (len = getLen())
		{
			while (true)
			{
				for (i = 0; i < len; i++)
				{
					c = readChar();
					code[i] = c;
				}

				code[i] = NULL;
				int val = hash(code);
				if (val == -1)
					break;

				putchar(str[val]);
			}
		}

		putchar('\n');
	}

	return 0;
}
void destroyList(DLinkedList* dLinkedList){
  if(dLinkedList->head != NULL){
    getHead(dLinkedList);
    dLinkedList->current = dLinkedList->head;
    while(deleteForward(dLinkedList)){};
  }
  free(dLinkedList);
}
Exemplo n.º 23
0
static SUBCLASSING *find( WNDPROC wndProc, HWND hwnd ) {

    SUBCLASSING *p = getHead( hwnd );
    while ( 0 != p && wndProc != p->wndProc ) {
        p = p->pNext;
    }
    return p;
}
Exemplo n.º 24
0
int reaction(queue_t * self){
       if (compare(self)==1){
        for(int i = getHead(self); i < getTail(self)-1;i++)
            queue_remove(self);
            queue_print(self);
    }
    return 0;
}
Exemplo n.º 25
0
int main() {
  // the list's name is "nodolista"
  insertHead(&nodolista.next, 1);
  insertTail(&nodolista.next, 6);
  insertTail(&nodolista.next, 3);
  insertTail(&nodolista.next, 8);
  insertTail(&nodolista.next, 4);
  insertTail(&nodolista.next, 7);
  insertTail(&nodolista.next, 5);
  insertTail(&nodolista.next, 9);
  insertTail(&nodolista.next, 2);
  // end
  
  //printList
  printList(nodolista.next);
  
  //getHead
  printf("\nHead:\t[ (%d) %d ]\n", getHead(nodolista.next)->index, getHead(nodolista.next)->info);
  
  //getTail
  printf("Tail:\t[ (%d) %d ]\n", getTail(nodolista.next)->index, getTail(nodolista.next)->info);
 
  // getPrev 
  int k = 0;
  printf("Insert an index to return the prev (starting from 0 to %d): ", getIndex(getTail(nodolista.next)));
  scanf("%d", &k);
  printf("The prev of value %d is: %d\n", getValue(nodolista.next, k)->info, \
  									(getPrev(nodolista.next, k) ? getPrev(nodolista.next, k)->info : 0));
  
  // search
  k = 0;
  printf("Insert a value to search into the list: ");
  scanf("%d", &k);
  printf("The search went: %s\n", (search(nodolista.next, k) ? "true" : "false"));
  
  // delete
  printf("Insert an item in the list to delete: ");
  scanf("%d", &k);
  delete(nodolista.next, k);
  printList(nodolista.next);
  
  // inserction sort
  inserctionSort(nodolista.next);
  printList(nodolista.next);
  return 0;
} //end main
Exemplo n.º 26
0
                void NReadUserByNameResponse::toStruct(_NReadUserByNameResponse & input)
                {
                    memset(&input, 0, sizeof(_NReadUserByNameResponse));

                    getHead().toStruct(input.Head);
                    getData().toStruct(input.Data);

                    input.Head.Control.Size = sizeof(_NReadUserByNameResponse);
                }
void DisjointSetInt::mergeSets(int idNode1, int idNode2){
	int head1 = getHead(idNode1);
	int head2 = getHead(idNode2);
	if (head1 == -1 || head2 == -1){
		cerr << "access invalid node in disjointSet" << endl;
	}
	else if (head1 != head2){
		if (head[head1] < head[head2]){
			head[head1] += head[head2];
			head[head2] = head1;
		}
		else {
			head[head2] += head[head1];
			head[head1] = head2;
		}
		--numSets;
	}
}
Exemplo n.º 28
0
      //! Retrieves data from the FIFO.
      //! @param buffer buffer that will hold the retrieved data.
      //! @param length amount of bytes to retrieve.
      //! @return amount of bytes retrieved.
      inline uint32_t
      get(uint8_t* buffer, uint32_t length)
      {
        uint32_t l = getHead(buffer, length);

        m_out += l;

        return l;
      }
Exemplo n.º 29
0
bool Core::isOnSnake(const int x, const int y)
{
  t_pos	head;
  
  head = getHead();
  if (x == head.x && y == head.y)
    return (true);
  return (false);
}
                void NCheckLoginCredentialsResponse::toStruct(_NCheckLoginCredentialsResponse & input)
                {
                    memset(&input, 0, sizeof(_NCheckLoginCredentialsResponse));

                    getHead().toStruct(input.Head);
                    getData().toStruct(input.Data);

                    input.Head.Control.Size = sizeof(_NCheckLoginCredentialsResponse);
                }