Beispiel #1
0
Nodes Region::buildnodes()
{
	Nodes Result;
	srand(unsigned(time(0)));
	if(typ==1)
	{
		double steph=double(PIXELSH)/numofrows,stepw=double(PIXELSW)/numofcols;
		if(numofrows<=numofcols)
		{
			for(int i=0;i<=numofcols;i++)
				for(int j=0;j<=numofrows;j++)
					//Result.push_back(Point(stepw*i,steph*j),sqrt(stepw*i*stepw*i+steph*j*steph*j)/PIXELSH*50-50);
					//Result.push_back(Point(stepw*i,steph*j),double(rand())/RAND_MAX*200-100);
					Result.push_back(Point(stepw*i,steph*j),0);
		}
		else
		{
			for(int j=0;j<=numofrows;j++)
				for(int i=0;i<=numofcols;i++)
					//Result.push_back(Point(stepw*i,steph*j),sqrt(stepw*i*stepw*i+steph*j*steph*j)/PIXELSH*50-50);
					//Result.push_back(Point(stepw*i,steph*j),double(rand())/RAND_MAX*200-100);
					Result.push_back(Point(stepw*i,steph*j),0);
		}
	}
	return Result;
}
    QObjectList loadAll(NodeObjectMap &map) {
        
        Nodes nodes;
        
        Triples candidates = m_s->match(Triple(Node(), Uri("a"), Node()));
        foreach (Triple t, candidates) {
            if (t.c.type != Node::URI) continue;
            nodes.push_back(t.a);
        }

        LoadState state;
        state.requested = nodes;
        state.map = map;
        state.loadFlags = LoadState::IgnoreUnknownTypes;

        collect(state);
        load(state);

        map = state.map;

        QObjectList objects;
        foreach (Node n, nodes) {
            QObject *o = map.value(n);
            if (o) objects.push_back(o);
        }
Beispiel #3
0
void Config::_stopNodes()
{
    // wait for the nodes to stop, destroy entities, disconnect
    Nodes stoppingNodes;
    const Nodes& nodes = getNodes();
    for( Nodes::const_iterator i = nodes.begin(); i != nodes.end(); ++i )
    {
        Node* node = *i;
        const State state = node->getState();
        if( state != STATE_STOPPED && state != STATE_FAILED )
            continue;

        LBASSERT( !node->isActive() || state == STATE_FAILED );
        if( node->isApplicationNode( ))
            continue;

        co::NodePtr netNode = node->getNode();
        if( !netNode ) // already disconnected
            continue;

        LBLOG( LOG_INIT ) << "Exiting node" << std::endl;

        if( state == STATE_FAILED )
            node->setState( STATE_STOPPED );

        stoppingNodes.push_back( node );
        LBASSERT( netNode.isValid( ));

        netNode->send( fabric::CMD_SERVER_DESTROY_CONFIG )
                << getID() << LB_UNDEFINED_UINT32;
        netNode->send( fabric::CMD_CLIENT_EXIT );
    }

    // now wait that the render clients disconnect
    uint32_t nSleeps = 50; // max 5 seconds for all clients
    for( Nodes::const_iterator i = stoppingNodes.begin();
         i != stoppingNodes.end(); ++i )
    {
        Node*        node    = *i;
        co::NodePtr netNode = node->getNode();
        node->setNode( 0 );

        if( nSleeps )
            while( netNode->isConnected() && --nSleeps )
                lunchbox::sleep( 100 ); // ms

        if( netNode->isConnected( ))
        {
            co::LocalNodePtr localNode = getLocalNode();
            LBASSERT( localNode.isValid( ));

            LBWARN << "Forcefully disconnecting exited render client node"
                   << std::endl;
            localNode->disconnect( netNode );
        }

        LBLOG( LOG_INIT ) << "Disconnected node" << std::endl;
    }
}
Beispiel #4
0
void LocalNode::getNodes( Nodes& nodes, const bool addSelf ) const
{
    base::ScopedMutex< base::SpinLock > mutex( _nodes );
    for( NodeHash::const_iterator i = _nodes->begin();
         i != _nodes->end(); ++i )
    {
        EQASSERT( i->second->isConnected( ));
        if( addSelf || i->second != this )
            nodes.push_back( i->second );
    }
}
Beispiel #5
0
Nodes *av_to_nodes(pTHX_ SV *from_)
{
	Nodes *ret = new Nodes();
	AV *from = (AV *)((SvROK(from_)) ? SvRV(from_) : from_);
	int size = av_len(from);
	for (int i = 0; i <= size; i++) {
		SV *arg = (SV *)*av_fetch(from, i, FALSE);
		ret->push_back(hv_to_node(aTHX_ arg));
	}
	return ret;
}
Beispiel #6
0
static void neighbors(Node *n, Nodes &r, NodePool &p, const Grid &g) {
  Tile v[16];
  size_t s = g.adjacent(n->tile, v, COUNTOF(v));
  r.clear();
  for (Tile *t = v; t < v + s; ++t) {
    float c = g.get(*t);
    if (c > 0.1f) {
      Node *a = new (p) Node(*t, c);
      r.push_back(a);
    }
  }
}
Beispiel #7
0
Nodes Test::parse_nodes(const char *nodes_str) const
{
	Nodes nodes;
	std::vector<int> array = parse_int_array(nodes_str);
	assert(array.size() % 2 == 0);
	for (int i = 0; i < array.size(); i += 2) {
		int qid(array[i]);
		int kid(array[i+1]);
		nodes.push_back(Node(qid, kid));
	}
	return nodes;
}
void GraphProtoInterface::build_from_proto(graph::Graph *graph) { 
  vector <Graphnode *> nodes;
  vector <Graphedge *> edges;


  int edge_count = 0;
  for (int i = 0; i < graph->node_size(); i++) {
    const graph::Graph_Node & node = graph->node(i);
    for (int j=0; j < node.edge_size(); j++) {
      const graph::Graph_Edge& edge = node.edge(j);
      edge_count++;
    }
  }

  set_up(*graph, graph->node_size(), edge_count);


  for (int i = 0; i < graph->node_size(); i++) {
    const graph::Graph_Node & node = graph->node(i);

    Graphnode * my_node = new Graphnode(node.id());
    my_node->set_label(node.label());
    process_node(node, my_node);
    nodes.push_back(my_node);
  }

  uint edge_id = 0;
  for (int i = 0; i < graph->node_size(); i++) {
    const graph::Graph_Node& node = graph->node(i);

    for (int j=0; j < node.edge_size(); j++) {
      const graph::Graph_Edge& edge = node.edge(j);
      int to_node = edge.to_node();

      
      Graphedge * my_edge = new Graphedge(edge_id, *nodes[node.id()], *nodes[to_node]);
      process_edge(edge, my_edge);      

      //((HypernodeImpl*)_nodes[to_node])->add_edge(forest_edge);
      nodes[node.id()]->add_edge(my_edge);
      nodes[my_edge->to_node()->id()]->add_in_edge(my_edge);
      edge_id++;
      edges.push_back(my_edge);
    }
  }

  Nodes * ns  = new Nodes ();
  Edges * es = new Edges ();
  
  foreach (Graphnode * n, nodes) {
    ns->push_back((Node) n);
  }
Beispiel #9
0
Node::Nodes Node::children() const
{
   Nodes childs;
   for (Children::const_iterator it = children_.begin(), end = children_.end();
        it != end; ++it) {
      if (it->is_node()) {
         assert(it->node());
         childs.push_back(it->node());
      }
      else {
         assert(false);
      }
   }
   return childs;
}
        osgDB::ReaderWriter::ReadResult readNodeFromArchive(osgDB::Archive& archive, const osgDB::ReaderWriter::Options* options) const
        {
            osgDB::ReaderWriter::ReadResult result(osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND);

            if (!archive.getMasterFileName().empty())
            {
                result = archive.readNode(archive.getMasterFileName(), options);
            }
            else
            {
                osgDB::Archive::FileNameList fileNameList;
                if (archive.getFileNames(fileNameList))
                {
                    typedef std::list< osg::ref_ptr<osg::Node> > Nodes;
                    Nodes nodes;
                    for(osgDB::Archive::FileNameList::iterator itr = fileNameList.begin();
                        itr != fileNameList.end();
                        ++itr)
                    {
                        result = archive.readNode(*itr, options);
                        if (result.validNode()) nodes.push_back(result.getNode());
                    }

                    if (!nodes.empty())
                    {
                        if (nodes.size()==1)
                        {
                            result = osgDB::ReaderWriter::ReadResult(nodes.front().get());
                        }
                        else
                        {
                            osg::ref_ptr<osg::Group> group = new osg::Group;
                            for(Nodes::iterator itr = nodes.begin();
                                itr != nodes.end();
                                ++itr)
                            {
                                group->addChild(itr->get());
                            }
                            result = osgDB::ReaderWriter::ReadResult(group.get());
                        }
                    }
                }
            }
            return result;
        }
Beispiel #11
0
Nodes Node::filter(const std::string& selector,const std::string& type) const {
	std::string xpat;
	if(type=="css") xpat = xpath(selector);
	else if(type=="xpath") xpat = selector;
	else STENCILA_THROW(Exception,"Unknown selector type <"+type+">");
	try {
		// Select nodes
		pugi::xpath_node_set selected = pimpl_->select_nodes(xpat.c_str());
		// Construct Nodes from pugi::xpath_node_set
		Nodes nodes;
		for(pugi::xpath_node_set::const_iterator it = selected.begin(); it != selected.end(); ++it){
			nodes.push_back(it->node());
		}
		return nodes;
	} catch (const pugi::xpath_exception& e){
		STENCILA_THROW(Exception,e.what());
	}
}
Beispiel #12
0
void threes(const Vec2 &point)
{
	point.toString();
	int l,f,u,d;
	l = f = point.x;
	u = d = point.y;
	PVector tmpX;
	PVector tmpY;
	WTF node;
	node.centre = point;
	int countX = 0;
	while(--l > 0)
	{
		if (Map[l][point.y] == Map[point.x][point.y])
		{
			cout<<l<<" l "<<point.y<<endl;
			tmpX.push_back(Vec2(l,point.y));
			++countX;
		}
		else
			break;
	};
	while(++f < xCount)
	{
		if (Map[f][point.y] == Map[point.x][point.y])
		{
			cout<<f<<" f "<<point.y<<endl;
			tmpX.push_back(Vec2(f,point.y));
			++countX;
		}
		else
			break;
	};
	if (countX>=2)
	{
		cout<<"X is THREE"<<endl;
		node.vecX = tmpX;
	}

	int countY = 0;
	while(--u > 0)
	{
		if (Map[point.x][u] == Map[point.x][point.y])
		{
			cout<<point.x<<" u "<<u<<endl;
			tmpY.push_back(Vec2(point.x,u));
			++countY;
		}
		else
			break;
	};
	while(++d < yCount)
	{
		if (Map[point.x][d] == Map[point.x][point.y])
		{
			cout<<point.x<<" d "<<d<<endl;
			tmpY.push_back(Vec2(point.x,d));
			++countY;
		}
		else
			break;
	};
	if (countY>=2)
	{
		cout<<"Y is THREE"<<endl;
		node.vecY = tmpY;
	}
	bool cross = countX>=2&&countY>=2;
	bool five = countX>=4||countY>=4;
	bool four = countX>=3||countY>=3;
	bool three = countX>=2||countY>=2;
	node.p = 0;
	if (countX>=2&&countY>=2)
	{
		node.p |= CROSS;
	}
	if (countX>=4||countY>=4)
	{
		node.p |= FIVE;	
	}
	else if (countX>=3||countY>=3)
	{
		node.p |= FOUR;	
	}
	else if (countX>=2||countY>=2)
	{
		node.p |= THREE;		
	}
	if (node.p!=0)
	{
		cout<<"Yes"<<endl;
		nodes.push_back(node);
	}
}
/**
 * Takes a subgraph of the given graph (all nodes in the graph with the given label),
 * partitions this subgraph into even smaller subgraphs (using something similar to k-means),
 * and gives all small subgraphs a unique label (using the given min_label).
 *
 * @param graph
 * @param label_of_connected_component
 * @param size_of_largest_partition
 * @param min_label_for_partition_labeling
 * @return the number of generated partitions
 */
std::size_t
partition_connected_component(UniGraph * graph, std::size_t label_of_connected_component, std::size_t partition_size, std::size_t min_label_for_partition_labeling)
{
    typedef std::size_t Node;
    typedef std::size_t Label;
    typedef std::vector<Node> Nodes;

    Nodes nodes;
    for (Node node = 0; node < graph->num_nodes(); ++node)
        if (graph->get_label(node) == label_of_connected_component)
            nodes.push_back(node);

    const std::size_t num_partitions = (nodes.size() + partition_size - 1) / partition_size; // division and rounding up

    /********* k-means clustering *******/

    const std::size_t num_kmeans_iterations = 100;

    Nodes centroids;

    /* Draw centroids randomly. */
    std::default_random_engine generator;
    std::uniform_int_distribution<std::size_t> distribution(0, nodes.size() - 1);
    for(std::size_t partition = 0; partition < num_partitions; ++partition) {
        Node centroid = std::numeric_limits<Node>::max();
        while (std::find(centroids.begin(), centroids.end(), centroid) != centroids.end())
            centroid = nodes.at(distribution(generator));
        centroids.push_back(centroid);
    }

    for (std::size_t kmeans_iteration = 0; kmeans_iteration < num_kmeans_iterations; ++kmeans_iteration) {
        const Label unvisited = std::numeric_limits<Label>::max();
        for (Node const & node : nodes)
            graph->set_label(node, unvisited);

        /* Put centroids into queues. */
        std::vector<Nodes> queues(num_partitions);
        for (std::size_t i = 0; i < num_partitions; ++i)
            queues.at(i).push_back(centroids.at(i));

        /* Grow regions starting from centroids */
        while (std::any_of(queues.begin(), queues.end(), [](Nodes const & queue){return !queue.empty();})) {
#pragma omp parallel for
            for (std::size_t queue_id = 0; queue_id < queues.size(); ++queue_id) {
                Nodes & old_queue = queues.at(queue_id);
                std::unordered_set<Node> new_queue;
                for (Node node : old_queue)
                    graph->set_label(node, min_label_for_partition_labeling + queue_id); // there is a race condition for partition boundary nodes but we don't care
                for (Node node : old_queue) {
                    /* Copy all unvisited (and not yet inserted) neighbors into new queue. */
                    for (Node neighbor : graph->get_adj_nodes(node))
                        if (graph->get_label(neighbor) == unvisited)
                            new_queue.insert(neighbor);
                }

                old_queue.clear();
                old_queue.insert(old_queue.begin(), new_queue.begin(), new_queue.end());
            }
        }

        /* If we are in the final iteration we stop here to keep the graph labels
         * (they would be removed in the following region shrinking step). */
        if (kmeans_iteration == num_kmeans_iterations - 1)
            break;

        /* Put partition boundary nodes into queues. */
        for (Node const node : nodes) {
            Label const cur_label = graph->get_label(node);
            std::size_t const cur_queue = cur_label - min_label_for_partition_labeling;
            Nodes const & neighbors = graph->get_adj_nodes(node);
            /* Each node, where any of its neighbors has a different label, is a boundary node. */
            if (std::any_of(neighbors.begin(), neighbors.end(), [graph, cur_label]
                (Node const neighbor) { return graph->get_label(neighbor) != cur_label; } ))
                queues.at(cur_queue).push_back(node);
        }

        /* Shrink regions starting from boundaries to obtain new centroids. */
#pragma omp parallel for
        for (std::size_t queue_id = 0; queue_id < queues.size(); ++queue_id) {
            Nodes & old_queue = queues.at(queue_id);
            while (!old_queue.empty()){
                std::unordered_set<Node> new_queue;
                for (Node node : old_queue)
                    graph->set_label(node, unvisited);
                for (Node node : old_queue) {
                    /* Copy all neighbors that have not yet been marked (and have not yet been inserted) into new queue. */
                    for (Node neighbor : graph->get_adj_nodes(node))
                        if (graph->get_label(neighbor) == min_label_for_partition_labeling + queue_id)
                            new_queue.insert(neighbor);
                }

                /* If the new queue is empty we are (almost) finished and use a random node from the old queue as new centroid. */
                if (new_queue.empty()) {
                    std::uniform_int_distribution<std::size_t> distribution(0, old_queue.size() - 1);
                    centroids.at(queue_id) = old_queue.at(distribution(generator));
                }

                /* Replace old queue with new one. */
                old_queue.clear();
                old_queue.insert(old_queue.begin(), new_queue.begin(), new_queue.end());
            }
        }
    }

    return num_partitions;
}
Beispiel #14
0
void LeaflessOrthoRouter::route(Logger *logger) {

    // Set up for logging.
    unsigned ln = logger != nullptr ? logger->nextLoggingIndex : 0;
    std::function<void(unsigned)> log = [ln, this, logger](unsigned n)->void{
        if (logger!=nullptr) {
            std::string fn = string_format("%02d_%02d_routing_attempt", ln, n);
            std::string path = logger->writeFullPathForFilename(fn);
            this->m_ra.router.outputInstanceToSVG(path);
        }
    };

    /*
     * We may need to route multiple times to ensure that at least two sides of each node are being used,
     * but in theory we should never have to route more than 4n+1 times.
     *
     *  Proof: We always begin with an initial routing. We want to show it could be necessary to re-route
     *  at most 4n times.
     *
     *  In order to see this, we first argue that the worst-case-scenario for any single node is that it
     *  require four routings. Consider then some node u all of whose edges have been routed to one side, s0. We
     *  then pick some edge e0 incident to u, say that it may not connect to side s0, and we re-route for the first time.
     *
     *  While unlikely, it could be that, for whatever reason, now all edges incident to node u are routed to some other side,
     *  s1. We then pick some edge e1 (could be the same or different from e0), forbid it from connecting to
     *  side s1, and re-route for a second time.
     *
     *  Again, for whatever reason, all edges could now connect to one
     *  of the two remaining sides, s2. Continuing in this way, we could be led to re-route a third and a fourth time. But
     *  prior to the fourth re-routing it would be the case that for each side si of node u, there was
     *  some edge ei incident to u that had been forbidden from connecting on side si. Therefore on the fourth
     *  re-routing it would be impossible for all edges to connect on any single side of u.
     *
     *  So much for the case of a single node. However, in again a highly unlikely worst-case-scenario, it could be
     *  that during the first five routings no other node besides u was a pseudoleaf (had all edges routed to one side),
     *  but after the fifth some other node became a pseudoleaf. In this way we could be led to do four re-routings
     *  for each node in the graph. QED
     *
     * In practice, it would probably be very rare for more that two routings to ever be necessary. For this
     * requires the odd circumstance, considered in the proof, that forbidding one edge from connecting on a
     * given side somehow results in /all/ edges incident at that node migrating to some other, single side.
     *
     * In order that our theory be tested, we use an infinite loop with counter and assertion, instead
     * of a mere for-loop which would fail silently.
     */
    size_t numRoutings = 0;
    size_t maxRoutings = 4*m_n + 1;
    while (true) {
        m_ra.router.processTransaction();
        log(++numRoutings);
        // As explained in the comments above, at most five routings should ever be needed.
        COLA_ASSERT(numRoutings <= maxRoutings);
        // For testing purposes, we may want to record the results of
        // each routing attempt.
        if (recordEachAttempt) {
            m_ra.recordRoutes(true);
            routingAttemptTglf.push_back(m_graph->writeTglf());
        }
        // Are there any nodes having all of their edges routed
        // out of just one side? This is what we want to prevent.
        // Such nodes would become leaves in a planarisation, so we
        // call them "pseudoleaves".
        Nodes pseudoLeaves;
        // For each such Node (if any), there is a sole direction in which
        // all connectors depart. We keep track of those directions as we work.
        vector<CardinalDir> soleDepartureDirecs;
        // Check each Node in the Graph:
        for (auto p : m_graph->getNodeLookup()) {
            Node_SP &u = p.second;
            const EdgesById edgeLookup = u->getEdgeLookup();
            // Sanity check, that Node u is not an actual leaf:
            COLA_ASSERT(edgeLookup.size() > 1);
            // Determine the departure direction from Node u for its first Edge.
            auto edge_it = edgeLookup.cbegin();
            CardinalDir d0 = departureDir((*edge_it).second, u);
            // If two or more directions have been used, some edge must depart
            // in a different direction than this one. (For if all the rest equal
            // this first one, then all are the same.)
            bool isPseudoLeaf = true;
            for (auto jt = ++edge_it; jt != edgeLookup.cend(); ++jt) {
                CardinalDir d1 = departureDir((*jt).second, u);
                if (d1 != d0) {
                    isPseudoLeaf = false;
                    break;
                }
            }
            if (isPseudoLeaf) {
                pseudoLeaves.push_back(u);
                soleDepartureDirecs.push_back(d0);
            }
        }
        // Are there any pseudoleaves?
        if (pseudoLeaves.empty()) {
            // If there are none, then we're done routing, and can break out of the outer while loop.
            break;
        } else {
            // But if there are still pseudoleaves, then we need to work on them.
            for (size_t i = 0; i < pseudoLeaves.size(); ++i) {
                // Get the Node and the direction in which all connectors currently depart from it.
                Node_SP u = pseudoLeaves[i];
                CardinalDir d0 = soleDepartureDirecs[i];
                // Now among all Edges incident at this Node we must select one that is still
                // allowed to depart in at least two directions (hence at least one different
                // from d0), and remove direction d0 from its list of allowed directions.
                //
                // If possible, we would like to choose such an Edge e such that if v is the Node
                // at the other end, then the predominant cardinal direction from Node u to Node v
                // be different than d0; for such would seem a suitable Edge to depart in a different
                // direction. However, such an Edge may not exist. In that case, we will just take
                // any one.
                Edge_SP candidate;
                for (auto p : u->getEdgeLookup()) {
                    Edge_SP &e = p.second;
                    // If this Edge is only allowed the one direction, then skip it.
                    if (isSoleDirec(m_allowedConnDirs.at(e->id()).at(u->id()))) continue;
                    // Otherwise mark it as the candidate.
                    candidate = e;
                    // Determine the predominant cardinal direction from Node u to the Node v at
                    // the opposite end of Edge e.
                    Node_SP v = e->getOtherEnd(*u);
                    CardinalDir d1 = Compass::cardinalDirection(u, v);
                    // If this is different from direction d0, then we're happy to accept this candidate.
                    if (d1 != d0) break;
                }
                // Start with the directions allowed last time:
                ConnDirFlags available = m_allowedConnDirs.at(candidate->id()).at(u->id());
                // XOR with the connection flag corresponding to cardinal direction d0,
                // so that this direction is no longer allowed.
                available ^= Compass::libavoidConnDirs.at(d0);
                // Record the new value.
                m_allowedConnDirs[candidate->id()][u->id()] = available;
                // Set a new ConnEnd.
                Point p = u->getCentre();
                ConnEnd end(p, available);
                ConnRef *cr = m_ra.edgeIdToConnRef.at(candidate->id());
                if (u->id() == candidate->getSourceEnd()->id()) {
                    cr->setSourceEndpoint(end);
                } else {
                    cr->setDestEndpoint(end);
                }
            }
        }
    }
    // Finally, the routing is done and we can set the connector routes in the Edge objects.
    m_ra.recordRoutes(true);
}
Beispiel #15
0
void Config::_stopNodes()
{
    // wait for the nodes to stop, destroy entities, disconnect
    Nodes stoppingNodes;
    const Nodes& nodes = getNodes();
    for( Nodes::const_iterator i = nodes.begin(); i != nodes.end(); ++i )
    {
        Node* node = *i;
        const State state = node->getState();
        if( state != STATE_STOPPED && state != STATE_FAILED )
            continue;

        EQASSERT( !node->isActive() || state == STATE_FAILED );
        if( node->isApplicationNode( ))
            continue;

        co::NodePtr netNode = node->getNode();
        if( !netNode ) // already disconnected
            continue;

        EQLOG( LOG_INIT ) << "Exiting node" << std::endl;

        if( state == STATE_FAILED )
            node->setState( STATE_STOPPED );

        stoppingNodes.push_back( node );
        EQASSERT( netNode.isValid( ));

        fabric::ServerDestroyConfigPacket destroyConfigPacket;
        destroyConfigPacket.configID = getID();
        netNode->send( destroyConfigPacket );

        ClientExitPacket clientExitPacket;
        netNode->send( clientExitPacket );
    }

    // now wait that the render clients disconnect
    uint32_t nSleeps = 50; // max 5 seconds for all clients
    for( Nodes::const_iterator i = stoppingNodes.begin();
            i != stoppingNodes.end(); ++i )
    {
        Node*        node    = *i;
        co::NodePtr netNode = node->getNode();
        node->setNode( 0 );

        if( nSleeps )
            while( netNode->isConnected() && --nSleeps )
                co::base::sleep( 100 ); // ms

        if( netNode->isConnected( ))
        {
            co::LocalNodePtr localNode = getLocalNode();
            EQASSERT( localNode.isValid( ));

            EQWARN << "Forcefully disconnecting exited render client node"
                   << std::endl;
            localNode->disconnect( netNode );
        }

        EQLOG( LOG_INIT ) << "Disconnected node" << std::endl;
    }
}
Beispiel #16
0
int main(int argc, char **argv)
{
  char *file_input = NULL;
  int c;
  
  Node node;
  Nodes nodes;
  struct Summary summary;
  memset(&summary, 0, sizeof(struct Summary));
  
  // options
  while ((c = getopt(argc, argv, "i:")) != -1) {
    switch (c) {
      case 'i':
        file_input = optarg;
        break;
      default:
        break;
    }
  }
  
  if (!file_input) {
    printf("Usage: ./build_tree -i inputs.txt\n");
    exit(EXIT_SUCCESS);
  }
  
  // read input file
  std::ifstream fin(file_input);
  if (!fin.is_open()) {
    std::cerr << "open file failure: " << file_input << std::endl;
    exit(EXIT_FAILURE);
  }
  while (!fin.eof()) {
    std::string uid;
    std::string balance;
    if (!std::getline(fin, uid, '\t') || !std::getline(fin, balance, '\n')) {
      break;
    }
    make_user_node(uid.c_str(), atoll(balance.c_str()), &node);
    nodes.push_back(node);
    summary.sum += node.sum;
  }
  fin.close();
  summary.user_count = nodes.size();
  
  // nodes at level 0 should be sorted
  std::sort(nodes.begin(), nodes.end());
  
  int idx = 0;
  Nodes parents;
  parents.reserve(nodes.size()%2 + 1);
  while (nodes.size() > 1) {
    if (nodes.size() % 2 == 1) {
      summary.padding_sum += nodes[nodes.size()-1].sum;
      nodes.push_back(nodes[nodes.size()-1]);
    }
    
    for (Nodes::iterator it = nodes.begin(); it != nodes.end(); it++) {
      std::cout << idx++ << "\t" << summary.level << "\t" << it->sum << "\t";
      dump_hex(it->hash, 8);
      std::cout << std::endl;
    }
    parents.resize(0);
    build_parent_nodes(&nodes, &parents);
    nodes = parents;
    summary.level++;
  }
  std::cout << idx++ << "\t" << summary.level << "\t" << nodes[0].sum << "\t";
  dump_hex(nodes[0].hash, 8);
  std::cout << std::endl;
  
  std::cout << "summary:\t" << summary.user_count << "\t" << summary.sum << "\t"
    << summary.padding_sum << "\t" << summary.level << std::endl;
  
  return 0;
}
Beispiel #17
0
void	xrSmoothNodes()
{
	Nodes	smoothed;	smoothed.reserve(g_nodes.size());
	Marks	mark;		mark.assign(g_nodes.size(),false);

	int inv_count = 0;
	for (u32 i=0; i<g_nodes.size(); i++)
	{
		vertex& N = g_nodes[i];

		Fvector	P1,P2,P3,P4,P,REF;
		int		c;

		// smooth point LF
		{
			bool	bCorner = false;

			c=1;	N.PointLF(REF);	P1.set(REF);
			if (N.nLeft()!=InvalidNode) {
				vertex& L = g_nodes[N.nLeft()];

				L.PointFR(P);	merge(P1);
				if (L.nForward()!=InvalidNode) {
					bCorner = true;
					vertex& C = g_nodes[L.nForward()];

					C.PointRB(P);	merge(P1);
				}
			}
			if (N.nForward()!=InvalidNode) {
				vertex& F = g_nodes[N.nForward()];

				F.PointBL(P);	merge(P1);
				if ((!bCorner) && (F.nLeft()!=InvalidNode)) {
					bCorner = true;

					vertex& C = g_nodes[F.nLeft()];
					C.PointRB(P);	merge(P1);
				}
			}
			R_ASSERT(c<=4);
			P1.div(float(c));
		}

		// smooth point FR
		{
			bool	bCorner = false;

			c=1;	N.PointFR(REF); P2.set(REF);
			if (N.nForward()!=InvalidNode) {
				vertex& F = g_nodes[N.nForward()];

				F.PointRB(P);	merge(P2);
				if (F.nRight()!=InvalidNode) {
					bCorner = true;
					vertex& C = g_nodes[F.nRight()];

					C.PointBL(P);	merge(P2);
				}
			}
			if (N.nRight()!=InvalidNode) {
				vertex& R = g_nodes[N.nRight()];

				R.PointLF(P);	merge(P2);
				if ((!bCorner) && (R.nForward()!=InvalidNode)) {
					bCorner = true;

					vertex& C = g_nodes[R.nForward()];
					C.PointBL(P);	merge(P2);
				}
			}
			R_ASSERT(c<=4);
			P2.div(float(c));
		}

		// smooth point RB
		{
			bool	bCorner = false;

			c=1;	N.PointRB(REF); P3.set(REF);
			if (N.nRight()!=InvalidNode) {
				vertex& R = g_nodes[N.nRight()];

				R.PointBL(P);	merge(P3);
				if (R.nBack()!=InvalidNode) {
					bCorner = true;
					vertex& C = g_nodes[R.nBack()];

					C.PointLF(P);	merge(P3);
				}
			}
			if (N.nBack()!=InvalidNode) {
				vertex& B = g_nodes[N.nBack()];

				B.PointFR(P);	merge(P3);
				if ((!bCorner) && (B.nRight()!=InvalidNode)) {
					bCorner = true;

					vertex& C = g_nodes[B.nRight()];
					C.PointLF(P);	merge(P3);
				}
			}
			R_ASSERT(c<=4);
			P3.div(float(c));
		}

		// smooth point BL
		{
			bool	bCorner = false;

			c=1;	N.PointBL(REF); P4.set(REF);
			if (N.nBack()!=InvalidNode) {
				vertex& B = g_nodes[N.nBack()];

				B.PointLF(P);	merge(P4);
				if (B.nLeft()!=InvalidNode) {
					bCorner = true;
					vertex& C = g_nodes[B.nLeft()];

					C.PointFR(P);	merge(P4);
				}
			}
			if (N.nLeft()!=InvalidNode) {
				vertex& L = g_nodes[N.nLeft()];

				L.PointRB(P);	merge(P4);
				if ((!bCorner) && (L.nBack()!=InvalidNode)) {
					bCorner = true;

					vertex& C = g_nodes[L.nBack()];
					C.PointFR(P);	merge(P4);
				}
			}
			R_ASSERT(c<=4);
			P4.div(float(c));
		}

		// align plane
		Fvector data[4]; data[0]=P1; data[1]=P2; data[2]=P3; data[3]=P4;
		Fvector vOffs,vNorm,D;
		vNorm.set(N.Plane.n);
		vOffs.set(N.Pos);
		Mgc::OrthogonalPlaneFit(
			4,(Mgc::Vector3*)data,
			*((Mgc::Vector3*)&vOffs),
			*((Mgc::Vector3*)&vNorm)
		);
		if (vNorm.y<0) vNorm.invert();

		// create _new node
		vertex NEW = N;
		NEW.Plane.build	(vOffs,vNorm);
		D.set			(0,1,0);
		N.Plane.intersectRayPoint(N.Pos,D,NEW.Pos);	// "project" position
		smoothed.push_back	(NEW);

		// verify placement
		/*
		mark[i]			= !!ValidNode	(NEW);

		if (!mark[i])	inv_count++;.
		*/
	}
	g_nodes = smoothed;

	if (inv_count) Msg("%d invalid nodes detected",inv_count);
}
Beispiel #18
0
TableInfo phuffman::utility::BuildTable(InputIterator first, InputIterator last, Codes& table) {
    using namespace std;
    typedef DepthCounterNode Node;
    typedef multimap<size_t, Node*> HuffmanTree;
    typedef vector<Node*> Nodes;

    assert(distance(first, last) <= constants::MAXIMUM_DATABLOCK_SIZE);
    assert(table.size() >= constants::ALPHABET_SIZE);

    HuffmanTree tree;
    Nodes leafs;
    TableInfo info;

    // Initialize tree
    {
        Frequencies frequencies = CountFrequencies(first, last);
        Frequencies::const_iterator first = frequencies.begin(), last = frequencies.end();
        while (first != last) {
            Node* leaf = new Node(first->symbol);
            tree.insert(make_pair(first->frequency, leaf));
            leafs.push_back(leaf);
            ++first;
        }
    }

    // Build tree
    {
        for (size_t i=0, size=tree.size(); i<size-1; ++i) {
            HuffmanTree::iterator first = tree.begin(), second = tree.begin();
            ++second;
            size_t freq = first->first + second->first; // Calculate freq for a node
            Node* node = new Node(first->second, second->second); 
            ++second;
            tree.erase(first, second); // Remove two nodes with the smallest frequency
            tree.insert(make_pair(freq, node)); // Add node that points to previosly removed nodes
        }
        assert(tree.size() == 1);
    }

    // Count codelengths
    // In fact, codelengths are already counted in the 'depth' member of a node
    // There is only one exception: if the tree contains only one object, we need to set it's depth manually
    Node* root = tree.begin()->second;
    root->depth = 1;

    // Sort nodes by codelength
    sort(leafs.begin(), leafs.end(), TreeComparator);

    // Build table
    {
        Nodes::const_iterator first = leafs.begin(), last = leafs.end();
        Node *curNode = *first;
        info.maximum_codelength = curNode->depth;
        Code curCode = CodeMake(curNode->depth, 0);
        table[curNode->element] = curCode;
        ++first;
        while (first != last) {
            assert(curNode->depth >= curCode.codelength);
            curNode = *first;
            // If current codeword and next codeword have equal lengths
            if (curNode->depth == curCode.codelength) {
            // Just increase codeword by 1
                curCode.code += 1;
            }
            // Otherwise
            else {
                // Increase codeword by 1 and _after_ that shift codeword right
                curCode.code = (curCode.code + 1) >> (curNode->depth - curCode.codelength);
            }
            curCode.codelength = curNode->depth;
            table[curNode->element] = curCode;
            ++first;
        }
    }
    
    delete root;

    return info;
}
Beispiel #19
0
Nodes Node::children(void) const {
	Nodes children;
	for(auto child : pimpl_->children()) children.push_back(child);
	return children;
}
Beispiel #20
0
void Evaluator::subst_macros()
{
    int cntr = 0;
    Token gtok(0, 0);
    while (1)
    {
        if (++cntr > MAX_SUBST)
            throw Err("Too many macro substitutions: " + bug::to_string(MAX_SUBST) + ", possible recursion", gtok);

        bool weresubs = false;

        Nodes old = root->children;
        root->children.clear();

        Nodes leftovers;
        for (auto i : old)
        {
            Instruction * pin = get<Instruction>(NOTHR, i);
            if (pin)
            {
                for (auto j : leftovers)
                    i->children[0]->children[1]->children.push_back(j);

                leftovers.clear();

                root->addChild(i);
                continue;
            }

            Macuse * u = get<Macuse>(LNFUN, i);
            gtok = u->tok();

            if (u->name() == "@end")
            {
                for (auto j : u->children[0]->children)
                    leftovers.push_back(j);
                continue;
            }

            Nodes inject = Macros(root).process_macuse(*u);

            if (!inject.empty())
            {
                Pnode pn = inject.front();
                Instruction * pin = get<Instruction>(NOTHR, pn);
                if (pin)
                {
                    for (auto j : leftovers)
                        pn->children[0]->children[1]->children.push_back(j);
                }
                else
                {
                    Macuse * pu = get<Macuse>(LNFUN, pn);
                    for (auto j : leftovers)
                        pu->children[0]->children.push_back(j);
                }

                leftovers.clear();
            }

            for (auto j : inject)
                root->addChild(j);

            weresubs = true;
        }

        if (!weresubs)
        {
            if (leftovers.empty())
                break;

            if (root->children.empty())
                throw Err("Labels used in empty program");

            throw Err("Program finishes with label (see macro definition)", root->children.back()->tok());
        }
        // this can be improved later (one extra loop)
        // when expanding macro we can detect that no new macro introduced
    } // while
}
Beispiel #21
0
Nodes Confinement::table()
{
    Unumber N = root->comp->proc.N;
    Unumber N2 = root->comp->proc.N2;
    Token tok = mu.tok();

    if (N.iszero())
        throw Err("Macro _table requires N", tok);

    if (mu.children.size() != 7)
        throw Err("Macro _table requires 6 arguments ("
                  + bug::to_string(mu.children.size() - 1) + ")", tok);

    string type;
    {
        // sanity check
        get<Expr>(LNFUN, mu.children[1]);
        get<Term>(LNFUN, mu.children[1]->children[0]);

        Idn * id = get<Idn>(NOTHR, mu.children[1]->children[0]->children[0]);
        if (id)
            type = id->s;
    }

    Cell gr = get<Expr>(LNFUN, mu.children[2])->val();
    Cell p1 = get<Expr>(LNFUN, mu.children[3])->val();
    Cell c1 = get<Expr>(LNFUN, mu.children[4])->val();
    Cell p2 = get<Expr>(LNFUN, mu.children[5])->val();
    Cell c2 = get<Expr>(LNFUN, mu.children[6])->val();

    //if (!gr.ts().s.iszero()) throw Err("Argument must be open value: (" + gr.str() + ")", mu.children[2]->tok());
    if (!p1.ts().s.iszero()) throw Err("Argument must be open value: (" + p1.str() + ")", mu.children[3]->tok());
    if (!p2.ts().s.iszero()) throw Err("Argument must be open value: (" + p2.str() + ")", mu.children[5]->tok());
    Unumber grn = gr.x();
    Unumber p1n = p1.ts().t;
    Unumber p2n = p2.ts().t;

    Unumber gc = grn;
    std::vector<Cell> vm;

    int cntr = ORDMAX;
    while (--cntr > 0)
    {
        Unumber k = gc;
        k.pow(p1n, N2);
        k = k.mul(c1.x(), N2);

        Unumber v = gc;
        v.pow(p2n, N2);
        v = v.mul(c2.x(), N2);

        vm.push_back(Cell(k, Cell::X));
        vm.push_back(Cell(v, Cell::X));

        gc = gc.mul(grn, N2);

        if (gc == grn)
            break;
    }

    if (!cntr)
        throw Err("Table order is too high - increase constant ORDMAX ("
                  + bug::to_string(ORDMAX) + ")", tok);

    Instruction * pi = new Instruction(tok);

    pi->typ = Instruction::eData;

    if (type == "_map_")
        for (decltype(vm.size()) i = 0; i < vm.size(); i += 2)
        {
            Cell address = vm[i];
            Cell value = vm[i + 1];
            Pnode itm = item(tsnum(tok, value));

            Pnode labels(new Labels(tok));
            labels->addChild(tsnum(tok, address));

            Litem * r_litem = new Litem(tok, labels, itm, Litem::eItm);
            Pnode litem(r_litem);

            pi->addChild(litem);
        }
    else if (type == "_array_")
        for (auto i : vm)
        {
            Pnode itm = item(tsnum(tok, i));

            Pnode labels(new Labels(tok));
            Litem * r_litem = new Litem(tok, labels, itm, Litem::eItm);
            Pnode litem(r_litem);

            pi->addChild(litem);
        }
    else
        throw Err("Macro _table requires type: '_map_' or '_array_'", tok);

    Nodes r;
    r.push_back(Pnode(pi));
    return r;
}