Ejemplo n.º 1
0
int main() {
  
  igraph_trie_t trie;
  long int id;
  int i;
  char *str;

  /* init */
  igraph_trie_init(&trie, 0);

  /* add and get values */
  igraph_trie_get(&trie, "hello", &id);  printf("hello: %li\n", id);
  igraph_trie_get(&trie, "hepp", &id);   printf("hepp:  %li\n", id);
  igraph_trie_get(&trie, "alma", &id);   printf("alma:  %li\n", id);
  igraph_trie_get(&trie, "also", &id);   printf("also:  %li\n", id);

  igraph_trie_get(&trie, "hello", &id);  printf("hello: %li\n", id);
  igraph_trie_get(&trie, "hepp", &id);   printf("hepp:  %li\n", id);
  igraph_trie_get(&trie, "alma", &id);   printf("alma:  %li\n", id);
  igraph_trie_get(&trie, "also", &id);   printf("also:  %li\n", id);

  igraph_trie_get(&trie, "a", &id);      printf("a:     %li\n", id);
  igraph_trie_get(&trie, "axon", &id);   printf("axon:  %li\n", id);

  igraph_trie_get(&trie, "hello", &id);  printf("hello: %li\n", id);
  igraph_trie_get(&trie, "hepp", &id);   printf("hepp:  %li\n", id);
  igraph_trie_get(&trie, "alma", &id);   printf("alma:  %li\n", id);
  igraph_trie_get(&trie, "also", &id);   printf("also:  %li\n", id);

  /* check for existence */
  igraph_trie_check(&trie, "head", &id); printf("head:  %li\n", id);
  igraph_trie_check(&trie, "alma", &id); printf("alma:  %li\n", id);
   
  /* destroy */
  igraph_trie_destroy(&trie);

  /* the same with index */
  igraph_trie_init(&trie, 1);

  igraph_trie_get(&trie, "hello", &id);  printf("hello: %li\n", id);
  igraph_trie_get(&trie, "hepp", &id);   printf("hepp:  %li\n", id);
  igraph_trie_get(&trie, "alma", &id);   printf("alma:  %li\n", id);
  igraph_trie_get(&trie, "also", &id);   printf("also:  %li\n", id);

  igraph_trie_get(&trie, "hello", &id);  printf("hello: %li\n", id);
  igraph_trie_get(&trie, "hepp", &id);   printf("hepp:  %li\n", id);
  igraph_trie_get(&trie, "alma", &id);   printf("alma:  %li\n", id);
  igraph_trie_get(&trie, "also", &id);   printf("also:  %li\n", id);

  igraph_trie_get(&trie, "a", &id);      printf("a:     %li\n", id);
  igraph_trie_get(&trie, "axon", &id);   printf("axon:  %li\n", id);

  igraph_trie_get(&trie, "hello", &id);  printf("hello: %li\n", id);
  igraph_trie_get(&trie, "hepp", &id);   printf("hepp:  %li\n", id);
  igraph_trie_get(&trie, "alma", &id);   printf("alma:  %li\n", id);
  igraph_trie_get(&trie, "also", &id);   printf("also:  %li\n", id);

  /* check for existence */
  igraph_trie_check(&trie, "head", &id); printf("head:  %li\n", id);
  igraph_trie_check(&trie, "alma", &id); printf("alma:  %li\n", id);

  for (i=0; i<igraph_trie_size(&trie); i++) {
    igraph_trie_idx(&trie, i, &str);
    printf("%d: %s\n", i, str);
  }
  igraph_trie_destroy(&trie);

  if (!IGRAPH_FINALLY_STACK_EMPTY) return 1;
  
  return 0;
}
void igraph_i_graphml_add_attribute_key(const xmlChar** attrs, 
					struct igraph_i_graphml_parser_state *state) {
  xmlChar **it;
  igraph_trie_t *trie=0;
  igraph_vector_ptr_t *ptrvector=0;
  long int id;
  int ret;
  igraph_i_graphml_attribute_record_t *rec=
    igraph_Calloc(1, igraph_i_graphml_attribute_record_t);

  if (!state->successful) return;
   
   if (rec==0) { 
    igraph_error("Cannot parse GraphML file", __FILE__, __LINE__, 
		 IGRAPH_ENOMEM);
    igraph_i_graphml_sax_handler_error(state, "Cannot parse GraphML file");
    return;
  }
  IGRAPH_FINALLY(igraph_free, rec);
  for (it=(xmlChar**)attrs; *it; it+=2) {
    if (xmlStrEqual(*it, toXmlChar("id"))) {
      const char *id=(const char*)(*(it+1));
      rec->id=strdup(id);
    } else if (xmlStrEqual(*it, toXmlChar("attr.name"))) {
      const char *name=fromXmlChar(*(it+1));
      rec->record.name=strdup(name);
    } else if (xmlStrEqual(*it, toXmlChar("attr.type"))) {
      if (xmlStrEqual(*(it+1), (xmlChar*)"boolean")) { 
	rec->type=I_GRAPHML_BOOLEAN;
	rec->record.type=IGRAPH_ATTRIBUTE_NUMERIC;	    
      } else if (xmlStrEqual(*(it+1), toXmlChar("string"))) {
	rec->type=I_GRAPHML_STRING;
	rec->record.type=IGRAPH_ATTRIBUTE_STRING;
      } else if (xmlStrEqual(*(it+1), toXmlChar("float"))) { 
	rec->type=I_GRAPHML_FLOAT;
	rec->record.type=IGRAPH_ATTRIBUTE_NUMERIC;
      } else if (xmlStrEqual(*(it+1), toXmlChar("double"))) { 
	rec->type=I_GRAPHML_DOUBLE;
	rec->record.type=IGRAPH_ATTRIBUTE_NUMERIC;
      } else if (xmlStrEqual(*(it+1), toXmlChar("int"))) {
	rec->type=I_GRAPHML_INTEGER;
	rec->record.type=IGRAPH_ATTRIBUTE_NUMERIC;
      } else if (xmlStrEqual(*(it+1), toXmlChar("long"))) {
	rec->type=I_GRAPHML_LONG;
	rec->record.type=IGRAPH_ATTRIBUTE_NUMERIC;
      } else {
	igraph_error("Cannot parse GraphML file, unknown attribute type", 
		     __FILE__, __LINE__, IGRAPH_PARSEERROR);
        igraph_i_graphml_sax_handler_error(state, "Cannot parse GraphML file, unknown attribute type");
        return;
      }
    } else if (xmlStrEqual(*it, toXmlChar("for"))) {
      /* graph, vertex or edge attribute? */
      if (xmlStrEqual(*(it+1), toXmlChar("graph"))) { 
	trie=&state->g_names;
	ptrvector=&state->g_attrs;
      } else if (xmlStrEqual(*(it+1), toXmlChar("node"))) {
	trie=&state->v_names;
	ptrvector=&state->v_attrs;
      } else if (xmlStrEqual(*(it+1), toXmlChar("edge"))) {
	trie=&state->e_names;
	ptrvector=&state->e_attrs;
      } else {
	igraph_error("Cannot parse GraphML file, unknown attribute type",
		     __FILE__, __LINE__, IGRAPH_PARSEERROR);
        igraph_i_graphml_sax_handler_error(state, "Cannot parse GraphML file, unknown attribute type");
        return;
      }
    }
  }

  if (trie == 0 && state->successful) {
    igraph_error("Cannot parse GraphML file, missing 'for' attribute", __FILE__, __LINE__, IGRAPH_PARSEERROR);
    igraph_i_graphml_sax_handler_error(state, "Cannot parse GraphML file, missing 'for' attribute");
    return;
  }
	
  /* add to trie, attribues */
  igraph_trie_get(trie, rec->id, &id);
  if (id != igraph_trie_size(trie)-1) {
    igraph_error("Cannot parse GraphML file, duplicate attribute", 
		 __FILE__, __LINE__, IGRAPH_PARSEERROR);
    igraph_i_graphml_sax_handler_error(state, "Cannot parse GraphML file, duplicate attribute");
    return;
  }
  ret=igraph_vector_ptr_push_back(ptrvector, rec);
  if (ret) {
    igraph_error("Cannot read GraphML file", __FILE__, __LINE__, ret);
    igraph_i_graphml_sax_handler_error(state, "Cannot read GraphML file");
    return;
  }

  /* create the attribute values */
  switch (rec->record.type) {
    igraph_vector_t *vec;
    igraph_strvector_t *strvec;
  case IGRAPH_ATTRIBUTE_NUMERIC:
    vec=igraph_Calloc(1, igraph_vector_t);
    if (vec==0) {
      igraph_error("Cannot parse GraphML file", __FILE__, __LINE__,
		   IGRAPH_ENOMEM);
      igraph_i_graphml_sax_handler_error(state, "Cannot parse GraphML file");
      return;
    }
    rec->record.value=vec;
    igraph_vector_init(vec, 0);    
    break;
  case IGRAPH_ATTRIBUTE_STRING:
    strvec=igraph_Calloc(1, igraph_strvector_t);
    if (strvec==0) {
      igraph_error("Cannot parse GraphML file", __FILE__, __LINE__, 
		   IGRAPH_ENOMEM);
      igraph_i_graphml_sax_handler_error(state, "Cannot parse GraphML file");
      return;
    }
    rec->record.value=strvec;
    igraph_strvector_init(strvec, 0);
    break;
  default: break;
  }

  IGRAPH_FINALLY_CLEAN(1);	/* rec */
}