Exemple #1
0
int igraph_i_eigen_matrix_lapack_cmp_sm(void *extra, const void *a,
					const void *b) {
  igraph_i_eml_cmp_t *myextra=(igraph_i_eml_cmp_t *) extra;
  int *aa=(int*) a, *bb=(int*) b;
  igraph_real_t a_m=VECTOR(*myextra->mag)[*aa];
  igraph_real_t b_m=VECTOR(*myextra->mag)[*bb];

  if (MORE(a_m, b_m)) {
    return 1;
  } else if (LESS(a_m, b_m)) {
    return -1;
  } else {
    igraph_real_t a_r=VECTOR(*myextra->real)[*aa];
    igraph_real_t a_i=VECTOR(*myextra->imag)[*aa];
    igraph_real_t b_r=VECTOR(*myextra->real)[*bb];
    igraph_real_t b_i=VECTOR(*myextra->imag)[*bb];
    if (NONZERO(a_i) && ZERO(b_i))    { return -1; }
    if (ZERO(a_i)    && NONZERO(b_i)) { return  1; }
    if (LESS(a_r, b_r)) { return -1; }
    if (MORE(a_r, b_r)) { return  1; }
    if (LESS(a_i, b_i)) { return -1; }
    if (MORE(a_i, b_i)) { return  1; }
  }
  return 0;
}  
int partition(Item a[], int l, int r)
{
  int i = l;    /* left index */
  int j = r-1;      /* right index */
  Item v = a[r];  /* v is the partitioning element */

  do  
    {
      /* pre-increment sets i to l on first iteration */
      while (LESS(a[i], v))     /* scan until >= */
	{
	  ++i;
	}

      while (j>i && !(LESS(a[j],v)))
	{     /* scan until <, or until end of array */
	  --j;
	}
      if (i < j) 
	EXCH(a[i], a[j]);      
    }
  while(i < j);

  /* after partition, exchange a[i] with partition element */
  EXCH(a[i], a[r]);
  return i;
}
Exemple #3
0
static Comparison is_best_action_rp(Action *prod, Action *best)
{
  PREFER(prod->id > 1, best->id <= 1);
  LESS(get_ainit(prod), get_ainit(best));
#ifdef DAE
  LESS(duration(prod), duration(best));
#endif
  return Equal;
}
Exemple #4
0
static Comparison open_list_cmp(Node *node1, Node *node2)
{
  LESS(node1->fvalue, node2->fvalue);
#ifdef DAE
  LESS(node1->makespan, node2->makespan);
#else
  LESS(node1->length, node2->length);
#endif
  LESS(node1->id, node2->id);
  return Equal;
}
SrReal SrRay3D::distanceRaySquared(const SrRay3D& ray)const
{
	SrVector3D u = mBase - ray.mBase;
	SrReal a = mDirection.dot(mDirection);
	SrReal b = mDirection.dot(ray.mDirection);
	SrReal c = ray.mDirection.dot(ray.mDirection);
	SrReal d = mDirection.dot(u);
	SrReal e = ray.mDirection.dot(u);
	SrReal det = a*c - b*b;
	SrReal sNum,sDenom,tNum,tDenom;
	tDenom = sDenom = det;
	if( EQUAL(det,0) )
	{
		sNum = 0;
		tNum = e;
		tDenom = c;
	}
	else
	{
		sNum = b*e - c*d;
		tNum = a*e - b*d;
	}
	//check s
	if( LESS(sNum,0) )
	{
		sNum = 0;
		tNum = e;
		tDenom = c;
	}
	//check t
	if( LESS(tNum,0) )
	{
		tNum = 0;
		if( LESS(-d,0) )
		{
			sNum = 0;
		}
		else
		{
			sNum = -d;
			sDenom = a;
		}
	}
	// Parameters of nearest points on restricted domain
	SrReal s = 0  , t = 0;
	if( UNEQUAL(sDenom,0) )
		s= sNum / sDenom ;
	if( UNEQUAL(tDenom,0) )
		t = tNum / tDenom;
	SrVector3D v = u + s*mDirection - t*ray.mDirection;
	return v.dot(v);
}
Exemple #6
0
bool CLocusNameChannel::operator <(const CLocusNameChannel &x) const
{
  bool bRtn = false;
  if(m_nMinBP < x.m_nMinBP)
  {
    bRtn = m_nChannel <= x.m_nChannel;
  }
  else if(m_nChannel < x.m_nChannel)
  {
    bRtn = true;
  }
  else if(m_nChannel == x.m_nChannel)
  {
    // we already know the m_nMinBP >= x.m_nMinBP;
    // this code should never be reached
    if(m_nMinBP == x.m_nMinBP) 
    {
      if(m_nMaxBP < x.m_nMaxBP)
      {
        bRtn = true;
      }
      else if(m_nMaxBP == x.m_nMaxBP)
      {
        nwxStringLessNoCaseSort LESS;
        bRtn = LESS(m_sName,x.m_sName);
      }
    }
  }
  return bRtn;
}
Exemple #7
0
/*
 * tree_insert --
 *   Insert the item into the tree.
 *   See section 13.3, "Introduction to Algorithms", 2/e for details
 *   on the algorithm.
 */
void tree_insert(const T tree, Item item)
{
    assert(tree);

    Node *x, *y, *z;

    /*
     * z is the inserted node
     */
    z = malloc(sizeof *z);
    assert(z);
    z->item = item;

    /*
     * Traverse the tree from the root down to find a place for the
     * new node.
     */
    y = NIL(tree);
    x = tree->root;
    while (x != NIL(tree)) {
        x->size++;              // maintain the size field of each node
        y = x;
        if (LESS(KEY(item), KEY(x->item)))
            x = x->left;
        else
            x = x->right;
    }

    /*
     * now y is the parent of new inserted node z
     */
    z->parent = y;
    if (y == NIL(tree))
        tree->root = z;
    else if (LESS(KEY(z->item), KEY(y->item)))
        y->left = z;
    else
        y->right = z;
    z->left = z->right = NIL(tree);
    z->color = RED;
    z->size = 1;
    insert_fixup(tree, z);
}
Exemple #8
0
template<int axis> static inline int spatial_partition(RawArray<Perturbed2> X, Random& random) {
  // We use exact arithmetic to perform the partition, which is important in case many points are coincident
  #define LESS(i,j) (i!=j && axis_less<axis>(X[i],X[j]))

  // We partition by picking three elements at random, and running partition based on the middle element.
  const int n = X.size();
  int i0 = random.uniform<int>(0,n),
      i1 = random.uniform<int>(0,n),
      i2 = random.uniform<int>(0,n);
  if (!LESS(i0,i1)) swap(i0,i1);
  if (!LESS(i1,i2)) swap(i1,i2);
  if (!LESS(i0,i1)) swap(i0,i1);
  const auto Xmid = X[i1];
  swap(X[i1],X.back());

  // Perform the partition.  We use the version of partition from Wikipedia: http://en.wikipedia.org/wiki/Quicksort#In-place_version
  int mid = 0;
  for (const int i : range(n-1))
    if (axis_less<axis>(X[i],Xmid))
      swap(X[i],X[mid++]);
  return mid;
}
Exemple #9
0
static Node *search(const T tree, Key key)
{
    assert(tree);

    Node *node = tree->root;

    while (node != NIL(tree) && !EQ(key, KEY(node->item)))
        if (LESS(key, KEY(node->item)))
            node = node->left;
        else
            node = node->right;
    return node;
}
void insertion_sort(Item a[], int left, int right)
{
  int i, j;
  
  for(i=left; i<=right; i++)
    {
      j=i;
      while(j>left && LESS(a[j],a[j-1]))
	{
	  EXCH(a[j-1],a[j]);
	  j--;
	}
    }

}
Exemple #11
0
/*
 * tree_search --
 *   Search for the item whose key is equal to 'k'.
 */
Item tree_search(const T tree, Key k)
{
    assert(tree);

    Node *node = tree->root;

    while (node != NIL(tree) && !EQ(k, KEY(node->item)))
        if (LESS(k, KEY(node->item)))
            node = node->left;
        else
            node = node->right;
    if (node != NIL(tree))
        return node->item;
    else
        return NULLitem;
}
Exemple #12
0
Bool search_A_for_better_state( State S, int h, State *S_, int *h_ )

{

    int i, h__;
    State S__;

    lspace_start = 0;
    lspace_end = 0;
    lspace_size = 0;

    hash_state( S );

    for ( i = 0; i < gnum_A; i++ ) {
        result_to_dest( &S__, S, gA[i] );
        add_to_search_space( S__, gA[i], -1 );
    }

    while ( TRUE ) {
        if ( lspace_start == lspace_end ) {
            return FALSE;
        }
        h__ = expand_A_first_node( h );
        if ( LESS( h__, h ) ) {
            break;
        }
    }

    reset_hash_entrys();

    extract_plan_fragment();

    source_to_dest( S_, lsearch_space[lspace_start].S );
    *h_ = h__;

    if ( lsearch_space[lspace_start].depth > gmax_search_depth ) {
        gmax_search_depth = lsearch_space[lspace_start].depth;
    }

    if ( lspace_size > gmax_search_size ) {
        gmax_search_size = lspace_size;
    }

    return TRUE;

}
Exemple #13
0
longlong lesspart(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
    struct DoubleBuffer *data = (struct DoubleBuffer *) initid->ptr;
    double limit = *((double*) args->args[1]);

    double psum = 0;

    ulonglong count = 0;

    if (limit < 0) {
        *error = 1;
        return 0;
    }

    LESS();

    return (longlong) count;
}
void selection_sort(Item a[], int left, int right)
{
  int i, j;
  int min;

  for(i=left; i<right; i++)
    {
      min = i;

      for(j=i+1; j<=right; j++)
	{
	  if( LESS(a[j],a[min]) )
	    min = j;
	}      

      EXCH(a[i],a[min]);
    }
}
static int heapinsert( table_t *t, entry_t *item )
{
	entry_t *heap = t->heap;

	/*** Still room for an entry? ***/
	if (t->size < t->heapsize) {
		memcpy( &(heap[t->size]), item, sizeof(entry_t));
		siftup( t, t->size );
		t->size++;		
		return 0;
	}

	/*** Worse than the worst performer? ***/
	if ( LESS(*item, heap[0]) ) {
		return 0;
	}

	/*** Insert into heap and reheap ***/
	memcpy( &(heap[0]), item, sizeof(entry_t));
	siftdown( t, t->size, 0 );
	return 0;
}
Exemple #16
0
Bool do_best_first_search( void )

{

  static Bool fc = TRUE;
  static State S;

  BfsNode *first;
  int i, min = INFINITY;
  Bool start = TRUE;

  if ( fc ) {
    make_state( &S, gnum_ft_conn );
    fc = FALSE;
  }

  lbfs_space_head = new_BfsNode();
  lbfs_space_had = NULL;

  add_to_bfs_space( &ginitial_state, -1, NULL );

  while ( TRUE ) {
    if ( (first = lbfs_space_head->next) == NULL ) {
      printf("\n\nbest first search space empty! problem proven unsolvable.\n\n");
      return FALSE;
    }

    lbfs_space_head->next = first->next;
    if ( first->next ) {
      first->next->prev = lbfs_space_head;
    }

    if ( LESS( first->h, min ) ) {
      min = first->h;
      if ( start ) {
	printf("\nadvancing to distance : %4d", min);
	start = FALSE;
	fflush(stdout);
      } else {
	printf("\n                        %4d", min);
	fflush(stdout);
      }
    }

    if ( first->h == 0 ) {
      break;
    }

    get_A( &(first->S) );
    for ( i = 0; i < gnum_A; i++ ) {
      if ( !result_to_dest( &S, NULL, first, gA[i] ) ) continue;
      add_to_bfs_space( &S, gA[i], first );
    }

    first->next = lbfs_space_had;
    lbfs_space_had = first;
  }

  extract_plan( first );
  return TRUE;

}
Exemple #17
0
void maxflow_setup_backedges(int ne, const int *edges, int *backedges)
{
    int i, n;

    for (i = 0; i < ne; i++)
	backedges[i] = i;

    /*
     * We actually can't use the C qsort() function, because we'd
     * need to pass `edges' as a context parameter to its
     * comparator function. So instead I'm forced to implement my
     * own sorting algorithm internally, which is a pest. I'll use
     * heapsort, because I like it.
     */

#define LESS(i,j) ( (edges[2*(i)+1] < edges[2*(j)+1]) || \
		    (edges[2*(i)+1] == edges[2*(j)+1] && \
		     edges[2*(i)] < edges[2*(j)]) )
#define PARENT(n) ( ((n)-1)/2 )
#define LCHILD(n) ( 2*(n)+1 )
#define RCHILD(n) ( 2*(n)+2 )
#define SWAP(i,j) do { int swaptmp = (i); (i) = (j); (j) = swaptmp; } while (0)

    /*
     * Phase 1: build the heap. We want the _largest_ element at
     * the top.
     */
    n = 0;
    while (n < ne) {
	n++;

	/*
	 * Swap element n with its parent repeatedly to preserve
	 * the heap property.
	 */
	i = n-1;

	while (i > 0) {
	    int p = PARENT(i);

	    if (LESS(backedges[p], backedges[i])) {
		SWAP(backedges[p], backedges[i]);
		i = p;
	    } else
		break;
	}
    }

    /*
     * Phase 2: repeatedly remove the largest element and stick it
     * at the top of the array.
     */
    while (n > 0) {
	/*
	 * The largest element is at position 0. Put it at the top,
	 * and swap the arbitrary element from that position into
	 * position 0.
	 */
	n--;
	SWAP(backedges[0], backedges[n]);

	/*
	 * Now repeatedly move that arbitrary element down the heap
	 * by swapping it with the more suitable of its children.
	 */
	i = 0;
	while (1) {
	    int lc, rc;

	    lc = LCHILD(i);
	    rc = RCHILD(i);

	    if (lc >= n)
		break;		       /* we've hit bottom */

	    if (rc >= n) {
		/*
		 * Special case: there is only one child to check.
		 */
		if (LESS(backedges[i], backedges[lc]))
		    SWAP(backedges[i], backedges[lc]);

		/* _Now_ we've hit bottom. */
		break;
	    } else {
		/*
		 * The common case: there are two children and we
		 * must check them both.
		 */
		if (LESS(backedges[i], backedges[lc]) ||
		    LESS(backedges[i], backedges[rc])) {
		    /*
		     * Pick the more appropriate child to swap with
		     * (i.e. the one which would want to be the
		     * parent if one were above the other - as one
		     * is about to be).
		     */
		    if (LESS(backedges[lc], backedges[rc])) {
			SWAP(backedges[i], backedges[rc]);
			i = rc;
		    } else {
			SWAP(backedges[i], backedges[lc]);
			i = lc;
		    }
		} else {
		    /* This element is in the right place; we're done. */
		    break;
		}
	    }
	}
    }

#undef LESS
#undef PARENT
#undef LCHILD
#undef RCHILD
#undef SWAP

}
Exemple #18
0
void achieve_goals( int time )

{

  int i, j, k, ft, min_p, min_e, ef, p, op;

  if ( gcmd_line.display_info == 123 ) {
    printf("\nselecting at step %3d: ", time-1);
  }

  for ( i = 0; i < lnum_goals_at[time]; i++ ) {
    ft = lgoals_at[time][i];

    if ( gft_conn[ft].is_true == time ) {
      /* fact already added by prev now selected op
       */
      continue;
    }

    min_p = INFINITY;
    min_e = -1;
    for ( j = 0; j < gft_conn[ft].num_A; j++ ) {
      ef = gft_conn[ft].A[j];
      if ( gef_conn[ef].level != time - 1 ) continue; 
      p = 0;
      for ( k = 0; k < gef_conn[ef].num_PC; k++ ) {
	p += gft_conn[gef_conn[ef].PC[k]].level;
      }
      if ( LESS( p, min_p ) ) {
	min_p = p;
	min_e = ef;
      }
    }
    ef = min_e;
    if ( !gef_conn[ef].in_plan ) {
      gef_conn[ef].in_plan = TRUE;
      gin_plan_E[gnum_in_plan_E++] = ef;
    }

    op = gef_conn[ef].op;
    if ( gop_conn[op].is_used != time ) {
      gop_conn[op].is_used = time;
      lused_O[lnum_used_O++] = op;
      if ( gcmd_line.display_info == 123 ) {
	print_op_name( op );
	printf("\n                       ");
      }
    }

    for ( j = 0; j < gef_conn[ef].num_PC; j++ ) {
      ft = gef_conn[ef].PC[j];
      if ( gft_conn[ft].is_true == time ) {
	/* a prev at this level selected op accidently adds this precond, 
	 * so we can order that op before this one and get the precond added for free.
	 */
	continue;
      }
      if ( gft_conn[ft].is_goal ) {
	/* this fact already is a goal
	 */
	continue;
      }
      lgoals_at[gft_conn[ft].level][lnum_goals_at[gft_conn[ft].level]++] = ft;
      gft_conn[ft].is_goal = TRUE;
      if ( !gft_conn[ft].ch ) {
	lch_F[lnum_ch_F++] = ft;
	gft_conn[ft].ch = TRUE;
      }
    }

    for ( j = 0; j < gef_conn[ef].num_A; j++ ) {
      ft = gef_conn[ef].A[j];
      gft_conn[ft].is_true = time;
      if ( !gft_conn[ft].ch ) {
	lch_F[lnum_ch_F++] = ft;
	gft_conn[ft].ch = TRUE;
      }
    }
    for ( j = 0; j < gef_conn[ef].num_I; j++ ) {
      for ( k = 0; k < gef_conn[gef_conn[ef].I[j]].num_A; k++ ) {
	ft = gef_conn[gef_conn[ef].I[j]].A[k];
	gft_conn[ft].is_true = time;
	if ( !gft_conn[ft].ch ) {
	  lch_F[lnum_ch_F++] = ft;
	  gft_conn[ft].ch = TRUE;
	}
      }
    }
  }

}
Exemple #19
0
static Comparison closed_list_cmp(Node *node1, Node *node2)
{
  LESS(node1->key, node2->key);
  return (Comparison) bitarray_cmp(node1->state, node2->state, fluents_nb);
}
Exemple #20
0
static void loop(void)
{
    LINK *lnk;
    PACKET *p;
    struct timeval now,next,delta,*to;
    fd_set curr;
    char *buffer;
    int ready,size;

    if (!(buffer = malloc(sizeof(PACKET)-1+ATM_MAX_AAL5_PDU+4095))) {
        perror("buffer");
        exit(1);
    }
    buffer = (char *) (((unsigned long) buffer+4095-(sizeof(PACKET)-1)) &
                       ~4095);
    if (gettimeofday(&now,NULL) < 0) {
        perror("gettimeofday");
        exit(1);
    }
    while (1) {
        curr = in;
        for (lnk = links; lnk; lnk = lnk->next)
            if (lnk->queue) break;
        if (!lnk) to = NULL;
        else {
            for (next = lnk->queue->due; lnk; lnk = lnk->next)
                if (lnk->queue && LESS(lnk->queue->due,next))
                    next = lnk->queue->due;
            delta.tv_sec = next.tv_sec-now.tv_sec;
            delta.tv_usec = next.tv_usec-now.tv_usec;
            if (delta.tv_usec < 0) {
                delta.tv_sec--;
                delta.tv_usec += 1000000;
            }
            if (delta.tv_usec > 0) {
                delta.tv_sec++;
                delta.tv_usec -= 1000000;
            }
            if (delta.tv_sec < 0) delta.tv_sec = delta.tv_usec = 0;
            to = &delta;
        }
        if ((ready = select(fds,&curr,NULL,NULL,to)) < 0) {
            perror("select");
            exit(1);
        }
        if (gettimeofday(&now,NULL) < 0) {
            perror("gettimeofday");
            exit(1);
        }
        if (ready)
            for (lnk = links; lnk; lnk = lnk->next)
                while (1) {
                    size = read(lnk->in,buffer,ATM_MAX_AAL5_PDU);
                    if (size < 0) {
                        if (errno == EAGAIN) break;
                        else {
                            perror("read");
                            exit(1);
                        }
                    }
                    if (size > 0) {
                        if (!(p = malloc(sizeof(PACKET)-1+size))) {
                            perror("malloc");
                            exit(1);
                        }
                        memcpy(p->data,buffer,size);
                        p->size = size;
                        p->due.tv_sec = now.tv_sec+lnk->delay.tv_sec;
                        p->due.tv_usec = now.tv_usec+lnk->delay.tv_usec;
                        if (p->due.tv_usec > 1000000) {
                            p->due.tv_sec++;
                            p->due.tv_usec -= 1000000;
                        }
                        p->next = NULL;
                        if (lnk->queue) lnk->last->next = p;
                        else lnk->queue = p;
                        lnk->last = p;
                    }
                }
        for (lnk = links; lnk; lnk = lnk->next)
            while (lnk->queue && LESS(lnk->queue->due,now)) {
                p = lnk->queue;
                lnk->queue = p->next;
                if ((size = write(lnk->out,p->data,p->size)) < 0) {
                    perror("write");
                    exit(1);
                }
                if (p->size != size)
                    fprintf(stderr,"short write: %d < %d\n",size,p->size);
                free(p);
            }
    }
}
Exemple #21
0
static Int
getLessSidesArea(Area a, Area b)
{ int a_top, a_center, a_bottom, a_left, a_middle, a_right;
  int b_top, b_center, b_bottom, b_left, b_middle, b_right;
  register unsigned long mask;

  InitAreaA;
  InitAreaB;

  NormaliseArea(ax, ay, aw, ah);
  NormaliseArea(bx, by, bw, bh);

  a_top = ay;
  a_bottom = ay+ah-1;
  a_center = (a_top+a_bottom+1)/2;

  a_left = ax;
  a_right = ax+aw-1;
  a_middle = (a_left+a_right+1)/2;

  b_top = by;
  b_bottom = by+bh-1;
  b_center = (b_top+b_bottom+1)/2;

  b_left = bx;
  b_right = bx+bw-1;
  b_middle = (b_left+b_right+1)/2;

  mask = 0;

  LESS(a_top,    b_top,    mask, 01);
  LESS(a_top,    b_center, mask, 02);
  LESS(a_top,    b_bottom, mask, 04);
  LESS(a_center, b_top,    mask, 010);
  LESS(a_center, b_center, mask, 020);
  LESS(a_center, b_bottom, mask, 040);
  LESS(a_bottom, b_top,    mask, 0100);
  LESS(a_bottom, b_center, mask, 0200);
  LESS(a_bottom, b_bottom, mask, 0400);

  LESS(a_left,   b_left,   mask, 01000);
  LESS(a_left,   b_middle, mask, 02000);
  LESS(a_left,   b_right,  mask, 04000);
  LESS(a_middle, b_left,   mask, 010000);
  LESS(a_middle, b_middle, mask, 020000);
  LESS(a_middle, b_right,  mask, 040000);
  LESS(a_right,  b_left,   mask, 0100000);
  LESS(a_right,  b_middle, mask, 0200000);
  LESS(a_right,  b_right,  mask, 0400000);

  answer(toInt(mask));
}
Exemple #22
0
/*************************************
 * Jovi: update for multiple purpose *
 * BEST FIRST SEARCH IMPLEMENTATION  *
 *************************************/
Bool do_best_first_search_for_multiple_purpose ( void ) {
    
	static Bool fc_for_multiple_purpose = TRUE; /*first round*/
	static State S;
    
	BfsNode *first;
	int i, min = INFINITY;
	Bool start = TRUE;
    
	if ( fc_for_multiple_purpose ) {
		make_state( &S, gnum_ft_conn );
		S.max_F = gnum_ft_conn;
		fc_for_multiple_purpose = FALSE;
	}
    
	lbfs_space_head = new_BfsNode();
	lbfs_space_had = NULL;
    
	for ( i = 0; i < BFS_HASH_SIZE; i++ ) {
		lbfs_hash_entry[i] = NULL;
	}
    
	add_to_bfs_space_for_multiple_purpose( &ginitial_state, -1, NULL );
    
	while ( TRUE ) {
		if ( (first = lbfs_space_head->next) == NULL ) {
			printf("\n\nbest first search space empty! problem proven unsolvable.\n\n");
			return FALSE;
		}
        
		lbfs_space_head->next = first->next;
		if ( first->next ) {
			first->next->prev = lbfs_space_head;
		}
        
		if ( LESS( first->h, min ) ) {
			min = first->h;
			if ( start ) {
				printf("\nadvancing to distance : %4d", min);
				start = FALSE;
			} else {
				printf("\n                        %4d", min);
			}
            
			/*
             * modified by JC: return ealier
             */
			if(is_solved_state( &first->S )){
				printf("\nstate have seen. return!\n");
				break;
			}
		}
        
		if ( first->h == 0 ) {
			break;
		}
        
		get_A( &(first->S) );
		for ( i = 0; i < gnum_A; i++ ) {
            
			/*if(g_is_strong){*/
			/*JC: dismiss invalid actions*/
            int k;
            Bool found = FALSE;
            for(k = 0; k < gnum_IV; k++){
                if(gA[i] == gInvActs[k].act){
                    found = TRUE;
                    break;
                }
            }
			/*}*/
            
			if(found) continue;
            
			result_to_dest( &S, &(first->S), gA[i] );
			add_to_bfs_space_for_multiple_purpose( &S, gA[i], first );
		}
        
		first->next = lbfs_space_had;
		lbfs_space_had = first;
	}
    
	extract_plan( first );
	return TRUE;
}
Exemple #23
0
/*************************************************
 * FUNCTIONS FOR BREADTH FIRST SEARCH IN H SPACE *
 *************************************************/
Bool search_for_better_state_for_multiple_purpose ( State *S, int h, State *S_, int *h_ ) {
    
	static Bool first_call_for_multiple_purpose = TRUE;
	static State S__;
    
	int i, h__, depth = 0, g;
	EhcNode *tmp;
    
	if ( first_call_for_multiple_purpose ) {
		make_state( &S__, gnum_ft_conn );
		S__.max_F = gnum_ft_conn;
		first_call_for_multiple_purpose = FALSE;
	}
    
	/* don't hash states, but search nodes.
     * this way, don't need to keep states twice in memory */
	tmp = new_EhcNode();
	copy_source_to_dest( &(tmp->S), S);
	hash_ehc_node( tmp );
    
	lehc_current_end = lehc_space_head->next;
	
	for ( i = 0; i < gnum_H; i++ ) {
		int k;
		Bool found = FALSE;
		for(k = 0; k < gnum_IV; k++){
			if(same_state(&gInvActs[k].state, S) && gH[i] == gInvActs[k].act){
				found = TRUE;
				break;
			}
		}
		if(found) continue;
        
		if(is_D_action(gop_conn[gH[i]].action->name)) continue;
		
		g = result_to_dest( &S__, S, gH[i] );
		add_to_ehc_space( &S__, gH[i], NULL, g );
	}
    
	for ( i = 0; i < gnum_H; i++ ) {
		int k;
		Bool found = FALSE;
		for(k = 0; k < gnum_IV; k++){
			if(same_state(&gInvActs[k].state, S) && gH[i] == gInvActs[k].act){
				found = TRUE;
				break;
			}
		}
		if(found) continue;
        
		if(!is_D_action(gop_conn[gH[i]].action->name)) continue;
		
        /* D_cation, not in the gH */
		g = result_to_dest( &S__, S, gH[i] );
		add_to_ehc_space( &S__, gH[i], NULL, g );
	}
    
	lehc_current_start = lehc_space_head->next;
    
	while ( TRUE ) {
		if ( lehc_current_start == lehc_current_end ) {
			reset_ehc_hash_entrys();
			free( tmp );
			return FALSE;
		}
		
		if ( lehc_current_start->depth > depth ) {
			depth = lehc_current_start->depth;
			if ( depth > gmax_search_depth ) {
				gmax_search_depth = depth;
			}
			printf("[%d]", depth);
			fflush( stdout );
		}
        
		h__ = expand_first_node( h );
		if ( LESS( h__, h ) ) {
			break;
		}
	}
    
	reset_ehc_hash_entrys();
	free( tmp );
    
	extract_plan_fragment( S );
    
	source_to_dest( S_, &(lehc_current_start->S) );
	*h_ = h__;
    
	return TRUE;
}
Exemple #24
0
int runsv_main(int argc UNUSED_PARAM, char **argv)
{
	struct stat s;
	int fd;
	int r;
	char buf[256];

	INIT_G();

	dir = single_argv(argv);

	xpiped_pair(selfpipe);
	close_on_exec_on(selfpipe.rd);
	close_on_exec_on(selfpipe.wr);
	ndelay_on(selfpipe.rd);
	ndelay_on(selfpipe.wr);

	sig_block(SIGCHLD);
	bb_signals_recursive_norestart(1 << SIGCHLD, s_child);
	sig_block(SIGTERM);
	bb_signals_recursive_norestart(1 << SIGTERM, s_term);

	xchdir(dir);
	/* bss: svd[0].pid = 0; */
	if (S_DOWN) svd[0].state = S_DOWN; /* otherwise already 0 (bss) */
	if (C_NOOP) svd[0].ctrl = C_NOOP;
	if (W_UP) svd[0].sd_want = W_UP;
	/* bss: svd[0].islog = 0; */
	/* bss: svd[1].pid = 0; */
	gettimeofday_ns(&svd[0].start);
	if (stat("down", &s) != -1)
		svd[0].sd_want = W_DOWN;

	if (stat("log", &s) == -1) {
		if (errno != ENOENT)
			warn_cannot("stat ./log");
	} else {
		if (!S_ISDIR(s.st_mode)) {
			errno = 0;
			warn_cannot("stat log/down: log is not a directory");
		} else {
			haslog = 1;
			svd[1].state = S_DOWN;
			svd[1].ctrl = C_NOOP;
			svd[1].sd_want = W_UP;
			svd[1].islog = 1;
			gettimeofday_ns(&svd[1].start);
			if (stat("log/down", &s) != -1)
				svd[1].sd_want = W_DOWN;
			xpiped_pair(logpipe);
			close_on_exec_on(logpipe.rd);
			close_on_exec_on(logpipe.wr);
		}
	}

	if (mkdir("supervise", 0700) == -1) {
		r = readlink("supervise", buf, sizeof(buf));
		if (r != -1) {
			if (r == sizeof(buf))
				fatal2x_cannot("readlink ./supervise", ": name too long");
			buf[r] = 0;
			mkdir(buf, 0700);
		} else {
			if ((errno != ENOENT) && (errno != EINVAL))
				fatal_cannot("readlink ./supervise");
		}
	}
	svd[0].fdlock = xopen3("log/supervise/lock"+4,
			O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
	if (flock(svd[0].fdlock, LOCK_EX | LOCK_NB) == -1)
		fatal_cannot("lock supervise/lock");
	close_on_exec_on(svd[0].fdlock);
	if (haslog) {
		if (mkdir("log/supervise", 0700) == -1) {
			r = readlink("log/supervise", buf, 256);
			if (r != -1) {
				if (r == 256)
					fatal2x_cannot("readlink ./log/supervise", ": name too long");
				buf[r] = 0;
				fd = xopen(".", O_RDONLY|O_NDELAY);
				xchdir("./log");
				mkdir(buf, 0700);
				if (fchdir(fd) == -1)
					fatal_cannot("change back to service directory");
				close(fd);
			}
			else {
				if ((errno != ENOENT) && (errno != EINVAL))
					fatal_cannot("readlink ./log/supervise");
			}
		}
		svd[1].fdlock = xopen3("log/supervise/lock",
				O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
		if (flock(svd[1].fdlock, LOCK_EX) == -1)
			fatal_cannot("lock log/supervise/lock");
		close_on_exec_on(svd[1].fdlock);
	}

	mkfifo("log/supervise/control"+4, 0600);
	svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
	close_on_exec_on(svd[0].fdcontrol);
	svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
	close_on_exec_on(svd[0].fdcontrolwrite);
	update_status(&svd[0]);
	if (haslog) {
		mkfifo("log/supervise/control", 0600);
		svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
		close_on_exec_on(svd[1].fdcontrol);
		svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
		close_on_exec_on(svd[1].fdcontrolwrite);
		update_status(&svd[1]);
	}
	mkfifo("log/supervise/ok"+4, 0600);
	fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
	close_on_exec_on(fd);
	if (haslog) {
		mkfifo("log/supervise/ok", 0600);
		fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
		close_on_exec_on(fd);
	}
	for (;;) {
		struct pollfd x[3];
		unsigned deadline;
		char ch;

		if (haslog)
			if (!svd[1].pid && svd[1].sd_want == W_UP)
				startservice(&svd[1]);
		if (!svd[0].pid)
			if (svd[0].sd_want == W_UP || svd[0].state == S_FINISH)
				startservice(&svd[0]);

		x[0].fd = selfpipe.rd;
		x[0].events = POLLIN;
		x[1].fd = svd[0].fdcontrol;
		x[1].events = POLLIN;
		/* x[2] is used only if haslog == 1 */
		x[2].fd = svd[1].fdcontrol;
		x[2].events = POLLIN;
		sig_unblock(SIGTERM);
		sig_unblock(SIGCHLD);
		poll(x, 2 + haslog, 3600*1000);
		sig_block(SIGTERM);
		sig_block(SIGCHLD);

		while (read(selfpipe.rd, &ch, 1) == 1)
			continue;

		for (;;) {
			pid_t child;
			int wstat;

			child = wait_any_nohang(&wstat);
			if (!child)
				break;
			if ((child == -1) && (errno != EINTR))
				break;
			if (child == svd[0].pid) {
				svd[0].wstat = wstat;
				svd[0].pid = 0;
				pidchanged = 1;
				svd[0].ctrl &= ~C_TERM;
				if (svd[0].state != S_FINISH) {
					fd = open("finish", O_RDONLY|O_NDELAY);
					if (fd != -1) {
						close(fd);
						svd[0].state = S_FINISH;
						update_status(&svd[0]);
						continue;
					}
				}
				svd[0].state = S_DOWN;
				deadline = svd[0].start.tv_sec + 1;
				gettimeofday_ns(&svd[0].start);
				update_status(&svd[0]);
				if (LESS(svd[0].start.tv_sec, deadline))
					sleep(1);
			}
			if (haslog) {
				if (child == svd[1].pid) {
					svd[0].wstat = wstat;
					svd[1].pid = 0;
					pidchanged = 1;
					svd[1].state = S_DOWN;
					svd[1].ctrl &= ~C_TERM;
					deadline = svd[1].start.tv_sec + 1;
					gettimeofday_ns(&svd[1].start);
					update_status(&svd[1]);
					if (LESS(svd[1].start.tv_sec, deadline))
						sleep(1);
				}
			}
		} /* for (;;) */
		if (read(svd[0].fdcontrol, &ch, 1) == 1)
			ctrl(&svd[0], ch);
		if (haslog)
			if (read(svd[1].fdcontrol, &ch, 1) == 1)
				ctrl(&svd[1], ch);

		if (sigterm) {
			ctrl(&svd[0], 'x');
			sigterm = 0;
		}

		if (svd[0].sd_want == W_EXIT && svd[0].state == S_DOWN) {
			if (svd[1].pid == 0)
				_exit(EXIT_SUCCESS);
			if (svd[1].sd_want != W_EXIT) {
				svd[1].sd_want = W_EXIT;
				/* stopservice(&svd[1]); */
				update_status(&svd[1]);
				close(logpipe.wr);
				close(logpipe.rd);
			}
		}
	} /* for (;;) */
	/* not reached */
	return 0;
}
Exemple #25
0
void achieve_goals( int time ) {
    
    int i, j, k, ft, min_p, min_e, ef, p, op;
    
    if ( gcmd_line.display_info == 123 ) {
        printf("\nselecting at step %3d: ", time-1);
    }
    
    for ( i = 0; i < lnum_goals_at[time]; i++ ) {
        ft = lgoals_at[time][i];
        
        if ( gft_conn[ft].is_true == time ) {
            /* fact already added by prev now selected op
             */
            continue;
        }
        
        min_p = INFINITY;
        min_e = -1;
        /* fact not been added by prev*/
        /* for each fact, get the minimal level of precondtion*/
        for ( j = 0; j < gft_conn[ft].num_A; j++ ) {
            ef = gft_conn[ft].A[j];
            if ( gef_conn[ef].level != time - 1 ) continue;
            p = 0;
            for ( k = 0; k < gef_conn[ef].num_PC; k++ ) {
                p += gft_conn[gef_conn[ef].PC[k]].level;
            }
            if ( LESS( p, min_p ) ) {
                min_p = p;
                min_e = ef;
            }
        }
        ef = min_e;
        if ( !gef_conn[ef].in_plan ) {
            gef_conn[ef].in_plan = TRUE;
            gin_plan_E[gnum_in_plan_E++] = ef;
        }
        
        op = gef_conn[ef].op;
        if ( gop_conn[op].is_used != time ) {
            if ( gop_conn[op].is_used == INFINITY ) {
                lused_O[lnum_used_O++] = op;
            }
            gop_conn[op].is_used = time;
            lh++;
            if ( gcmd_line.display_info == 123 ) {
                print_op_name( op );
                printf("\n                       ");
            }
        }
        
        for ( j = 0; j < gef_conn[ef].num_PC; j++ ) {
            ft = gef_conn[ef].PC[j];
            if ( gft_conn[ft].is_true == time ) {
                /* a prev at this level selected op accidently adds this precond,
                 * so we can order that op before this one and get the precond added for free.
                 */
                continue;
            }
            if ( gft_conn[ft].is_goal ) {
                /* this fact already is a goal
                 */
                continue;
            }
            lgoals_at[gft_conn[ft].level][lnum_goals_at[gft_conn[ft].level]++] = ft;
            gft_conn[ft].is_goal = TRUE;
            if ( !gft_conn[ft].ch ) {
                lch_F[lnum_ch_F++] = ft;
                gft_conn[ft].ch = TRUE;
            }
        }
        
        for ( j = 0; j < gef_conn[ef].num_A; j++ ) {
            ft = gef_conn[ef].A[j];
            gft_conn[ft].is_true = time;
            /* NOTE: one level below a goal will only be skipped if it's true value is time-1,
             * so subgoals introduced by prev selected ops are not excluded here.
             *
             * --- neither those of the at this level prev selected oned - which we want -
             * nor those of at prev levels selected ops - which we would want to be skipped.
             *
             * --- so the ordering consraints assumed are valid but don't explore
             * the full potential.
             */
            if ( !gft_conn[ft].ch ) {
                lch_F[lnum_ch_F++] = ft;
                gft_conn[ft].ch = TRUE;
            }
        }
        for ( j = 0; j < gef_conn[ef].num_I; j++ ) {
            for ( k = 0; k < gef_conn[gef_conn[ef].I[j]].num_A; k++ ) {
                ft = gef_conn[gef_conn[ef].I[j]].A[k];
                gft_conn[ft].is_true = time;
                if ( !gft_conn[ft].ch ) {
                    lch_F[lnum_ch_F++] = ft;
                    gft_conn[ft].ch = TRUE;
                }
            }
        }
    }
}
Exemple #26
0
void game::wish()
{
 int a = 0, shift = 0;
 int line;
 char ch = '.';
 bool search = false, found = false;
 std::string pattern;
 std::string info;
 item tmp;
 tmp.corpse = mtypes[0];
 do {
  erase();
  mvprintw(0, 0, "Wish for a: ");
  if (search) {
   found = false;
   if (ch == '\n') {
    search = false;
    found = true;
    pattern = "";
    ch = '.';
   } else if (ch == KEY_BACKSPACE && pattern.length() > 0)
    pattern.erase(pattern.end() - 1);
   else
    pattern += ch;

   for (int i = 0; i < itypes.size() && !found; i++) {
    if (itypes[i]->name.find(pattern) != std::string::npos) {
     shift = i;
     a = 0;
     if (shift + 23 > itypes.size()) {
      a = shift + 23 - itypes.size();
      shift = itypes.size() - 23;
     }
     found = true;
    }
   }
   if (found)
    mvprintw(1, 0, "%s               ", pattern.c_str());
   else if (search)
    mvprintz(1, 0, c_red, "%s not found!             ", pattern.c_str());
   else
    mvprintw(1, 0, "                      ");
  } else {	// Not searching; scroll by keys
   if (ch == 'j') a++;
   if (ch == 'k') a--;
   if (ch == '/') { 
    search = true;
    pattern =  "";
   }
  }
  if (a < 0) {
   a = 0;
   shift--;
   if (shift < 0) shift = 0;
  }
  if (a > 22) {
   a = 22;
   shift++;
   if (shift + 23 > itypes.size()) shift = itypes.size() - 23;
  }
  for (int i = 1; i < LESS(24, itypes.size()); i++) {
   mvprintz(i, 40, c_white, itypes[i-1+shift]->name.c_str());
   printz(itypes[i-1+shift]->color, "%c%", itypes[i-1+shift]->sym);
  }
  tmp.make(itypes[a + shift]);
  if (tmp.is_tool())
   tmp.charges = dynamic_cast<it_tool*>(tmp.type)->max_charges;
  else if (tmp.is_ammo())
   tmp.charges = 100;
  info = tmp.info();
  line = 2;
  mvprintw(line, 1, info.c_str());
  ch = getch();
 } while (ch != '\n');
 clear();
 mvprintw(0, 0, "\nWish granted.");
 tmp.invlet = nextinv;
 u.i_add(tmp);
 advance_nextinv();
 getch();
}
Exemple #27
0
Bool search_for_better_state( State *S, int h, State *S_, int *h_ )

{

  static Bool first_call = TRUE;
  static State S__;

  int i, h__, depth = 0;
  EhcNode *tmp;

  if ( first_call ) {
    make_state( &S__, gnum_ft_conn );
    first_call = FALSE;
  }

  /* don't hash states, but search nodes.
   * this way, don't need to keep states twice in memory
   */
  tmp = new_EhcNode();
  copy_source_to_dest( &(tmp->S), S);
  hash_ehc_node( tmp );

  lehc_current_end = lehc_space_head->next;
  for ( i = 0; i < gnum_H; i++ ) {
      /* see result-to-dest params explanation at fn header
       */
    if ( !result_to_dest( &S__, tmp, NULL, gH[i] ) ) continue;
    add_to_ehc_space( &S__, gH[i], tmp );
  }
  lehc_current_start = lehc_space_head->next;

  while ( TRUE ) {  
    if ( lehc_current_start == lehc_current_end ) {
      reset_ehc_hash_entrys();
      free( tmp );
      return FALSE;
    }
    if ( lehc_current_start->depth > depth ) {
      depth = lehc_current_start->depth;
      if ( depth > gmax_search_depth ) {
	gmax_search_depth = depth;
      }
      printf("[%d]", depth);
      fflush( stdout );
      /* HACK: if this is going to more than 15, then most likely this is just a stupid
       * effect of helpful actions pruning, and we won't get anywhere anyway.
       */
      if ( depth > 15 ) {
	reset_ehc_hash_entrys();
	free( tmp );
	return FALSE; 
      }     
    }
    h__ = expand_first_node( h );
    if ( LESS( h__, h ) ) {
      break;
    }
  }

  reset_ehc_hash_entrys();
  free( tmp );

  extract_plan_fragment( S );

  source_to_dest( S_, &(lehc_current_start->S) );
  *h_ = h__;

  return TRUE;

}
Exemple #28
0
Bool search_for_better_state( State *S, int h, State *S_, int *h_ )

{

  static Bool first_call = TRUE;
  static State S__;

  int i, h__, depth = 0, g;
  EhcNode *tmp;

  if ( first_call ) {
    make_state( &S__, gnum_ft_conn );
    S__.max_F = gnum_ft_conn;
    first_call = FALSE;
  }

  /* don't hash states, but search nodes.
   * this way, don't need to keep states twice in memory
   */
  tmp = new_EhcNode();
  copy_source_to_dest( &(tmp->S), S);
  hash_ehc_node( tmp );

  lehc_current_end = lehc_space_head->next;
  for ( i = 0; i < gnum_H; i++ ) {
    g = result_to_dest( &S__, S, gH[i] );
    add_to_ehc_space( &S__, gH[i], NULL, g );
  }
  lehc_current_start = lehc_space_head->next;

  while ( TRUE ) {  
    if ( lehc_current_start == lehc_current_end ) {
      reset_ehc_hash_entrys();
      free( tmp );
      return FALSE;
    }
    if ( lehc_current_start->depth > depth ) {
      depth = lehc_current_start->depth;
      if ( depth > gmax_search_depth ) {
	gmax_search_depth = depth;
      }
      printf("[%d]", depth);
      fflush( stdout );
    }
    h__ = expand_first_node( h );
    if ( LESS( h__, h ) ) {
      break;
    }
  }

  reset_ehc_hash_entrys();
  free( tmp );

  extract_plan_fragment( S );

  source_to_dest( S_, &(lehc_current_start->S) );
  *h_ = h__;

  return TRUE;

}
Exemple #29
0
PLUS()PLUS()                  //R + + 
STRINGIZE( PLUS()PLUS() )     //R "++" 
//R 
MINUS()MINUS()                //R - - 
STRINGIZE( MINUS()MINUS() )   //R "--" 
//R 
DOT()DOT()DOT()               //R .. . 
STRINGIZE( DOT()DOT()DOT() )  //R "..." 

// the following are regressions reported by Stefan Seefeld
//R #line 43 "t_9_003.cpp"
GREATER()GREATER()            //R > > 
STRINGIZE( GREATER()GREATER() ) //R ">>" 
//R
LESS()LESS()                  //R < < 
STRINGIZE( LESS()LESS() )     //R "<<" 

#define COMMA() ,
#define AND() &
#define CHAR() char
#define STAR() *

// Make sure no whitespace gets inserted in between the operator symbols
//R #line 56 "t_9_003.cpp"
void foo(char&, char)               //R void foo(char&, char) 
void foo(char *)                    //R void foo(char *) 
void foo(char *&)                   //R void foo(char *&) 
void foo(CHAR()AND()COMMA() CHAR()) //R void foo(char&, char) 
void foo(CHAR() STAR())             //R void foo(char *) 
void foo(CHAR() STAR()AND())        //R void foo(char *&) 
SrReal SrRay3D::distanceSegmentSquared(const SrSegment3D& segment)const
{
	SrVector3D direction = segment.mPoint2 - segment.mPoint1;
	SrVector3D u = mBase - segment.mPoint1;
	SrReal a = mDirection.dot(mDirection);
	SrReal b = mDirection.dot(direction);
	SrReal c = direction.dot(direction);
	SrReal d = mDirection.dot(u);
	SrReal e = direction.dot(u);
	SrReal det = a*c - b*b;
	SrReal sNum,sDenom,tNum,tDenom;
	tDenom = sDenom = det;
	if( EQUAL(det,0) )
	{
		sNum = 0;
		tNum = e;
		tDenom = c;
	}
	else
	{
		sNum = b*e - c*d;
		tNum = a*e - b*d;
	}
	//check s
	if( LESS(sNum,0) )
	{
		sNum = 0;
		tNum = e;
		tDenom = c;
	}

	//check t
	if( LESS(tNum,0) )
	{
		tNum = 0;
		if( LESS(-d,0) )
		{
			sNum = 0;
		}
		else
		{
			sNum = -d;
			sDenom  = a;
		}
	}
	else if( GREATER(tNum,tDenom) )
	{
		tNum = tDenom;
		if( LESS((-d + b),0) )
			sNum = 0;
		else
		{
			sNum = -d + b;
			sDenom = a;
		}
	}
	// Parameters of nearest points on restricted domain
	SrReal s = 0  , t = 0;
	if( UNEQUAL(sDenom,0) )
		s= sNum / sDenom ;
	if( UNEQUAL(tDenom,0) )
		t = tNum / tDenom;

	SrVector3D v = u + s*mDirection - t*direction;
	return v.dot(v);
}