SparseNode crm114__list_search(unsigned int c, SparseNode init, SparseElementList *l)
{
  SparseNode curr = init;

  if (!l) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_search: null list.\n");
    }
    return make_null_node(l->compact);
  }

  if (crm114__list_is_empty(l)) {
    return make_null_node(l->compact);
  }

  if (c <= node_col(l->head)) {
    return l->head;
  }

  if (c >= node_col(l->tail)) {
    return l->tail;
  }

  while (!null_node(curr) && node_col(curr) < c) {
    curr = next_node(curr);
  }

  while (!null_node(curr) && node_col(curr) > c) {
    curr = prev_node(curr);
  }

  return curr;
}
SparseElementList *crm114__list_map(void **addr, void *last_addr, int *n_elts_ptr) {
  SparseElementList *l;
  SparseNode n, pn;
  int n_elts = *n_elts_ptr, i;

  if (!addr || !*addr || !last_addr || n_elts < 0 || *addr >= last_addr) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_map: null arguments.\n");
    }
    *n_elts_ptr = 0;
    return NULL;
  }

  if ((void *)((SparseElementList *)*addr + 1) > last_addr) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_map: not enough memory for list.\n");
    }
    *n_elts_ptr = 0;
    return NULL;
  }

  l = (SparseElementList *)(*addr);
  *addr = l + 1;
  l->head = node_map(l->compact, addr, last_addr);
  pn = l->head;
  for (i = 1; i < n_elts; i++) {
    if (null_node(pn)) {
      break;
    }
    n = node_map(l->compact, addr, last_addr);
    if (null_node(n)) {
      break;
    }
    if (l->compact) {
      pn.compact->next = n.compact;
      n.compact->prev = pn.compact;
    } else {
      pn.precise->next = n.precise;
      n.precise->prev = pn.precise;
    }
    pn = n;
  }
  if (i != n_elts) {
    if (!null_node(pn)) {
      if (l->compact) {
	pn.compact->next = NULL;
      } else {
	pn.precise->next = NULL;
      }
    }
    *n_elts_ptr = i;
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_map: Couldn't read in enough elements.\n");
    }
  }
  l->last_addr = *addr;
  l->tail = pn;
  return l;
}
int crm114__list_read(SparseElementList *l, FILE *fp, int n_elts) {
  SparseNode n, pn;
  int i;
  size_t unused;

  if (!l || !fp || n_elts < 0) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_write: null arguments.\n");
    }
    return 0;
  }

  if (!crm114__list_is_empty(l)) {
    crm114__list_clear(l);
  }

  l->last_addr = NULL;

  unused = fread(l, sizeof(SparseElementList), 1, fp);
  if (n_elts <= 0) {
    return 0;
  }
  l->head = node_read(l->compact, fp);
  pn = l->head;
  for (i = 1; i < n_elts; i++) {
    if (null_node(pn)) {
      break;
    }
    n = node_read(l->compact, fp);
    if (null_node(n)) {
      break;
    }
    if (l->compact) {
      pn.compact->next = n.compact;
      n.compact->prev = pn.compact;
    } else {
      pn.precise->next = n.precise;
      n.precise->prev = pn.precise;
    }
    pn = n;
  }
  if (i != n_elts) {
    if (!null_node(pn)) {
      if (l->compact) {
	pn.compact->next = NULL;
      } else {
	pn.precise->next = NULL;
      }
    }
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_read: Couldn't read in enough elements.\n");
    }
  }
  l->tail = pn;
  return i;
}
Beispiel #4
0
  void save(
    IndexIterator first,IndexIterator last,Archive& ar,
    const unsigned int)const
  {
    /* calculate ordered positions */

    alg.execute(first,last);

    /* Given a consecutive subsequence of displaced elements
     * x1,...,xn, the following information is serialized:
     *
     *   p0,p1,...,pn,0
     *
     * where pi is a pointer to xi and p0 is a pointer to the element
     * preceding x1. Crealy, from this information is possible to
     * restore the original order on loading time. If x1 is the first
     * element in the sequence, the following is serialized instead:
     *
     *   p1,p1,...,pn,0
     *
     * For each subsequence of n elements, n+2 pointers are serialized.
     * An optimization policy is applied: consider for instance the
     * sequence
     *
     *   a,B,c,D
     * 
     * where B and D are displaced, but c is in its correct position.
     * Applying the schema described above we would serialize 6 pointers:
     *
     *  p(a),p(B),0
     *  p(c),p(D),0
     * 
     * but this can be reduced to 5 pointers by treating c as a displaced
     * element:
     *
     *  p(a),p(B),p(c),p(D),0
     */

    std::size_t last_saved=3; /* distance to last pointer saved */
    for(IndexIterator it=first,prev=first;it!=last;prev=it++,++last_saved){
      if(!alg.is_ordered(get_node(it))){
        if(last_saved>1)save_node(get_node(prev),ar);
        save_node(get_node(it),ar);
        last_saved=0;
      }
      else if(last_saved==2)save_node(null_node(),ar);
    }
    if(last_saved<=2)save_node(null_node(),ar);

    /* marks the end of the serialization info for [first,last) */

    save_node(null_node(),ar);
  }
void crm114__list_remove_elt(SparseElementList *l, SparseNode toremove) {

  if (!l) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_remove_elt: null list.\n");
    }
    return;
  }

  if (null_node(toremove)) {
    return;
  }

  if (!null_node(prev_node(toremove))) {
    if (l->compact) {
      toremove.compact->prev->next = toremove.compact->next;
    } else {
      toremove.precise->prev->next = toremove.precise->next;
    }
  } else {
    if (l->compact) {
      l->head.compact = toremove.compact->next;
    } else {
      l->head.precise = toremove.precise->next;
    }
  }
  if (!null_node(next_node(toremove))) {
    if (l->compact) {
      toremove.compact->next->prev = toremove.compact->prev;
    } else {
      toremove.precise->next->prev = toremove.precise->prev;
    }
  } else {
    if (l->compact) {
      l->tail.compact = toremove.compact->prev;
    } else {
      l->tail.precise = toremove.precise->prev;
    }
  }

  if (l->compact) {
    if (!(l->last_addr) || (void *)toremove.compact < (void *)l ||
	(void *)toremove.compact >= l->last_addr) {
      node_free(toremove);
    }
  } else {
    if (!(l->last_addr) || (void *)toremove.precise < (void *)l ||
	(void *)toremove.precise >= l->last_addr) {
      node_free(toremove);
    }
  }
}
conflict_graph::conflict_graph()
{
    conflict_node null_node(-1);
    
    // Fill f_conflict with four null nodes. In this way, we can access directly f_conflict with the id of the vertex in the convex hull.
    f_conflict.fill(null_node, 4);
}
void crm114__list_clear(SparseElementList *l) {
  SparseNode curr, next;
  int i;

  if (!l) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_clear: null list.\n");
    }
    return;
  }

  curr = l->head;

  i = 0;

  while (!null_node(curr)) {
    next = next_node(curr);
    if (!(l->last_addr)) {
      node_free(curr);
    } else {
      if (l->compact && ((void *)curr.compact < (void *)l ||
			 (void *)curr.compact >= l->last_addr)) {
	node_free(curr);
      }
      if (!(l->compact) && ((void *)curr.precise < (void *)l ||
			    (void *)curr.precise >= l->last_addr)) {
	node_free(curr);
      }
    }
    curr = next;
    i++;
  }
  l->head = make_null_node(l->compact);
  l->tail = make_null_node(l->compact);
}
//writes a node to a file
static inline size_t node_write(SparseNode n, FILE *fp) {
  if (null_node(n) || !fp) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "node_write: null arguments.\n");
    }
  }
  if (n.is_compact) {
    return sizeof(CompactSparseNode)*fwrite(n.compact,
					    sizeof(CompactSparseNode), 1, fp);
  }

  return sizeof(PreciseSparseNode)*fwrite(n.precise,
					  sizeof(PreciseSparseNode), 1, fp);
}
Beispiel #9
0
static int output_json_node_value (JsonNode *node,
				   PRN *prn)
{
    GType type = 0;
    int err = 0;

    if (null_node(node)) {
	gretl_errmsg_set("jsonget: got a null node");
	return E_DATA;
    }

    type = json_node_get_value_type(node);

#if 0
    fprintf(stderr, "jsonget: node type %s\n", g_type_name(type));
#endif    
    
    if (!handled_type(type)) {
	gretl_errmsg_sprintf("jsonget: unhandled object type '%s'", 
			     g_type_name(type));
	err = E_DATA;
    } else if (type == G_TYPE_STRING) {
	const gchar *s = json_node_get_string(node);

	if (s != NULL) {
	    pputs(prn, s);
	} else {
	    err = E_DATA;
	}	
    } else if (type == G_TYPE_DOUBLE) {
	double x = json_node_get_double(node);

	pprintf(prn, "%.15g", x);
    } else {
	gint64 k = json_node_get_int(node);
	double x = (double) k;

	pprintf(prn, "%.15g", x);
    }

    return err;
}
Beispiel #10
0
size_t crm114__list_write(SparseElementList *l, FILE *fp) {
  SparseNode curr;
  size_t size;

  if (!l || !fp) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_write: null arguments.\n");
    }
    return 0;
  }

  size = sizeof(SparseElementList)*fwrite(l, sizeof(SparseElementList),
					  1, fp);
  curr = l->head;
  while (!null_node(curr)) {
    size += node_write(curr, fp);
    curr = next_node(curr);
  }
  return size;
}
Beispiel #11
0
void *crm114__list_memmove(SparseElementList *to, SparseElementList *from) {
  void *curr;
  SparseNode n, tn, tpn;
  int i;

  if (!from || !to) {
    if (CRM114__MATR_DEBUG_MODE) {
      fprintf(stderr, "crm114__list_memmove: null arguments.\n");
    }
    return to;
  }

  *to = *from;
  curr = (void *)(to + 1);
  n = from->head;


  if (null_node(to->head)) {
    return curr;
  }
  if (from->compact) {
    to->head.compact = (CompactSparseNode *)curr;
    curr = (void *)((CompactSparseNode *)curr + 1);
    *(to->head.compact) = *(n.compact);
    to->head.precise = NULL;
  } else {
    to->head.precise = (PreciseSparseNode *)curr;
    curr = (void *)((PreciseSparseNode *)curr + 1);
    *(to->head.precise) = *(n.precise);
    to->head.compact = NULL;
  }

  tpn = to->head;
  n = next_node(n);
  i = 1;
  tn.is_compact = from->compact;
  tpn.is_compact = from->compact;
  while (!null_node(n)) {
    if (from->compact) {
      tn.compact = (CompactSparseNode *)curr;
      curr = (void *)((CompactSparseNode *)curr + 1);
      tn.compact->data = n.compact->data;
      tn.compact->prev = tpn.compact;
      tn.compact->next = NULL;
      tn.precise = NULL;
      tpn.compact->next = tn.compact;
    } else {
      tn.precise = (PreciseSparseNode *)curr;
      curr = (void *)((PreciseSparseNode *)curr + 1);
      tn.precise->data = n.precise->data;
      tn.precise->prev = tpn.precise;
      tn.precise->next = NULL;
      tn.compact = NULL;
      tpn.precise->next = tn.precise;
    }
    n = next_node(n);
    tpn = tn;
    i++;
  }
  to->tail = tpn;
  to->last_addr = curr;
  return curr;
}
Beispiel #12
0
int crm114__list_is_empty(SparseElementList *l) {
  if (!l) {
    return 1;
  }
  return null_node((l->head));
}
Beispiel #13
0
static int real_json_get (JsonParser *parser, const char *pathstr,
			  int *n_objects, PRN *prn)
{
    GError *gerr = NULL;
    JsonNode *match, *node;
    JsonPath *path;
    GType ntype;
    int err = 0;

    *n_objects = 0;

    node = json_parser_get_root(parser);

    if (node == NULL || json_node_is_null(node)) {
	gretl_errmsg_set("jsonget: got null root node");
	return E_DATA;
    }
    
    path = json_path_new();

    if (!json_path_compile(path, pathstr, &gerr)) {
	if (gerr != NULL) {
	    gretl_errmsg_sprintf("jsonget: failed to compile JsonPath: %s",
				 gerr->message);
	    g_error_free(gerr);
	} else {
	    gretl_errmsg_set("jsonget: failed to compile JsonPath");
	}	    
	g_object_unref(path);
	return E_DATA;
    }

    match = json_path_match(path, node);

    if (null_node(match)) {
	/* FIXME : maybe return empty string? */
	g_object_unref(path);
	return E_DATA;
    }

    /* in case we get floating-point output */
    gretl_push_c_numeric_locale();

    if (JSON_NODE_HOLDS_ARRAY(match)) {
	JsonArray *array = json_node_get_array(match);
	int len = 0, index = 0;

	if (non_empty_array(array)) {
	    len = json_array_get_length(array);
	    node = json_array_get_element(array, index);
	} else {
	    node = NULL;
	}

    repeat:

	if (null_node(node)) {
	    gretl_errmsg_set("jsonget: failed to match JsonPath");
	    ntype = 0;
	    err = E_DATA;
	    goto bailout;
	} else {
	    ntype = json_node_get_value_type(node);
	}

	if (node != NULL && !handled_type(ntype)) {
	    if (JSON_NODE_HOLDS_ARRAY(node)) {
		/* recurse on array type */
		array = json_node_get_array(node);
		if (non_empty_array(array)) {
		    node = json_array_get_element(array, 0);
		    goto repeat;
		}
	    } else if (json_node_get_node_type(node) == JSON_NODE_OBJECT) {
		err = excavate_json_object(node, n_objects, prn);
		if (!err) {
		    if (index < len - 1) {
			node = json_array_get_element(array, ++index);
			goto repeat;
		    }
		}
	    } else {
		gretl_errmsg_sprintf("jsonget: unhandled array type '%s'", 
				     g_type_name(ntype));
		err = E_DATA;
	    }
	} else if (array != NULL) {
	    int i, n = json_array_get_length(array);

	    for (i=0; i<n && !err; i++) {
		node = json_array_get_element(array, i);
		err = output_json_node_value(node, prn);
		if (!err) {
		    *n_objects += 1;
		    if (n > 1) {
			pputc(prn, '\n');
		    }
		}
	    }
	}
    } else {
	/* not an array-holding node */
	err = output_json_node_value(match, prn);
	if (!err) {
	    *n_objects += 1;
	}
    }

 bailout:

    gretl_pop_c_numeric_locale();

    json_node_free(match);
    g_object_unref(path);

    return err;
}