Example #1
0
        void add(const T& begin_node, const T& end_node) // add arc
        {
          if(begin_node == end_node)
          {
            return;
          }

          size_t aId = 0;
          size_t bId = 0;

          node_ptr aTestResult = is_node(begin_node);
          if(aTestResult.get()->is_null())
          {
            node_ptr pNode = node_ptr(new node<T>(begin_node));
            aId = pNode->id;
            pNode->null = false;
            if(pNode->id > matrix_size)
            {
              std::ostringstream msg;
              msg << "node id " << pNode->id << " is greater than maximum matrix size " << matrix_size << "!";
              throw std::invalid_argument(msg.str()); // TODO: add test for that
            }
            this->storage.push_back(pNode);
          }
          else
          {
            aId = aTestResult->id;
          }

          node_ptr bTestResult = is_node(end_node);
          if(bTestResult.get()->is_null())
          {
            node_ptr pNode = node_ptr(new node<T>(end_node));
            bId = pNode->id;
            pNode->null = false;
            if(pNode->id >matrix_size)
            {
              std::ostringstream msg;
              msg << "node id " << pNode->id << " is greater than maximum matrix size " << matrix_size << "!";
              throw std::invalid_argument(msg.str()); // TODO: add test for that
            }
            this->storage.push_back(pNode);
          }
          else
          {
            bId = bTestResult->id;
          }
          matrix2D.at(aId).at(bId) = 1;
          matrix2D.at(bId).at(aId) = 1;
        }
Example #2
0
static bool walk(struct Node *node, cbtree_walker_func cb_func, void *cb_arg)
{
	if (!is_node(node))
		return cb_func(cb_arg, get_external(node));
	return walk(node->child[0], cb_func, cb_arg)
	    && walk(node->child[1], cb_func, cb_arg);
}
Example #3
0
  Point AbstractNode::GetRelativePosition (const AbstractView* widget)
  {
    if (widget == nullptr)
      throw std::invalid_argument("Argument cannot be a nullptr!");

    Point pos = widget->position();
    AbstractNode* node = 0;
    AbstractView* parent = widget->super();

    while (parent) {

      if (is_node(parent)) {
        node = dynamic_cast<AbstractNode*>(parent);
        break;
      }

      pos = pos + parent->position() + parent->GetOffset();
      parent = parent->super();

    }

    if (node != this)
      throw std::out_of_range("Widget is not contained in this node!");

    return pos;
  }
Example #4
0
        void add(const T& begin_node, const T& end_node) // add arc
        {
          if(begin_node == end_node)
          {
            return;
          }

          size_t aId = 0;
          size_t bId = 0;

          node_ptr aTestResult = is_node(begin_node);
          if(aTestResult.get()->is_null())
          {
            node_ptr pNode = node_ptr(new node<T>(begin_node));
            aId = pNode->id;
            pNode->null = false;
            this->storage.push_back(pNode);
            adjectedListsVec.push_back(list_ptr(new std::list<T>));
          }
          else
          {
            aId = aTestResult->id;
          }

          node_ptr bTestResult = is_node(end_node);
          if(bTestResult.get()->is_null())
          {
            node_ptr pNode = node_ptr(new node<T>(end_node));
            bId = pNode->id;
            pNode->null = false;
            this->storage.push_back(pNode);
            adjectedListsVec.push_back(list_ptr(new std::list<T>));
          }
          else
          {
            bId = bTestResult->id;
          }

          adjectedListsVec[aId]->push_back(bId);
          adjectedListsVec[aId]->unique();

          adjectedListsVec[bId]->push_back(aId);
          adjectedListsVec[bId]->unique();

          assert(this->storage.size() == adjectedListsVec.size());
        }	
Example #5
0
/* walk nodes until external pointer is found */
static void *raw_lookup(struct CBTree *tree, const void *key, unsigned klen)
{
	struct Node *node = tree->root;
	unsigned bit;
	while (is_node(node)) {
		bit = get_bit(node->bitpos, key, klen);
		node = node->child[bit];
	}
	return get_external(node);
}
Example #6
0
/* recursive freeing */
static void destroy_node(struct CBTree *tree, struct Node *node)
{
	if (is_node(node)) {
		destroy_node(tree, node->child[0]);
		destroy_node(tree, node->child[1]);
		cx_free(tree->cx, node);
	} else if (tree->obj_free_cb) {
		void *obj = get_external(node);
		tree->obj_free_cb(tree->cb_ctx, obj);
	}
}
bool CL_CSSBoxNodeWalker::next(bool traverse_children)
{
	CL_CSSBoxNode *next = 0;
	if (traverse_children && is_element() && !get_element()->is_display_none())
		next = cur->get_first_child();
	if (next)
		level++;
	else
		next = cur->get_next_sibling();
	while (cur && !next && level > 0)
	{
		level--;
		cur = cur->get_parent();
		if (cur)
			next = cur->get_next_sibling();
	}
	cur = next;
	return is_node();
}
Example #8
0
int degree(graph_t *g, char *lbl){

edge_t *aux;
int count = ZERO, trovato;

if( !g || !lbl || !(g->node) ){
			errno = EINVAL;
			return NEG; }

if( (trovato = is_node(g,lbl)) <= NEG){	
			errno= EINVAL;
			return NEG;  }

aux = ((g->node)+trovato)->adj;

for(; aux != NULL; aux = aux->next)
			   count++;

return count;
} 
Example #9
0
/* insert into specific bit-position */
static bool insert_at(struct CBTree *tree, unsigned newbit, const void *key, unsigned klen, void *obj)
{
	/* location of current node/obj pointer under examination */
	struct Node **pos = &tree->root;
	struct Node *node;
	unsigned bit;

	while (is_node(*pos) && ((*pos)->bitpos < newbit)) {
		bit = get_bit((*pos)->bitpos, key, klen);
		pos = &(*pos)->child[bit];
	}

	bit = get_bit(newbit, key, klen);
	node = new_node(tree);
	if (!node)
		return false;
	node->bitpos = newbit;
	node->child[bit] = set_external(obj);
	node->child[bit ^ 1] = *pos;
	*pos = node;
	return true;
}
Example #10
0
/* true -> object was found and removed, false -> not found */
bool cbtree_delete(struct CBTree *tree, const void *key, unsigned klen)
{
	void *obj, *tmp;
	unsigned bit = 0;
	/* location of current node/obj pointer under examination */
	struct Node **pos = &tree->root;
	/* if 'pos' has user obj, prev_pos has internal node pointing to it */
	struct Node **prev_pos = NULL;

	if (!tree->root)
		return false;

	/* match bits we know about */
	while (is_node(*pos)) {
		bit = get_bit((*pos)->bitpos, key, klen);
		prev_pos = pos;
		pos = &(*pos)->child[bit];
	}

	/* does the key actually matches */
	obj = get_external(*pos);
	if (!key_matches(tree, obj, key, klen))
		return false;

	if (tree->obj_free_cb)
		tree->obj_free_cb(tree->cb_ctx, obj);

	/* drop the internal node pointing to our key */
	if (prev_pos) {
		tmp = *prev_pos;
		*prev_pos = (*prev_pos)->child[bit ^ 1];
		cx_free(tree->cx, tmp);
	} else {
		tree->root = NULL;
	}
	return true;
}
Example #11
0
/*
in :
    - f is the file that contents the tree
    - n_tab is the recursive depth-value
out :
    - the tree that is in the file
*/
static struct node_t *load_tree_tab(FILE *f, int n_tab)
{
    struct node_t *ret = NULL;
    string buffer;
    char *ptr;
    fpos_t mem;

    fprintf(log_file, "%d tabs\n", n_tab);

    while(fgets(buffer, sizeof(buffer), f) != NULL)
    {
        ptr = process(buffer);
        if((ptr - buffer) == n_tab-1)
        {
            fsetpos(f, &mem);
            break;
        }

        if(is_node(ptr))
        {
            fprintf(log_file, "creating a new node for %s\n", ptr);
            add_node(&ret, ptr);
        }
        else if(is_edge(ptr))
        {
            fprintf(log_file, "exploring [%s in %s]\n", ptr, ret->property.name_attribute);
            add_child(ret, load_tree_tab(f, n_tab+1), ptr+1);
        }
        else if(is_leaf(ptr))
        {
            fprintf(log_file, "creating a \"%s\" node\n", ptr+1);
            return new_leaf(ptr+1);
        }
        fgetpos(f, &mem);
    }
    return ret;
}
Example #12
0
void build_in_dht()
{
#ifdef DHT

	static bool running = false;
	static mutex mut;
	static dht::DhtRunner ht;
	static int port = 4443;
	
	if (!running)
	{
		running = true;
		int port = 4443;
		dout << "firing up DHT on port " << port << endl;
		// Launch a dht node on a new thread, using a
		// generated RSA key pair, and listen on port 4222.
		ht.run(port, dht::crypto::generateIdentity(), true);

		// Join the network through any running node,
		// here using a known bootstrap node.
		ht.bootstrap("127.0.0.1", "4444");

		// put some data on the dht
		std::vector<uint8_t> some_data(5, 10);
		ht.put("unique_key", some_data);

		// put some data on the dht, signed with our generated private key
		ht.putSigned("unique_key_42", some_data);

	}

	auto ht_ = &ht;

	EEE;	
	
	string bu = "http://idni.org/dht#put";
	auto bui = dict.set(mkiri(pstr(bu)));

	builtins[bui].push_back(
		[bu, entry, ht_](Thing *dummy, Thing *x) mutable {
		setproc(bu);
		TRACE_ENTRY;
		dout <<"sssss" << endl;
		switch(entry){
		case 0:
			x = getValue(x);
			
			if(is_node(*x))
			{
				node n = dict[get_node(*x)];
				string v = *n.value;
				string key,val;

				if (n._type == node::IRI)
				{
					if (has(mykb->first, v))
					{
						key = "root_graph_" + v;
						stringstream ss;
						ss << mykb->first[v];
						val = ss.str();
					}
					else
					{
						string h = strhash(v);
						key = "root_iri_" + h;
						val = v;
					}
				}
				else if (n._type == node::LITERAL)
				{
					string h = strhash(v);
					key = "root_lit_" + h;
					val = v;
				}
				else
				{
					dout << "nope." << endl;
					DONE;
				}
				dout << "putting " << key << "=" << val << endl;
				ht_->put(key, val);
				
			}
			else
				dout << "nope." << endl;
			
						
			END;
		}
	});
	
	
	
	bu = "http://idni.org/dht#dbg";
	bui = dict.set(mkiri(pstr(bu)));

	builtins[bui].push_back(
		[bu, entry, ht_](Thing *dummy, Thing *x) mutable {
		setproc(bu);
		TRACE_ENTRY;
		
		switch(entry){
		case 0:
			x = getValue(x);
					
			if(is_node(*x))
			{
				node n = dict[get_node(*x)];
				string v = *n.value;
				if (v == "on")
				{
					MSG("dht dbg on");
					enableDhtLogging(*ht_);
				}
				else{
					MSG("dht dbg off");
					ht_->setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
				}
			}
						
			END;
		}
	});

	bu = "http://idni.org/dht#setPort";
	bui = dict.set(mkiri(pstr(bu)));

	builtins[bui].push_back(
		[bu, entry, ht_](Thing *dummy, Thing *x) mutable {
		setproc(bu);
		TRACE_ENTRY;
		
		switch(entry){
		case 0:
			x = getValue(x);
					
			if(is_node(*x))
			{
				node n = dict[get_node(*x)];
				string v = *n.value;
				if (v == "on")
				{
					MSG("dht dbg on");
					enableDhtLogging(*ht_);
				}
				else{
					MSG("dht dbg off");
					ht_->setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
				}
			}
						
			END;
		}
	});


    // get data from the dht
    ht_->get("other_unique_key", [](const std::vector<std::shared_ptr<dht::Value>>& values) {
        // Callback called when values are found
        for (const auto& value : values)
            dout << "Found value: " << *value << std::endl;
        return true; // return false to stop the search
    });
#endif

}
Example #13
0
graph_t * load_graph (FILE * fdnodes, FILE * fdarcs){

unsigned int i;	
graph_t *graph;		/* punterà al grafo da creare */

char *app, *p, *segna_posto, *line, *flag;	/* vari puntatori ausiliari */
edge_t *aux, *helper;

/* - dim conterrà l'esatta dimensione della stringa rappresentante il nodo sorgente, 
   - i_sorg e i_dest conterranno gli indici dei nodi sorg/dest dell'arco letto  */

int conta = ZERO, dim, i_sorg, i_dest;	


if( !fdnodes || !fdarcs ){		/* file non esistenti o errore nella precedente apertura?? */
		errno = EINVAL;
		return NULL; }

				/* ---FASE 1--- : creazione nodi sorgente */

MALLOC_IF(app, sizeof(char), PDIM2)

while ( fgets(app, PDIM2 - UNO, fdnodes) )	/* per la correttezza della struttura da allocare, mi serve sapere quanti nodi leggerò */
				conta++;
free(app);

if(conta <= ZERO){
	errno = EINVAL;
	return NULL; }

MALLOC_IF(graph, sizeof(graph_t), UNO)

if( !(MALLOC(graph->node, sizeof(node_t), conta)) ){		/* allocazione array dei 'conta' nodi */
					free(graph);
					return NULL;  }

graph->size = conta;

rewind(fdnodes);

/* inserimento nodi */

for(i = ZERO; i < conta ;i++){	

		/* char * PDIM2 = (LLABEL+2) considerando anche il '\n' letto e il '\0' inserito dalla fgets */
		
		if( !(MALLOC(app, sizeof(char), PDIM2) ) ){
							free_graph(&graph);
							return NULL; }
		
		fgets( app, PDIM2, fdnodes);	/* copia in app un intera riga del file (compreso il carattere '\n' : risulterà 
							d'intralcio per la successivi confronti (es. chiamata di is_node); allora lo elimino... */ 
		dim = strlen(app);
		
		app[dim - UNO] = TCHAR;		/* e al suo posto inserisco '\0' per terminare la stringa */

		if( !check_node(&app,UNO) || !( MALLOC( ((graph->node)+i)->label, sizeof(char), dim)) ){
											free(app);
											free_graph(&graph);
											return NULL; }

		
		strncpy( ((graph->node)+i)->label, app, dim);		/* copio la stringa fino all'ex '\n' compreso (ora '\0') */

		free(app);


		((graph->node)+i)->adj = NULL; 	
		}


					/* ---FASE 2--- : creazione archi */

MALLOC_IF(app, sizeof(char), PDIM1)

while( (fgets( app, PDIM1 - UNO, fdarcs)) ){		/* start loop per la lettura delle stringhe-arco */

	/* 1° PARTE: nodo sorgente */

segna_posto = strchr(app,':');

/* check di correttezza della 1° parte della stringa letta */

if( !segna_posto || !check_edge(app, segna_posto, TRUE) ){ 
						STD_OUT1 
						}

MALLOC_IF(p, sizeof(char), LLABEL+UNO)		/* per contenere il nodo sorgente: stavolta basta +1, dato che non viene letto '\n' */

flag = app + (parzial_cpy(p, app, segna_posto));		/* copio la parte relativa al nodo sorgente e mi sistemo dopo i primi ':' */

if( (i_sorg = is_node(graph,p)) < ZERO){
				free(p);
				STD_OUT1
				 }

free(p);
			
	/* 2° PARTE: nodo destinazione */

/* mi sistemo sui secondi ':' e lancio un check sulla correttezza della 2° sottostringa (relativa al nodo destinazione) */

segna_posto = strchr(flag,':');	
		
if( !segna_posto || !check_edge(flag, segna_posto, TRUE) ) {
							STD_OUT1
							  }

MALLOC_IF(p, sizeof(char), LLABEL+UNO)

flag = flag + parzial_cpy(p,flag,segna_posto);	/* copio la seconda sottostringa e mi sistemo subito dopo i secondi ':' */


/* 3° parte: distanze in km + creazione elemento lista di adiacenza del nodo sorgente; 
 	non proseguo se il nodo destinazione non è valido, se esiste già un arco uguale a quello che vogliamo aggiungere oppure se la 3° sottostringa 
			(relativa alla distanza in km) è malformata*/

line = remove_newline(flag);


if( ((i_dest = is_node(graph,p)) < ZERO) || ((is_edge(graph,i_sorg,i_dest)) ) || !check_edge(line, segna_posto, FALSE) ){	
														free(p);
														free(line);
														STD_OUT1
															 }
Example #14
0
int add_edge (graph_t *g, char *e){

int isaved_sorg = NEG, isaved_dest = NEG;	/* variabili locali che mantengono gli indici del nodo sorgente e del nodo destinazione del grafo g */

char *segna_posto, *app;		/* utilizzati rispettivamente per mantenere il riferimento al carattere ":" e alle
					   tre divisioni (realizzate successivamente) della stringa "e" passata alla funzione */
edge_t *aux, *helper;

if( !g  || !e ){
	errno = EINVAL;
	return NEG; }

/* ---FASE 1--- : individuo l'indice del nodo sorgente */

/* delimito il primo pezzo di stringa, fino ai primi ":" (se esistono) e lancio un check sulla correttezza sulla 1° parte di "e" */

segna_posto = strchr(e,':');	

if( !segna_posto || !check_edge(e, segna_posto, TRUE) ) {
						errno = EINVAL;
						return NEG; }

if( !( MALLOC(app, sizeof(char), LLABEL)) )
				return NEG;

parzial_cpy(app,e,segna_posto);		/* copio la prima parte di "e" (quella riguardante il nodo sorgente) in app */

if( ( isaved_sorg = is_node(g,app) ) < ZERO) {		/* se trovo una corrispondenza al nodo sorgente richiesto, ne prendo l'indice */		
				free(app);	
				errno = EINVAL;			
				return NEG; }
free(app);		
	
/* ---FASE 2---: individuo l'indice del nodo destinazione */
	
/* mi sistemo sul secondo pezzo di stringa e la delimito */

e = segna_posto + UNO;
segna_posto = strchr(e,':');

/* check sulla corretteza della 2° parte di "e" */

if( !segna_posto || !check_edge(e, segna_posto, TRUE) ){
					errno = EINVAL;
					return NEG; }

if( !( MALLOC(app, sizeof(char), LLABEL)) )
				return NEG;
	
parzial_cpy(app,e,segna_posto);		/* come per la FASE1, copio la sottostringa di "e" (riguardante il nodo destinazione) in "app" */	
	
if( ( isaved_dest = is_node(g,app) ) < ZERO){		/* individuo l'indice del nodo destinazione e ne salvo l'indice (se esiste) */
				free(app);				
				errno = EINVAL;						
				return NEG; }
free(app);


/* ---FASE 3--- : individuo la distanza in km */

/* 	mi sistemo sul terzo ed ultimo pezzo di sottostringa( quella riguardante i km) e lancio un check sulla correttezza della 3° parte 
				della stringa : deve risultare composta da soli numeri [0-9] 	*/

e = segna_posto + UNO; 	

if( !check_edge(e, segna_posto, FALSE) ){
			errno = EINVAL;
			return NEG; }


/* ora bisogna controllare se l'arco individuato da "e" esiste già */ 

if( !(((g->node)+isaved_sorg)->adj) ){

	if( !( MALLOC( ((g->node)+isaved_sorg)->adj, sizeof(edge_t), UNO)) ) 		 
								return NEG;	
	((g->node)+isaved_sorg)->adj->label = isaved_dest;
	((g->node)+isaved_sorg)->adj->next = NULL;
	sscanf(e,"%lf",&(((g->node)+isaved_sorg)->adj->km));  } 	/* legge dalla stringa "e" ,converte in double e inserisce nel campo km */
						
	else	/* adj con almeno un elemento */
	{
	  aux = ((g->node)+isaved_sorg)->adj;	
		  
	  if( (is_edge(g,isaved_sorg,isaved_dest)) ){	 /* arco già presente  nella adj?? */
					errno = EINVAL;	
					return NEG; }
			
			else	/* arco non presente nella adj: si può procedere all'inserzione */
		{
		  for(; aux -> next != NULL; aux = aux->next);	 /* mi sposto in coda: se la adj è vuota il ciclo non parte */
		  
		  if( !(MALLOC(helper, sizeof(edge_t), UNO)) )   
							return NEG;
		  aux->next = helper;
		  helper->label = isaved_dest;
		  helper->next = NULL;
		  sscanf(e,"%lf",&(helper->km)); } 	/* legge dalla stringa "e",converte in double e inserisce nel campo km */
			  	
			}	
return ZERO;
}