Ejemplo n.º 1
0
void over(int i)
{
    //显示结束原因
    move(0, 0);
    int j;
    for(j=0;j<COLS;j++)
        addstr(" ");
    move(0, 2);
    if(1 == i)
        addstr("Crash the wall. Game over");
    else if(2 == i)
        addstr("Crash itself. Game over");
    else if(3 == i)
        addstr("Mission Complete");
    setTicker(0);                //关闭计时器
    deleteLink();                //释放链表的空间
}
Ejemplo n.º 2
0
void removeLink(Link *firstLink, Node *node) {
       
    Link *link = firstLink;
    Link *nextLink = NULL;
    Link *removedLink = NULL;

    do {
        
        nextLink = link->nextLink;
        
        if (link->node == node) {
            removedLink = link;
            deleteLink(removedLink);
            link = nextLink;
            break;
        }

        link = nextLink;
        nextLink = NULL;
            
    } while (link->nextLink != NULL);

}
Ejemplo n.º 3
0
//  factorSparse.  Does sparse phase of factorization
//return code is <0 error, 0= finished
int CoinFactorization::factorSparseSmall()
{
  int *indexRow = indexRowU_.array();
  int *indexColumn = indexColumnU_.array();
  CoinFactorizationDouble *element = elementU_.array();
  int count = 1;
  workArea_.conditionalNew(numberRows_);
  CoinFactorizationDouble *workArea = workArea_.array();
#ifndef NDEBUG
  counter1++;
#endif
  // when to go dense
  int denseThreshold = abs(denseThreshold_);

  CoinZeroN(workArea, numberRows_);
  //get space for bit work area
  int workSize = 1000;
  workArea2_.conditionalNew(workSize);
  unsigned int *workArea2 = workArea2_.array();

  //set markRow so no rows updated
  unsigned short *markRow = reinterpret_cast< unsigned short * >(markRow_.array());
  CoinFillN(markRow, numberRows_, static_cast< unsigned short >(SMALL_UNSET));
  int status = 0;
  //do slacks first
  int *numberInRow = numberInRow_.array();
  int *numberInColumn = numberInColumn_.array();
  int *numberInColumnPlus = numberInColumnPlus_.array();
  int *startColumnU = startColumnU_.array();
  int *startColumnL = startColumnL_.array();
  if (biasLU_ < 3 && numberColumns_ == numberRows_) {
    int iPivotColumn;
    int *pivotColumn = pivotColumn_.array();
    int *nextRow = nextRow_.array();
    int *lastRow = lastRow_.array();
    for (iPivotColumn = 0; iPivotColumn < numberColumns_;
         iPivotColumn++) {
      if (numberInColumn[iPivotColumn] == 1) {
        int start = startColumnU[iPivotColumn];
        CoinFactorizationDouble value = element[start];
        if (value == slackValue_ && numberInColumnPlus[iPivotColumn] == 0) {
          // treat as slack
          int iRow = indexRow[start];
          // but only if row not marked
          if (numberInRow[iRow] > 0) {
            totalElements_ -= numberInRow[iRow];
            //take out this bit of indexColumnU
            int next = nextRow[iRow];
            int last = lastRow[iRow];

            nextRow[last] = next;
            lastRow[next] = last;
            nextRow[iRow] = numberGoodU_; //use for permute
            lastRow[iRow] = -2; //mark
            //modify linked list for pivots
            deleteLink(iRow);
            numberInRow[iRow] = -1;
            numberInColumn[iPivotColumn] = 0;
            numberGoodL_++;
            startColumnL[numberGoodL_] = 0;
            pivotColumn[numberGoodU_] = iPivotColumn;
            numberGoodU_++;
          }
        }
      }
    }
    // redo
    preProcess(4);
    CoinFillN(markRow, numberRows_, static_cast< unsigned short >(SMALL_UNSET));
  }
  numberSlacks_ = numberGoodU_;
  int *nextCount = nextCount_.array();
  int *firstCount = firstCount_.array();
  int *startRow = startRowU_.array();
  int *startColumn = startColumnU;
  //#define UGLY_COIN_FACTOR_CODING
#ifdef UGLY_COIN_FACTOR_CODING
  CoinFactorizationDouble *elementL = elementL_.array();
  int *indexRowL = indexRowL_.array();
  int *saveColumn = saveColumn_.array();
  int *nextRow = nextRow_.array();
  int *lastRow = lastRow_.array();
#endif
  double pivotTolerance = pivotTolerance_;
  int numberTrials = numberTrials_;
  int numberRows = numberRows_;
  // Put column singletons first - (if false)
  separateLinks(1, (biasLU_ > 1));
#ifndef NDEBUG
  int counter2 = 0;
#endif
  while (count <= biggerDimension_) {
#ifndef NDEBUG
    counter2++;
    int badRow = -1;
    if (counter1 == -1 && counter2 >= 0) {
      // check counts consistent
      for (int iCount = 1; iCount < numberRows_; iCount++) {
        int look = firstCount[iCount];
        while (look >= 0) {
          if (look < numberRows_) {
            int iRow = look;
            if (iRow == badRow)
              printf("row count for row %d is %d\n", iCount, iRow);
            if (numberInRow[iRow] != iCount) {
              printf("failed debug on %d entry to factorSparse and %d try\n",
                counter1, counter2);
              printf("row %d - count %d number %d\n", iRow, iCount, numberInRow[iRow]);
              abort();
            }
            look = nextCount[look];
          } else {
            int iColumn = look - numberRows;
            if (numberInColumn[iColumn] != iCount) {
              printf("failed debug on %d entry to factorSparse and %d try\n",
                counter1, counter2);
              printf("column %d - count %d number %d\n", iColumn, iCount, numberInColumn[iColumn]);
              abort();
            }
            look = nextCount[look];
          }
        }
      }
    }
#endif
    int minimumCount = COIN_INT_MAX;
    double minimumCost = COIN_DBL_MAX;

    int iPivotRow = -1;
    int iPivotColumn = -1;
    int pivotRowPosition = -1;
    int pivotColumnPosition = -1;
    int look = firstCount[count];
    int trials = 0;
    int *pivotColumn = pivotColumn_.array();

    if (count == 1 && firstCount[1] >= 0 && !biasLU_) {
      //do column singletons first to put more in U
      while (look >= 0) {
        if (look < numberRows_) {
          look = nextCount[look];
        } else {
          int iColumn = look - numberRows_;

          assert(numberInColumn[iColumn] == count);
          int start = startColumnU[iColumn];
          int iRow = indexRow[start];

          iPivotRow = iRow;
          pivotRowPosition = start;
          iPivotColumn = iColumn;
          assert(iPivotRow >= 0 && iPivotColumn >= 0);
          pivotColumnPosition = -1;
          look = -1;
          break;
        }
      } /* endwhile */
      if (iPivotRow < 0) {
        //back to singletons
        look = firstCount[1];
      }
    }
    while (look >= 0) {
      if (look < numberRows_) {
        int iRow = look;
#ifndef NDEBUG
        if (numberInRow[iRow] != count) {
          printf("failed on %d entry to factorSparse and %d try\n",
            counter1, counter2);
          printf("row %d - count %d number %d\n", iRow, count, numberInRow[iRow]);
          abort();
        }
#endif
        look = nextCount[look];
        bool rejected = false;
        int start = startRow[iRow];
        int end = start + count;

        int i;
        for (i = start; i < end; i++) {
          int iColumn = indexColumn[i];
          assert(numberInColumn[iColumn] > 0);
          double cost = (count - 1) * numberInColumn[iColumn];

          if (cost < minimumCost) {
            int where = startColumn[iColumn];
            double minimumValue = element[where];

            minimumValue = fabs(minimumValue) * pivotTolerance;
            while (indexRow[where] != iRow) {
              where++;
            } /* endwhile */
            assert(where < startColumn[iColumn] + numberInColumn[iColumn]);
            CoinFactorizationDouble value = element[where];

            value = fabs(value);
            if (value >= minimumValue) {
              minimumCost = cost;
              minimumCount = numberInColumn[iColumn];
              iPivotRow = iRow;
              pivotRowPosition = -1;
              iPivotColumn = iColumn;
              assert(iPivotRow >= 0 && iPivotColumn >= 0);
              pivotColumnPosition = i;
              rejected = false;
              if (minimumCount < count) {
                look = -1;
                break;
              }
            } else if (iPivotRow == -1) {
              rejected = true;
            }
          }
        }
        trials++;
        if (trials >= numberTrials && iPivotRow >= 0) {
          look = -1;
          break;
        }
        if (rejected) {
          //take out for moment
          //eligible when row changes
          deleteLink(iRow);
          addLink(iRow, biggerDimension_ + 1);
        }
      } else {
        int iColumn = look - numberRows;

        assert(numberInColumn[iColumn] == count);
        look = nextCount[look];
        int start = startColumn[iColumn];
        int end = start + numberInColumn[iColumn];
        CoinFactorizationDouble minimumValue = element[start];

        minimumValue = fabs(minimumValue) * pivotTolerance;
        int i;
        for (i = start; i < end; i++) {
          CoinFactorizationDouble value = element[i];

          value = fabs(value);
          if (value >= minimumValue) {
            int iRow = indexRow[i];
            int nInRow = numberInRow[iRow];
            assert(nInRow > 0);
            double cost = (count - 1) * nInRow;

            if (cost < minimumCost) {
              minimumCost = cost;
              minimumCount = nInRow;
              iPivotRow = iRow;
              pivotRowPosition = i;
              iPivotColumn = iColumn;
              assert(iPivotRow >= 0 && iPivotColumn >= 0);
              pivotColumnPosition = -1;
              if (minimumCount <= count + 1) {
                look = -1;
                break;
              }
            }
          }
        }
        trials++;
        if (trials >= numberTrials && iPivotRow >= 0) {
          look = -1;
          break;
        }
      }
    } /* endwhile */
    if (iPivotRow >= 0) {
      assert(iPivotRow < numberRows_);
      int numberDoRow = numberInRow[iPivotRow] - 1;
      int numberDoColumn = numberInColumn[iPivotColumn] - 1;

      totalElements_ -= (numberDoRow + numberDoColumn + 1);
      if (numberDoColumn > 0) {
        if (numberDoRow > 0) {
          if (numberDoColumn > 1) {
            //  if (1) {
            //need to adjust more for cache and SMP
            //allow at least 4 extra
            int increment = numberDoColumn + 1 + 4;

            if (increment & 15) {
              increment = increment & (~15);
              increment += 16;
            }
            int increment2 =

              (increment + COINFACTORIZATION_BITS_PER_INT - 1) >> COINFACTORIZATION_SHIFT_PER_INT;
            int size = increment2 * numberDoRow;

            if (size > workSize) {
              workSize = size;
              workArea2_.conditionalNew(workSize);
              workArea2 = workArea2_.array();
            }
            bool goodPivot;
#ifndef UGLY_COIN_FACTOR_CODING
            //branch out to best pivot routine
            goodPivot = pivot(iPivotRow, iPivotColumn,
              pivotRowPosition, pivotColumnPosition,
              workArea, workArea2,
              increment2, markRow,
              SMALL_SET);
#else
#define FAC_SET SMALL_SET
#include "CoinFactorization.hpp"
#undef FAC_SET
#undef UGLY_COIN_FACTOR_CODING
#endif
            if (!goodPivot) {
              status = -99;
              count = biggerDimension_ + 1;
              break;
            }
          } else {
            if (!pivotOneOtherRow(iPivotRow, iPivotColumn)) {
              status = -99;
              count = biggerDimension_ + 1;
              break;
            }
          }
        } else {
/* Apaga um bitaite associado a um hashtag.		    */
void deleteBitaite(queue_ptr list) {
	link aux = list -> begin;
	list -> begin = list -> begin -> next;
	deleteLink(aux);
	list -> cont--;
}
Ejemplo n.º 5
0
void work(struct serversocket* ss) {
	
	int server_sockfd = ss->sockfd;
	int client_sockfd;
	int len;
	int buf[BUFSIZE];//数据传输缓冲区
	socklen_t sin_size = sizeof(struct sockaddr_in);
	struct sockaddr_in my_addr = ss->server_addr;//服务器端网络地址结构体
	struct sockaddr_in remote_addr;//客户端网络地址结构体


	// 创建一个epoll句柄
	int epoll_fd = epoll_create(MAX_EVENTS);
	if(epoll_fd==-1) {
		perror("epoll_create failed");
		writelog("创建epoll句柄失败");
		exit(EXIT_FAILURE);
	}
	writelog("创建epoll句柄");

   	struct epoll_event ev;// epoll事件结构体
	struct epoll_event events[MAX_EVENTS];// 事件监听队列
	ev.events=EPOLLIN;
	ev.data.fd=server_sockfd;
	
	//向epoll注册server_sockfd监听事件
	if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,server_sockfd,&ev)==-1) {
		perror("epll_ctl:server_sockfd register failed");
		writelog("向epoll注册server_sockfd监听事件失败");
		exit(EXIT_FAILURE);
	}
	writelog("向epoll注册server_sockfd监听事件");



	int nfds;// epoll监听事件发生的个数

    // 循环接受客户端请求	
    while(1)
	{
		// 等待事件发生
		printf("服务器就绪...\n");
		nfds=epoll_wait(epoll_fd,events,MAX_EVENTS,-1);
		if(nfds==-1) {
			perror("start epoll_wait failed");
			writelog("start epoll_wait failed");
			exit(EXIT_FAILURE);
		}

		printf("事件数量:%d\n", nfds);
		int i;
		for(i=0;i<nfds;i++)
		{
			if(events[i].data.fd == server_sockfd) {
				printf("客户端有新的连接请求\n");
	  		 	// 等待客户端连接请求到达
        		int tmp_sockfd;
				if((tmp_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0) {   
					perror("accept client_sockfd failed");   
	 				exit(EXIT_FAILURE);
				}
				printf("sockfd: %d\n服务端 accept 客户端\n", tmp_sockfd);
				char *ip = NULL;
				int port = 0;
				
				printf("判断客户端是否重复\n");
				//printf("获取客户端ip和端口号\n");
				
				if(getpeername(tmp_sockfd, (struct sockaddr*)&remote_addr, &sin_size) == 0) {
					ip = inet_ntoa(remote_addr.sin_addr);
					port = ntohs(remote_addr.sin_port);
					//printf("ip : %s\nport : %d\n", inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
				}
				
				//printf("ip : %s\nport : %d\n", ip, port);
				if(checkLink(ip, port) == 1) {
					printf("连接已经存在\n");
					close(tmp_sockfd);
					continue;
				}
				client_sockfd = tmp_sockfd;

				
				printf("保存客户端连接\n");
				if(holdLink(client_sockfd, ip, port) == 0) {//添加连接到连接池
					printf("保存失败\n");
					continue;
				}
				
				// 向epoll注册client_sockfd监听事件
				ev.events=EPOLLIN;
				ev.data.fd=client_sockfd;
				if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,client_sockfd,&ev)==-1) {
	  				perror("epoll_ctl:client_sockfd register failed");
					exit(EXIT_FAILURE);
				}
	 			printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));
			}
			// 客户端有数据发送过来
			else if(events[i].events & EPOLLIN) {
				printf("发数据的客户端sockfd: %d\n", events[i].data.fd);
				client_sockfd = events[i].data.fd;
				buf[0] = buf[1] = INF; 
				len = read(client_sockfd, &buf, sizeof(bao));
				//len=recv(client_sockfd,buf,BUFSIZE,0);
				if(len<0) {
					perror( "receive from client failed");
					epoll_ctl(epoll_fd,EPOLL_CTL_DEL, client_sockfd, &ev);
	 				exit(EXIT_FAILURE);
				}

				//客户端已经断开连接
				if(len == 0) {
					deleteLink(client_sockfd);
					epoll_ctl(epoll_fd,EPOLL_CTL_DEL, client_sockfd, &ev);
					close(client_sockfd);
					printf("客户端断开连接\n");
					continue;
				}
				printf("receive two numbers from client: %d %d\n",buf[0], buf[1]);
				int ans = buf[0] + buf[1];
				//char str[] = "I have received your message.";
				write(client_sockfd, &ans, sizeof(ans));
				//send(client_sockfd, str, sizeof(str),0);
			}
			// 服务器端发数据
			else if(events[i].events & EPOLLOUT) {
				printf("Server send sum to %d\n",  events[i].data.fd);
				//目前不需要该业务逻辑
			}
		}
	}   
}