Exemple #1
0
struct node *merge_lists(struct node *a, struct node *b)
{
    if(!a)return b;
    if(!b)return a;
    if(a->data>b->data)
        return merge_lists(b,a);
    a->next=merge_lists(a->next,b);
    return a;
}
Exemple #2
0
/** @brief Sorts a list using the bubble sort technique
 *
 *  @param list the list to be sorted
 */
void sort(List *list){
	List *n_list = new_empty_list();
	while(is_empty(list) != 1){
		append(n_list, remove_by_idx(list,get_min(list))->content);
	}
	merge_lists(list, n_list);
}
int main(int argc, char** argv)
{
  if (argc < 3)
  {
    std::cout << "usage: " << argv[0] << " <list A values> : <list B values" << std::endl;
    return 1;
  }

  ListNode* head_a = new ListNode(std::atoi(argv[1]));
  int i = 2;

  // build list A
  ListNode* p = head_a;
  while(i < argc && argv[i][0] != ':')
  {
    int val = std::atoi(argv[i]);

    ListNode* c = new ListNode(val);
    p->next = c;
    p = c;

    i++;
  }

  i++;

  ListNode* head_b = nullptr;

  if (i < argc)
  {
    head_b = new ListNode(std::atoi(argv[i]));
  }

  i++;

  // build list B
  p = head_b;
  while(i < argc && argv[i][0] != ':')
  {
    int val = std::atoi(argv[i]);

    ListNode* c = new ListNode(val);
    p->next = c;
    p = c;

    i++;
  }

  std::cout << "list A: ";
  print_list(head_a);

  std::cout << "list B: ";
  print_list(head_b);

  ListNode* merged_head = merge_lists(head_a, head_b);

  print_list(merged_head);

  return 0;
}
// merge sort
static list_t *sort_fortunes(list_t *head)
{
	list_t **new_head;

	do {
		new_head = &head;

		for (list_t *iter = head; iter;) {
			list_t * const tail1 = get_sorted_sublist(iter);
			list_t * const head2 = tail1->next;

			if (!head2) {
				*new_head = iter;
				break;
			}

			list_t * const tail2 = get_sorted_sublist(head2);
			list_t * const head1 = iter;

			iter = tail2->next;
			tail1->next = NULL;
			tail2->next = NULL;
			*new_head = merge_lists(head1, head2);
			new_head = tail1->next ? &tail2->next : &tail1->next;
		}
	} while (new_head != &head);

	return head;
}
Exemple #5
0
/**
   \brief Select features by polygon

   \param[in] Map vector map
   \param[in] type feature type
   \param[in] poly polygon coordinates
   \param[in,out] List list of selected features
   
   \return number of selected lines
*/
int sel_by_polygon(struct Map_info *Map,
		   int type, struct line_pnts *Polygon, struct ilist *List)
{
    struct ilist *List_tmp;

    if (first_selection) {
	List_tmp = List;
	first_selection = 0;
    }
    else {
	List_tmp = Vect_new_list();
    }

    /* no isles */
    Vect_select_lines_by_polygon(Map, Polygon, 0, NULL, type, List_tmp);

    G_debug(1, "  %d lines selected (by polygon)", List_tmp->n_values);

    /* merge lists (only duplicate items) */
    if (List_tmp != List) {
	merge_lists(List, List_tmp);
	Vect_destroy_list(List_tmp);
    }

    return List->n_values;
}
Exemple #6
0
static t_list	*merge_lists(t_list *first, t_list *second)
{
	if (!first)
		return (second);
	if (!second)
		return (first);
	if (ft_strcmp((char*)first->content, (char*)second->content) <= 0)
	{
		first->next = merge_lists(first->next, second);
		return (first);
	}
	else
	{
		second->next = merge_lists(first, second->next);
		return (second);
	}
}
Exemple #7
0
t_list			*merge_sort_matches(t_list *first)
{
	t_list	*second;

	if (!first || !first->next)
		return (first);
	second = split_list(first);
	first = merge_sort_matches(first);
	second = merge_sort_matches(second);
	return (merge_lists(first, second));
}
Exemple #8
0
/**
   \brief Select features by coordinates
 
   \param[in] Map vector map
   \param[in] type feature type
   \param[in] coords coordinates GRASS parameters
   \param[in] thresh threshold value for searching
   \param[in,out] List list of selected features

   \return number of selected lines
*/
int sel_by_coordinates(struct Map_info *Map,
		       int type, struct line_pnts *coords, double thresh,
		       struct ilist *List)
{
    int i;
    double east, north, maxdist;

    struct ilist *List_tmp, *List_in_box;
    struct line_pnts *box;

    if (first_selection) {
	List_tmp = List;
	first_selection = 0;
    }
    else {
	List_tmp = Vect_new_list();
    }

    box = Vect_new_line_struct();
    List_in_box = Vect_new_list();

    if (thresh < 0)
	maxdist = max_distance(thresh);
    else
	maxdist = thresh;

    for (i = 0; i < coords->n_points; i++) {
	east = coords->x[i];
	north = coords->y[i];

	coord2bbox(east, north, maxdist, box);

	Vect_select_lines_by_polygon(Map, box, 0, NULL, type, List_in_box);

	if (List_in_box->n_values > 0)
	    Vect_list_append_list(List_tmp, List_in_box);
    }

    G_debug(1, "  %d lines selected (by coordinates)", List_tmp->n_values);

    /* merge lists (only duplicate items) */
    if (List_tmp != List) {
	merge_lists(List, List_tmp);
	Vect_destroy_list(List_tmp);
    }

    Vect_destroy_line_struct(box);
    Vect_destroy_list(List_in_box);

    return List->n_values;
}
typename SortedList<T, Pred>::Node* SortedList<T, Pred>::merge_sort_helper(typename SortedList<T, Pred>::Node* current, unsigned chunk_size, const Sorter &sorter)
{
	if (current != tail_ && chunk_size > 1)
	{
		SortedList<T, Pred>::Node* temp = current;
		advanceNode(temp, chunk_size / 2);
		// Mergesort the left, mergesort the right, and merge the results!
		return(merge_lists(merge_sort_helper(current, chunk_size / 2, sorter),
				   chunk_size / 2,
				   merge_sort_helper(temp, chunk_size - (chunk_size / 2), sorter),
				   chunk_size - (chunk_size / 2),
				   sorter));
	}
	
	return(current);
}
Exemple #10
0
/**
   \brief Select features by category
 
   \param[in] Map vector map
   \param[in] cl_orig original list of categories (previously selected)
   \param[in] layer layer number
   \param[in] type feature type
   \param[in] cat category string
   \param[in,out] List list of selected features

   \return number of selected lines
*/
int sel_by_cat(struct Map_info *Map, struct cat_list *cl_orig,
	       int layer, int type, char *cats, struct ilist *List)
{
    struct ilist *List_tmp, *List_tmp1;
    struct cat_list *cl;

    int i, cat;

    if (first_selection || cl_orig) {
	List_tmp = List;
	first_selection = 0;
    }
    else {
	List_tmp = Vect_new_list();
    }

    List_tmp1 = Vect_new_list();

    if (cl_orig == NULL) {
	cl = Vect_new_cat_list();

	Vect_str_to_cat_list(cats, cl);
    }
    else {
	cl = cl_orig;
    }

    for (i = 0; i < cl->n_ranges; i++) {
	for (cat = cl->min[i]; cat <= cl->max[i]; cat++) {
	    Vect_cidx_find_all(Map, layer, type, cat, List_tmp1);
	    Vect_list_append_list(List_tmp, List_tmp1);
	}
    }

    G_debug(1, "  %d lines selected (by category)", List_tmp->n_values);

    /* merge lists (only duplicate items) */
    if (List_tmp != List) {
	merge_lists(List, List_tmp);
	Vect_destroy_list(List_tmp);
    }

    Vect_destroy_list(List_tmp1);

    return List->n_values;
}
Exemple #11
0
/**
   \brief Select features by id

   \param[in] Map vector map
   \param[in] type feature type
   \param[in] ids ids list
   \param[in,out] List list of selected features
 
   \return number of selected lines
*/
int sel_by_id(struct Map_info *Map, int type, char *ids, struct ilist *List)
{
    int i;
    int num, id;
    struct cat_list *il;	/* NOTE: this is not cat list, but list of id's */
    struct ilist *List_tmp;

    if (first_selection) {
	List_tmp = List;
	first_selection = 0;
    }
    else {
	List_tmp = Vect_new_list();
    }

    il = Vect_new_cat_list();
    Vect_str_to_cat_list(ids, il);

    num = Vect_get_num_lines(Map);

    for (i = 0; i < il->n_ranges; i++) {
	for (id = 1; id <= num; id++) {
	    if (!(Vect_read_line(Map, NULL, NULL, id) & type)) {
		continue;
	    }
	    if (id >= il->min[i] && id <= il->max[i]) {
		Vect_list_append(List_tmp, id);
	    }
	}
    }

    G_debug(1, "  %d lines selected (by id)", List_tmp->n_values);

    /* merge lists (only duplicate items) */
    if (List_tmp != List) {
	merge_lists(List, List_tmp);
	Vect_destroy_list(List_tmp);
    }

    Vect_destroy_cat_list(il);

    return List->n_values;
}
Exemple #12
0
/**
   \brief Select features by bbox
   
   \param[in] Map vector map
   \param[in] type feature type
   \param[in] bbox_opt bounding boxes
   \param[in,out] List list of selected features

   \return number of selected lines
*/
int sel_by_bbox(struct Map_info *Map,
		int type, double x1, double y1, double x2, double y2,
		struct ilist *List)
{
    BOUND_BOX bbox;

    struct ilist *List_tmp;

    if (first_selection) {
	List_tmp = List;
	first_selection = 0;
    }
    else {
	List_tmp = Vect_new_list();
    }

    /* bounding box */
    bbox.N = y1 < y2 ? y2 : y1;
    bbox.S = y1 < y2 ? y1 : y2;
    bbox.W = x1 < x2 ? x1 : x2;
    bbox.E = x1 < x2 ? x2 : x1;
    bbox.T = PORT_DOUBLE_MAX;
    bbox.B = -PORT_DOUBLE_MAX;

    Vect_select_lines_by_box(Map, &bbox, type, List_tmp);

    G_debug(1, "  %d lines selected (by bbox)", List_tmp->n_values);

    /* merge lists (only duplicate items) */
    if (List_tmp != List) {
	merge_lists(List, List_tmp);
	Vect_destroy_list(List_tmp);
    }

    return List->n_values;
}
Exemple #13
0
/**
   \brief Select vector features

   \param[in] Map vector map
   \param[in] action_mode tool
   \param[in] params GRASS parameters
   \param[in] List list of selected features
   
   \return list of newly selected features
*/
struct ilist *select_lines(struct Map_info *Map, enum mode action_mode,
			   struct GParams *params, double *thresh,
			   struct ilist *List)
{
    int layer, type;

    layer = atoi(params->fld->answer);
    type = Vect_option_to_types(params->type);

    /* select by id's */
    if (params->id->answer != NULL) {
	sel_by_id(Map, type, params->id->answer, List);
    }

    /* select by category (ignore tools catdel and catadd) */
    if ((action_mode != MODE_CATADD && action_mode != MODE_CATDEL) &&
	params->cat->answer != NULL) {
	sel_by_cat(Map, NULL, layer, type, params->cat->answer, List);
    }

    /* select by coordinates (+threshold) */
    if (params->coord->answer != NULL) {
	int i;
	double east, north;
	struct line_pnts *coords;

	coords = Vect_new_line_struct();
	i = 0;
	while (params->coord->answers[i]) {
	    east = atof(params->coord->answers[i]);
	    north = atof(params->coord->answers[i + 1]);
	    Vect_append_point(coords, east, north, 0.0);
	    i += 2;
	}

	G_verbose_message(_("Threshold value for coordinates is %.2f"),
			  thresh[THRESH_COORDS]);
	sel_by_coordinates(Map, type, coords, thresh[THRESH_COORDS], List);

	Vect_destroy_line_struct(coords);
    }

    /* select by bbox */
    if (params->bbox->answer != NULL) {
	struct line_pnts *bbox;
	double x1, y1, x2, y2;

	bbox = Vect_new_line_struct();

	x1 = atof(params->bbox->answers[0]);
	y1 = atof(params->bbox->answers[1]);
	x2 = atof(params->bbox->answers[2]);
	y2 = atof(params->bbox->answers[3]);

	Vect_append_point(bbox, x1, y1, -PORT_DOUBLE_MAX);
	Vect_append_point(bbox, x2, y1, PORT_DOUBLE_MAX);
	Vect_append_point(bbox, x2, y2, -PORT_DOUBLE_MAX);
	Vect_append_point(bbox, x1, y2, PORT_DOUBLE_MAX);
	Vect_append_point(bbox, x1, y1, -PORT_DOUBLE_MAX);

	/* sel_by_bbox not used */
	/*
	   sel_by_bbox(Map,
	   type, x1, y1, x2, y2,
	   List);
	 */
	sel_by_polygon(Map, type, bbox, List);

	Vect_destroy_line_struct(bbox);
    }

    /* select by polygon  */
    if (params->poly->answer != NULL) {
	int i;
	struct line_pnts *Polygon;

	Polygon = Vect_new_line_struct();

	for (i = 0; params->poly->answers[i]; i += 2) {
	    Vect_append_point(Polygon,
			      atof(params->poly->answers[i]),
			      atof(params->poly->answers[i + 1]), 0.0);
	}

	/* if first and last point of polygon does not match */
	if (atof(params->poly->answers[i - 1]) !=
	    atof(params->poly->answers[0])) {
	    Vect_append_point(Polygon, atof(params->poly->answers[0]),
			      atof(params->poly->answers[1]), 0.0);
	}

	sel_by_polygon(Map, type, Polygon, List);

	Vect_destroy_line_struct(Polygon);
    }

    /* select by where statement */
    if (params->where->answer != NULL) {
	sel_by_where(Map, layer, type, params->where->answer, List);
    }

    /* selecy by query */
    if (params->query->answer != NULL) {
	int query_type;
	struct ilist *List_tmp;

	if (first_selection) {
	    List_tmp = List;
	    first_selection = 0;
	}
	else {
	    List_tmp = Vect_new_list();
	}

	query_type = QUERY_UNKNOWN;
	if (strcmp(params->query->answer, "length") == 0) {
	    query_type = QUERY_LENGTH;
	}
	else if (strcmp(params->query->answer, "dangle") == 0) {
	    query_type = QUERY_DANGLE;
	}

	G_verbose_message(_("Threshold value for querying is %.2f"),
			  thresh[THRESH_QUERY]);
	Vedit_select_by_query(Map, type, layer, thresh[THRESH_QUERY],
			      query_type, List_tmp);

	/* merge lists (only duplicate items) */
	if (List_tmp != List) {
	    merge_lists(List, List_tmp);
	    Vect_destroy_list(List_tmp);
	}
    }

    if (params->reverse->answer) {
	reverse_selection(Map, type, &List);
    }

    G_message(_("%d of %d features selected from vector map <%s>"),
	      List->n_values,
	      Vect_get_num_lines(Map), Vect_get_full_name(Map));

    return List;
}
Exemple #14
0
void analize_tree(TREEPTR root, short condition, short reg, int level)
{
    char    neigh[10],path[10];
    PATHPTR path_list=NULL,
	    neigh_list=NULL,
	    breath_h=NULL,
	    breath_q=NULL;
    short   length, merged, end;
    TREEPTR node,neigh_node;
    MERGEPTR node1, node2;


    initialize_label(path,0);
    node=root;

    end=False;
    while (!end) {
	node=get_leaf(path);

	/*Compute merge operations*/
	if (node->region!=NULL) {    /*A leaf has been found*/
	    /*Merge its neighbours if condition is satisfied*/
	    if (node->included!=yes) {
		/*Add a new element to the list of merged regions*/
		add_new_merged_region(&merged_region_list);
		/*Insert the region in the list of merged regions*/
		insert_leaf_list(&merged_region_list, node);
		node->included=yes;
		/*Actualize the pointer to the merged region which includes it*/
		node->pmerge = merged_region_list;
	    }
	    /* Obtain the list of neighbours of the node referenced by path */
	    get_neighbours(&neigh_list,root,path);
	    while (neigh_list!=NULL) {
		initialize_label(neigh,0);
		/* Get and delete the first element in the list of neighbours */
		delete_first(&neigh_list,neigh);
		neigh_node=get_leaf(neigh);
		/* Get the pointers to the candidate regions */
		node1=*get_location(node);
		node2=*get_location(neigh_node);
		if ( node1!=node2 ) {
		    /* The neighbour region doesn't belong to a merged region yet*/
		    if (neigh_node->included!=yes) {
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Insert the neighbour in the same list than path */
			    /* All the regions that are homogeneous belong to */
			    /* the same list */ 
			    insert_leaf_list(get_location(node),neigh_node);
			    neigh_node->included=yes;
			    /* Set pmerge field to point to the list */
			    /* where it has been inserted */
			    neigh_node->pmerge=*get_location(node);
			}
		    } else {  /* Each region belongs to a different merged region */
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Check whether both merged regions are homogeneous */
			    if (abs( node1->mean - node2->mean ) < condition)
				/* Merge both lists of merged regions */
				merge_lists(&node1,&node2);
			}
		    }
		}
	    }
	} else {
	    length = strlen(path);
	    /* Insert new nodes to be analised using the breath first method */
	    path[length]='0'; insert_path(&breath_h,&breath_q,path);
	    path[length]='1'; insert_path(&breath_h,&breath_q,path);
	    path[length]='2'; insert_path(&breath_h,&breath_q,path);
	    path[length]='3'; insert_path(&breath_h,&breath_q,path);
	}
	/* Take a node from the list to analize it if there are nodes left */
	if (breath_h==NULL) end=True;
	    else delete_first(&breath_h,path);
    }
}
void intersectionList_reduce(void* key, void* left, void* right) {
  merge_lists((IntersectionEventList*) left, (IntersectionEventList*) right);
}
Exemple #16
0
static bool
opt_expr (struct predicate **eval_treep)
{
  struct predlist regex_list={NULL,NULL}, name_list={NULL,NULL};
  struct predlist cbo_list[NumEvaluationCosts];
  int i;
  struct predicate *curr;
  struct predicate **prevp;	/* Address of `curr' node. */
  struct predicate **last_sidep; /* Last predicate with side effects. */
  PRED_FUNC pred_func;
  enum predicate_type p_type;
  bool has_side_effects = false; /* Return value. */
  enum predicate_precedence prev_prec, /* precedence of last BI_OP in branch */
			    biop_prec; /* topmost BI_OP precedence in branch */

  if (eval_treep == NULL || *eval_treep == NULL)
    return (false);

  for (i=0; i<NumEvaluationCosts; i++)
    predlist_init (&cbo_list[i]);

  /* Set up to normalize tree as a left-linked list of ANDs or ORs.
     Set `curr' to the leftmost node, `prevp' to its address, and
     `pred_func' to the predicate type of its parent. */
  prevp = eval_treep;
  prev_prec = AND_PREC;
  curr = *prevp;
  while (curr->pred_left != NULL)
    {
      prevp = &curr->pred_left;
      prev_prec = curr->p_prec;	/* must be a BI_OP */
      curr = curr->pred_left;
    }

  /* Link in the appropriate BI_OP for the last expression, if needed. */
  if (curr->p_type != BI_OP)
    set_new_parent (curr, prev_prec, prevp);

  if (options.debug_options & (DebugExpressionTree|DebugTreeOpt))
    {
      /* Normalized tree. */
      fprintf (stderr, "Normalized Eval Tree:\n");
      print_tree (stderr, *eval_treep, 0);
    }

  /* Rearrange the predicates. */
  prevp = eval_treep;
  biop_prec = NO_PREC; /* not COMMA_PREC */
  if ((*prevp) && (*prevp)->p_type == BI_OP)
    biop_prec = (*prevp)->p_prec;
  while ((curr = *prevp) != NULL)
    {
      /* If there is a BI_OP of different precedence from the first
	 in the pred_left chain, create a new parent of the
	 original precedence, link the new parent to the left of the
	 previous and link CURR to the right of the new parent.
	 This preserves the precedence of expressions in the tree
	 in case we rearrange them. */
      if (curr->p_type == BI_OP)
	{
          if (curr->p_prec != biop_prec)
	    curr = set_new_parent (curr, biop_prec, prevp);
	}

      /* See which predicate type we have. */
      p_type = curr->pred_right->p_type;
      pred_func = curr->pred_right->pred_func;


      switch (p_type)
	{
	case NO_TYPE:
	case PRIMARY_TYPE:
	  /* Don't rearrange the arguments of the comma operator, it is
	     not commutative.  */
	  if (biop_prec == COMMA_PREC)
	    break;

	  /* If this predicate has no side effects, consider reordering it. */
	  if (!curr->pred_right->side_effects)
	    {
	      bool reorder;

	      /* If it's one of our special primaries, move it to the
		 front of the list for that primary. */
	      if (predicate_is_cost_free (curr->pred_right))
		{
		  if (options.debug_options & DebugTreeOpt)
		    {
		      fprintf (stderr, "-O%d: promoting cheap predicate ",
			       (int)options.optimisation_level);
		      print_predicate (stderr, curr->pred_right);
		      fprintf (stderr, " into name_list\n");
		    }
		  predlist_insert (&name_list, curr, prevp);
		  continue;
		}

	      if (pred_func == pred_regex)
		{
		  predlist_insert (&regex_list, curr, prevp);
		  continue;
		}

	      reorder = ((options.optimisation_level > 1)
			 && (NeedsType == curr->pred_right->p_cost
			     || NeedsInodeNumber == curr->pred_right->p_cost)
			 && !curr->pred_right->need_stat) ||
		(options.optimisation_level > 2);

	      if (reorder)
		{
		  if (options.debug_options & DebugTreeOpt)
		    {
		      fprintf (stderr, "-O%d: categorising predicate ",
			       (int)options.optimisation_level);
		      print_predicate (stderr, curr->pred_right);
		      fprintf (stderr, " by cost (%s)\n",
			       cost_name(curr->pred_right->p_cost));
		    }
		  predlist_insert (&cbo_list[curr->pred_right->p_cost], curr, prevp);
		  continue;
		}
	    }

	  break;

	case UNI_OP:
	  /* For NOT, check the expression trees below the NOT. */
	  curr->pred_right->side_effects
	    = opt_expr (&curr->pred_right->pred_right);
	  break;

	case BI_OP:
	  /* For nested 'AND' or 'OR', recurse (AND/OR form layers on
	     the left of the tree), and continue scanning this level
	     of 'AND' or 'OR'. */
	  curr->pred_right->side_effects = opt_expr (&curr->pred_right);
	  break;

	  /* At this point, get_expr and scan_rest have already removed
	     all of the user's parentheses. */

	default:
	  error (EXIT_FAILURE, 0, _("oops -- invalid expression type!"));
	  break;
	}

      if (curr->pred_right->side_effects == true)
	{
	  last_sidep = prevp;

	  /* Incorporate lists and reset list pointers for this group.  */
	  merge_lists (cbo_list, NumEvaluationCosts, &name_list, &regex_list, last_sidep);
	  has_side_effects = true;
	}

      prevp = &curr->pred_left;
    }

  /* Do final list merges. */
  last_sidep = prevp;
  merge_lists (cbo_list, NumEvaluationCosts, &name_list, &regex_list, last_sidep);
  return has_side_effects;
}
Exemple #17
0
/**
   \brief Select features according to SQL where statement

   \param[in] Map vector map
   \param[in] layer layer number
   \param[in] type feature type
   \param[in] where 'where' statement
   \param[in,out] List list of selected features
 
   \return number of selected lines
*/
int sel_by_where(struct Map_info *Map,
		 int layer, int type, char *where, struct ilist *List)
{
    struct cat_list *cat_list;
    struct ilist *List_tmp;
    struct field_info *Fi;
    dbDriver *driver;
    dbHandle handle;

    int *cats, ncats;

    if (first_selection) {
	List_tmp = List;
	first_selection = 0;
    }
    else {
	List_tmp = Vect_new_list();
    }

    cat_list = Vect_new_cat_list();

    if (layer < 1) {
	G_fatal_error(_("Layer must be > 0 for 'where'"));
    }

    Fi = Vect_get_field(Map, layer);

    if (!Fi) {
	G_fatal_error(_("Database connection not defined for layer %d"),
		      layer);
    }

    driver = db_start_driver(Fi->driver);

    if (!driver)
	G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);

    db_init_handle(&handle);

    db_set_handle(&handle, Fi->database, NULL);

    if (db_open_database(driver, &handle) != DB_OK)
	G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
		      Fi->database, Fi->driver);

    ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats);

    db_close_database(driver);
    db_shutdown_driver(driver);

    Vect_array_to_cat_list(cats, ncats, cat_list);

    /* free array of cats */
    if (ncats >= 0)
	G_free(cats);

    sel_by_cat(Map, cat_list, layer, type, NULL, List_tmp);

    G_debug(1, "  %d lines selected (by where)", List_tmp->n_values);

    /* merge lists (only duplicate items) */
    if (List_tmp != List) {
	merge_lists(List, List_tmp);
	Vect_destroy_list(List_tmp);
    }

    Vect_destroy_cat_list(cat_list);

    return List->n_values;
}
Exemple #18
0
int
EG_bdry_graph(Row_hdr *row_hdr, int rowh_dim, int num_cols, Node *node, int num_nodes, int clump_id)
{
  float bottom;
  int i;
  int j;
  float left;
  int *list;
  int list_ct = 0;
  int list_size;
  int *next_list;
  int next_list_ct = 0;
  int node_ct = 0;
  int *previous_list;
  int previous_list_ct = 0;
  float right;
  int *temp_list;
  float top;

  list_size = 2*num_cols; /* max number of corner pts on a line */

  /* allocate memory for list, next_list, previous_list */
  list = EG_malloc(list_size*sizeof(int));
  next_list = EG_malloc(list_size*sizeof(int));
  previous_list = EG_malloc(list_size*sizeof(int));
  if (list == NULL || next_list == NULL || previous_list == NULL)
    return(-1);

  /* initialize list counts to 0 */
  list_ct = 0;
  next_list_ct = 0;
  previous_list_ct = 0;

  /* initialize the node adjacency list to -1 */
  for (i=0; i<num_nodes; i++)
    {
      node[i].adj_list[NORTH] = NULL_NODE;
      node[i].adj_list[SOUTH] = NULL_NODE;
      node[i].adj_list[EAST] = NULL_NODE;
      node[i].adj_list[WEST] = NULL_NODE;
    }

  /*
   * Iterate over all intervals to build a graph.  In each iteration,
   * calculate boundary points for each interval rectangle and then
   * determine appropriate boundary edges for each boundary point by
   * looking at overlapping intervals.  Note that we are working from the
   * bottom of the set of intervals up to the top.
   */
  for (i=0; i<rowh_dim; i++)
    {
      for (j=0; j<row_hdr[i].size; j++)
	{
	  /*
	   * choose the appropriate intervals to use in determining the
	   * boundary
	   */
	  if (clump_id != 0 && row_hdr[i].intervals[j].id != clump_id)
	    continue;

	  /* find bounds of rectangle about this interval */
	    
	  left = row_hdr[i].intervals[j].begin - OFFSET_X;
	  right = row_hdr[i].intervals[j].end + OFFSET_X;
	  top = i + OFFSET_Y;
	  bottom = i - OFFSET_Y;
#ifdef NOTNOW
	  left = row_hdr[i].intervals[j].begin  * STRETCH_X - OFFSET_X;
	  right = row_hdr[i].intervals[j].end  * STRETCH_X + OFFSET_X;
	  top = i  * STRETCH_Y + OFFSET_Y;
	  bottom = i  * STRETCH_Y - OFFSET_Y;
#endif

	  /* set the boundary points of the rectangle about this interval */

	  /* set left top boundary point and next_list */
	  node[node_ct].x = left;
	  node[node_ct].y = top;
	  node[node_ct].row = i;
	  node[node_ct].col = j;
	  node[node_ct].near_x = row_hdr[i].intervals[j].begin;
	  set_node_list(node, node_ct, SOUTH, SOUTH_BIT, node_ct+2);
	  node[node_ct].corner = NW;
	  next_list[next_list_ct] = node_ct;
	  next_list_ct++;
	  node_ct++;

	  /* set right top boundary point and next_list */
	  node[node_ct].x = right;
	  node[node_ct].y = top;
	  node[node_ct].row = i;
	  node[node_ct].col = j;
	  node[node_ct].near_x = row_hdr[i].intervals[j].end;
	  set_node_list(node, node_ct, SOUTH, SOUTH_BIT, node_ct+2);
	  node[node_ct].corner = NE;
	  next_list[next_list_ct] = node_ct;
	  next_list_ct++;
	  node_ct++;
	  
	  /* set left bottom boundary point and list */
	  node[node_ct].x = left;
	  node[node_ct].y = bottom;
	  node[node_ct].row = i;
	  node[node_ct].col = j;
	  node[node_ct].near_x = row_hdr[i].intervals[j].begin;
	  set_node_list(node, node_ct, NORTH, NORTH_BIT, node_ct-2);
	  node[node_ct].corner = SW;
	  list[list_ct] = node_ct;
	  list_ct++;
	  node_ct++;
	  
	  /* set right bottom boundary point and list */
	  node[node_ct].x = right;
	  node[node_ct].y = bottom;
	  node[node_ct].row = i;
	  node[node_ct].col = j;
	  node[node_ct].near_x = row_hdr[i].intervals[j].end;
	  set_node_list(node, node_ct, NORTH, NORTH_BIT, node_ct-2);
	  node[node_ct].corner = SE;
	  list[list_ct] = node_ct;
	  list_ct++;
	  node_ct++;
	}
      
/*  for (k=0; k<next_list_ct; k++)
    printf("next_list[%d] = %d\n", k, next_list[k]);
  for (k=0; k<list_ct; k++)
    printf("list[%d] = %d\n", k, list[k]);
  for (k=0; k<previous_list_ct; k++)
    printf("previous_list[%d] = %d\n", k, previous_list[k]); */
      /*
       * Merge list with previous list assigning horizontal boundary
       * edges and working with duplicate boundary points appropriately.
       * This means that duplicates coming from overlapping rectangles do
       * not generate boundary edges.  Duplicates from caddy corner
       * rectangles generate two boundary edges.  This merge is done in
       * principle only since a merged list is not created.
       */
      merge_lists(list, list_ct, previous_list, previous_list_ct, node);

      /* exchange previous list with next list */
      temp_list = previous_list;
      previous_list = next_list;
      next_list = temp_list;
      
      /*
       * set previous_list_ct to next_list_ct and reset list_ct and
       * next_list_ct to 0
       */
      previous_list_ct = next_list_ct;
      next_list_ct = 0;
      list_ct = 0;

/*  for (k=0; k<previous_list_ct; k++)
    printf("previous_list[%d] = %d\n", k, previous_list[k]); */

    }	  

  /*
   * Assign horizontal boundary edges for the last list.  This list was
   * not handled in the above loop.
   */
  merge_lists(list, list_ct, previous_list, previous_list_ct, node);

  /* free memory for list, next_list, previous_list */
  (void)EG_free(list);
  (void)EG_free(next_list);
  (void)EG_free(previous_list);

  return(0);
} /* bdry_graph */