float KernelKmeansClusterer::distanceToCluster(int sampleId,
		const boost::unordered_set<int>& clusterIdSet) {
	if (clusterIdSet.empty()) {
		return FLT_MAX;
	}
	// the sum of weight of the cluster
	float weightSum = 0.0f;
	// the sum of kernels from the sample to all samples in the cluster, multiplied by the weight
	float kernelSum = 0.0f;
	// the sum of kernels from each pair of samples in the cluster, multiplied by weights
	float kernelPairSum = 0.0f;
	for (boost::unordered_set<int>::const_iterator iter = clusterIdSet.begin();
			iter != clusterIdSet.end(); ++iter) {
		float weight = mWeightArray[*iter];
		weightSum += weight;
		float kernelResult = (*mpKernel)(sampleId, *iter);
		kernelSum += weight * kernelResult;
		for (boost::unordered_set<int>::const_iterator _iter =
				clusterIdSet.begin(); _iter != clusterIdSet.end(); ++_iter) {
			float _kernelResult = (*mpKernel)(*iter, *_iter);
			kernelPairSum += weight * mWeightArray[*_iter] * _kernelResult;
		}
	}
	float kernelSelf = (*mpKernel)(sampleId, sampleId);
	if (weightSum == 0.0f) {
		return FLT_MAX;
	}
	return kernelSelf - 2 * kernelSum / weightSum
			+ kernelPairSum / (weightSum * weightSum);
}
Beispiel #2
0
 inline Observer& Observer::operator=(const Observer& o) {
     iterator i;
     for (i=observables_.begin(); i!=observables_.end(); ++i)
         (*i)->unregisterObserver(this);
     observables_ = o.observables_;
     for (i=observables_.begin(); i!=observables_.end(); ++i)
         (*i)->registerObserver(this);
     return *this;
 }
Beispiel #3
0
    inline void ObservableSettings::registerDeferredObservers(boost::unordered_set<Observer*>& observers) {
        if (updatesDeferred())
	{
		QL_TRACE("adding " << observers.size() << " observers to the deferred list");
        	deferredObservers_.insert(observers.begin(), observers.end());
	}
    }
Beispiel #4
0
void VNode::collect_vnodes(boost::unordered_set<Node*>& nodes,uint& total)
{
	total++;
	boost::unordered_set<Node*>::iterator it = nodes.find(this);
	if(it==nodes.end())
		nodes.insert(this);
}
Beispiel #5
0
//calculates average of vectors
boost::unordered_map<string, double> Cluster::_centroid(boost::unordered_set<int>& cluster_list) {

  unordered_string_map centroid;

  int cluster_size = cluster_list.size();
  
  for (boost::unordered_set<int>::iterator it = cluster_list.begin(); it != cluster_list.end(); ++it) {

    //add each element of vector to centroid
    for (unordered_string_map::iterator ivec = doc_term_index[ *it ].begin(); ivec != doc_term_index[ *it ].end(); ++ivec) {
  
      //element (term) doesn't exist, we add it to the map
      if ( centroid.count ( ivec->first ) == 0) {
	//cout << "ivec second: " << ivec->second <<endl;
	//cout << "cluster_size: " << cluster_size <<endl;

	centroid.insert(unordered_string_map::value_type( ivec->first, double( (double)ivec->second / cluster_size ) ));
      }
      else {
	centroid[ivec->first] += double( ((double)ivec->second / cluster_size) );
      }

    }
  }    
  return centroid;
}
PNS_NODE::~PNS_NODE()
{
    TRACE( 0, "PNS_NODE::delete %p", this );

    if( !m_children.empty() )
    {
        TRACEn( 0, "attempting to free a node that has kids.\n" );
        assert( false );
    }

#ifdef DEBUG
    if( allocNodes.find( this ) == allocNodes.end() )
    {
        TRACEn( 0, "attempting to free an already-free'd node.\n" );
        assert( false );
    }

    allocNodes.erase( this );
#endif

    for( PNS_INDEX::ITEM_SET::iterator i = m_index->begin(); i != m_index->end(); ++i )
    {
        if( (*i)->BelongsTo( this ) )
            delete *i;
    }


    releaseGarbage();
    unlinkParent();

    delete m_index;
}
Beispiel #7
0
    inline void ObservableSettings::enableUpdates() {
    	updatesEnabled_  = true;
    	updatesDeferred_ = false;

    	// if there are outstanding deferred updates, do the notification
        if (deferredObservers_.size() > 0)
        {
            bool successful = true;
            std::string errMsg;

            QL_TRACE("deferred notification of " << deferredObservers_.size() << " observers");
            for (iterator i=deferredObservers_.begin(); i!=deferredObservers_.end(); ++i) {
                try {
                    (*i)->update();
                } catch (std::exception& e) {
                    successful = false;
                    errMsg = e.what();
                } catch (...) {
                    successful = false;
                }
            }

            deferredObservers_.clear();

            QL_ENSURE(successful,
                  "could not notify one or more observers: " << errMsg);
        }
    }
		static void serialize(storage_type& s, const boost::unordered_set<T>& o)
			{
			uint32_t sz = o.size();
			s.serialize(sz);

			for (auto it = o.begin(), it_end = o.end(); it != it_end; ++it)
				s.serialize(*it);
			}
void cluster_machine::dfs(const size_t curr, boost::unordered_set<size_t> &vis)
{
    vis.insert(curr);
    for (size_t e = first_[curr]; e != -1; e = next_[e]) {
        if ( vis.find(v_[e]) == vis.end() )
            dfs(v_[e], vis);
    }
}
Beispiel #10
0
 inline std::pair<boost::unordered_set<boost::shared_ptr<Observable> >::iterator, bool>
 Observer::registerWith(const boost::shared_ptr<Observable>& h) {
     if (h) {
         h->registerObserver(this);
         return observables_.insert(h);
     }
     return std::make_pair(observables_.end(), false);
 }
Beispiel #11
0
Strings get_live_object_names() {
  IMP::Vector<std::string> ret;
  for (boost::unordered_set<Object*>::const_iterator it = live_.begin();
       it != live_.end(); ++it) {
    ret.push_back((*it)->get_name());
  }
  return ret;
}
Beispiel #12
0
IMPKERNEL_END_NAMESPACE
IMPKERNEL_BEGIN_INTERNAL_NAMESPACE
void check_live_objects() {
  for (boost::unordered_set<Object*>::const_iterator it = live_.begin();
       it != live_.end(); ++it) {
    IMP_USAGE_CHECK((*it)->get_ref_count() > 0,
                    "Object " << (*it)->get_name() << " is not ref counted.");
  }
}
		void update(const key_type& inKey, const boost::unordered_set<value_type>& inValues)
			{
				{
				boost::unordered_set<value_type> curValues(getValues(inKey));
				for (auto it = curValues.begin(); it != curValues.end(); ++it)
					if (inValues.find(*it) == inValues.end())
						drop(inKey, *it);
				}

			insert(inKey, inValues);
			}
Beispiel #14
0
IMPKERNEL_END_NAMESPACE
IMPKERNEL_BEGIN_INTERNAL_NAMESPACE
void check_live_objects() {
  for (boost::unordered_set<Object*>::const_iterator it = live_.begin();
       it != live_.end(); ++it) {
    IMP_USAGE_CHECK((*it)->get_ref_count() > 0,
                    "Object " << (*it)->get_name()
                    << " is alive but not ref counted - memory leakage possible."
                    << " This usually happens if an owning pointer is released"
                    << " without being either deleted manually or assigned to"
                    << " another owning pointer.");
  }
}
void save(Archive & ar, const boost::unordered_set<T> & t, unsigned int version)
{
    // write the size
    // TODO: should we handle bucket size as well?
    typedef typename boost::unordered_set<T>::size_type size_type;
    typedef typename boost::unordered_set<T>::const_iterator const_iterator;
    size_type size = t.size();
    ar & BOOST_SERIALIZATION_NVP(size);
    unsigned int count = 0;
    for (const_iterator it = t.begin(); it != t.end(); it++) {
        ar & boost::serialization::make_nvp("item" + count, *it);
        count++;
    }
}
Beispiel #16
0
static bool collectShortcuts(const std::string & str, StringVector & vs) 
{
	static boost::unordered_set<std::string> commonFilters;
	if(commonFilters.empty()) {
#if RULE_KEY_HASH_LENGTH==7 // 7
		commonFilters.insert("http://");
		commonFilters.insert("ttp://w");
		commonFilters.insert("tp://ww");
		commonFilters.insert("p://www");
		commonFilters.insert("://www.");
#elif RULE_KEY_HASH_LENGTH==8 // 8
		commonFilters.insert("http://w");
		commonFilters.insert("ttp://ww");
		commonFilters.insert("tp://www");
		commonFilters.insert("p://www.");
#elif RULE_KEY_HASH_LENGTH==9 // 9
		commonFilters.insert("http://ww");
		commonFilters.insert("ttp://www");
		commonFilters.insert("tp://www.");
#endif
	}

	int i = 0;
	bool isFindShoutcut = false;
	while (i < abpmin(str.length() - RULE_KEY_HASH_LENGTH,80)) 
	{
		unsigned int j = i;
		for (; j < str.length(); j++) {
			if ((str[j] == '*' || str[j] == '^'))
			{
				break;
			}
		}
		for (unsigned int k = i; j - k >= RULE_KEY_HASH_LENGTH; k++) 
		{
			std::string key = str.substr(k, RULE_KEY_HASH_LENGTH);
			if(commonFilters.find(key)!=commonFilters.end())
				continue;
			isFindShoutcut = true;
			vs.push_back(key); //append(key);
		}
		i = j + 1;
	}
	return isFindShoutcut;
}
Beispiel #17
0
int PNS_NODE::QueryColliding( const PNS_ITEM* aItem,
        PNS_NODE::Obstacles& aObstacles, int aKindMask, int aLimitCount )
{
    obstacleVisitor visitor( aObstacles, aItem, aKindMask );

    assert( allocNodes.find( this ) != allocNodes.end() );

    visitor.SetCountLimit( aLimitCount );
    visitor.SetWorld( this, NULL );

    // first, look for colliding items ourselves
    m_index->Query( aItem, m_maxClearance, visitor );

    // if we haven't found enough items, look in the root branch as well.
    if( !isRoot() && ( visitor.m_matchCount < aLimitCount || aLimitCount < 0) )
    {
        visitor.SetWorld( m_root, this );
        m_root->m_index->Query( aItem, m_maxClearance, visitor );
    }

    return aObstacles.size();
}
Beispiel #18
0
    inline void Observable::notifyObservers() {
    	// check whether the notifications should be triggered
    	if (!settings_.updatesEnabled())
    	{
    	    // if updates are only deferred, flag this for later notification
    	    // these are held centrally by the settings singleton
            if (settings_.updatesDeferred())
    		settings_.registerDeferredObservers(observers_);

    	    return;
    	}

        if (observers_.size() > 0)
        {
            bool successful = true;
            std::string errMsg;

            QL_TRACE("direct notification of " << observers_.size() << " observers");
            for (iterator i=observers_.begin(); i!=observers_.end(); ++i) {
                try {
                   (*i)->update();
                } catch (std::exception& e) {
                    // quite a dilemma. If we don't catch the exception,
                    // other observers will not receive the notification
                    // and might be left in an incorrect state. If we do
                    // catch it and continue the loop (as we do here) we
                    // lose the exception. The least evil might be to try
                    // and notify all observers, while raising an
                    // exception if something bad happened.
                    successful = false;
                    errMsg = e.what();
                } catch (...) {
                    successful = false;
                }
            }
            QL_ENSURE(successful,
                  "could not notify one or more observers: " << errMsg);
        }
    }
Beispiel #19
0
PNS_NODE::~PNS_NODE()
{
    if( !m_children.empty() )
    {
        TRACEn( 0, "attempting to free a node that has kids.\n" );
        assert( false );
    }

    if( allocNodes.find( this ) == allocNodes.end() )
    {
        TRACEn( 0, "attempting to free an already-free'd node.\n" );
        assert( false );
    }

    allocNodes.erase( this );

    for( PNS_INDEX::ItemSet::iterator i = m_index->begin();
         i != m_index->end(); ++i )
        if( (*i)->BelongsTo( this ) )
            delete *i;

    unlinkParent();
    delete m_index;
}
Beispiel #20
0
    bool isLocalSipUri( const string& requestUri ) {

        static bool initialized = false ;
        static boost::unordered_set<string> setLocalUris ;

        if( !initialized ) {
            initialized = true ;

            nta_agent_t* agent = theOneAndOnlyController->getAgent() ;
            tport_t *t = nta_agent_tports( agent ) ;
            for (tport_t* tport = t; tport; tport = tport_next(tport) ) {
                const tp_name_t* tpn = tport_name( tport );
                if( 0 == strcmp( tpn->tpn_host, "*") ) 
                    continue ;

                string localUri = tpn->tpn_host ;
                localUri += ":" ;
                localUri += (tpn->tpn_port ? tpn->tpn_port : "5060");

                setLocalUris.insert( localUri ) ;

                if( 0 == strcmp(tpn->tpn_host,"127.0.0.1") ) {
                    localUri = "localhost:" ;
                    localUri += (tpn->tpn_port ? tpn->tpn_port : "5060");
                    setLocalUris.insert( localUri ) ;
                }
            }
       }

        url_t *url = url_make(theOneAndOnlyController->getHome(), requestUri.c_str() ) ;
        string uri = url->url_host ;
        uri += ":" ;
        uri += ( url->url_port ? url->url_port : "5060") ;

        return setLocalUris.end() != setLocalUris.find( uri ) ;
    }
Beispiel #21
0
void Object::remove_live_object(Object* o) {
  IMP_INTERNAL_CHECK(live_.find(o) != live_.end(),
                     "Object " << o->get_name() << " not found in live list.");
  live_.erase(o);
}
Beispiel #22
0
Objects get_live_objects() {
  Objects ret(live_.begin(), live_.end());
  return ret;
}
 /**
   * Return the mirror information of the vertex in the i'th position.
   */
  inline const std::vector<graph_shard_id_t> mirrors(size_t i) const {
    const boost::unordered_set<graph_shard_id_t> mirrorset = shard_impl.vertex_mirrors[i];
    std::vector<graph_shard_id_t> ret(mirrorset.begin(), mirrorset.end());
    return ret;
  }
 static void exec(OutArcType& oarc, const boost::unordered_set<T>& vec){
   serialize_iterator(oarc,
                      vec.begin(), vec.end(), vec.size());
 }
Beispiel #25
0
 inline Observer::~Observer() {
     for (iterator i=observables_.begin(); i!=observables_.end(); ++i)
         (*i)->unregisterObserver(this);
 }
// Make sure an offer is still valid. If not, mark it unfunded.
bool OfferCreateTransactor::bValidOffer (
    SLE::ref            sleOfferDir,
    uint256 const&      uOfferIndex,
    const uint160&      uOfferOwnerID,
    const STAmount&     saOfferPays,
    const STAmount&     saOfferGets,
    const uint160&      uTakerAccountID,
    boost::unordered_set<uint256>&  usOfferUnfundedFound,
    boost::unordered_set<uint256>&  usOfferUnfundedBecame,
    boost::unordered_set<uint160>&  usAccountTouched,
    STAmount&           saOfferFunds)                       // <--
{
    bool    bValid;

    if (sleOfferDir->isFieldPresent (sfExpiration) && sleOfferDir->getFieldU32 (sfExpiration) <= mEngine->getLedger ()->getParentCloseTimeNC ())
    {
        // Offer is expired. Expired offers are considered unfunded. Delete it.
        WriteLog (lsINFO, OfferCreateTransactor) << "bValidOffer: encountered expired offer";

        usOfferUnfundedFound.insert (uOfferIndex);

        bValid  = false;
    }
    else if (uOfferOwnerID == uTakerAccountID)
    {
        // Would take own offer. Consider old offer expired. Delete it.
        WriteLog (lsINFO, OfferCreateTransactor) << "bValidOffer: encountered taker's own old offer";

        usOfferUnfundedFound.insert (uOfferIndex);

        bValid  = false;
    }
    else if (!saOfferGets.isPositive () || !saOfferPays.isPositive ())
    {
        // Offer has bad amounts. Consider offer expired. Delete it.
        WriteLog (lsWARNING, OfferCreateTransactor) << boost::str (boost::format ("bValidOffer: BAD OFFER: saOfferPays=%s saOfferGets=%s")
                % saOfferPays % saOfferGets);

        usOfferUnfundedFound.insert (uOfferIndex);
    }
    else
    {
        WriteLog (lsTRACE, OfferCreateTransactor) << "bValidOffer: saOfferPays=" << saOfferPays.getFullText ();

        saOfferFunds    = mEngine->getNodes ().accountFunds (uOfferOwnerID, saOfferPays);

        if (!saOfferFunds.isPositive ())
        {
            // Offer is unfunded, possibly due to previous balance action.
            WriteLog (lsDEBUG, OfferCreateTransactor) << "bValidOffer: offer unfunded: delete";

            boost::unordered_set<uint160>::iterator account = usAccountTouched.find (uOfferOwnerID);

            if (account != usAccountTouched.end ())
            {
                // Previously touched account.
                usOfferUnfundedBecame.insert (uOfferIndex); // Delete unfunded offer on success.
            }
            else
            {
                // Never touched source account.
                usOfferUnfundedFound.insert (uOfferIndex);  // Delete found unfunded offer when possible.
            }

            bValid  = false;
        }
        else
        {
            bValid  = true;
        }
    }

    return bValid;
}
Beispiel #27
0
 inline Observer::Observer(const Observer& o)
 : observables_(o.observables_) {
     for (iterator i=observables_.begin(); i!=observables_.end(); ++i)
         (*i)->registerObserver(this);
 }
Beispiel #28
0
 inline void ObservableSettings::registerDeferredObservers(
     const boost::unordered_set<Observer*>& observers) {
     if (updatesDeferred()) {
         deferredObservers_.insert(observers.begin(), observers.end());
     }
 }
Beispiel #29
0
void Grid::processDiscoveredCells(Player &player, std::vector<SharedCell> &playerCells, const boost::unordered_set<CellID> &discoveredCells)
{
    playerCells.push_back(SharedCell(new Cell()));;
    if (player.enabledItems[STREAMER_TYPE_OBJECT])
    {
        boost::unordered_map<int, Item::SharedObject>::iterator o = player.visibleCell->objects.begin();
        while (o != player.visibleCell->objects.end())
        {
            boost::unordered_set<CellID>::iterator d = discoveredCells.find(o->second->cell->cellID);
            if (d != discoveredCells.end())
            {
                o = player.visibleCell->objects.erase(o);
            }
            else
            {
                ++o;
            }
        }
        playerCells.back()->objects.swap(player.visibleCell->objects);
    }
    if (player.enabledItems[STREAMER_TYPE_CP])
    {
        boost::unordered_map<int, Item::SharedCheckpoint>::iterator c = player.visibleCell->checkpoints.begin();
        while (c != player.visibleCell->checkpoints.end())
        {
            boost::unordered_set<CellID>::iterator d = discoveredCells.find(c->second->cell->cellID);
            if (d != discoveredCells.end())
            {
                c = player.visibleCell->checkpoints.erase(c);
            }
            else
            {
                ++c;
            }
        }
        playerCells.back()->checkpoints.swap(player.visibleCell->checkpoints);
    }
    if (player.enabledItems[STREAMER_TYPE_RACE_CP])
    {
        boost::unordered_map<int, Item::SharedRaceCheckpoint>::iterator t = player.visibleCell->raceCheckpoints.begin();
        while (t != player.visibleCell->raceCheckpoints.end())
        {
            boost::unordered_set<CellID>::iterator d = discoveredCells.find(t->second->cell->cellID);
            if (d != discoveredCells.end())
            {
                t = player.visibleCell->raceCheckpoints.erase(t);
            }
            else
            {
                ++t;
            }
        }
        playerCells.back()->raceCheckpoints.swap(player.visibleCell->raceCheckpoints);
    }
    if (player.enabledItems[STREAMER_TYPE_MAP_ICON])
    {
        boost::unordered_map<int, Item::SharedMapIcon>::iterator m = player.visibleCell->mapIcons.begin();
        while (m != player.visibleCell->mapIcons.end())
        {
            boost::unordered_set<CellID>::iterator d = discoveredCells.find(m->second->cell->cellID);
            if (d != discoveredCells.end())
            {
                m = player.visibleCell->mapIcons.erase(m);
            }
            else
            {
                ++m;
            }
        }
        playerCells.back()->mapIcons.swap(player.visibleCell->mapIcons);
    }
    if (player.enabledItems[STREAMER_TYPE_3D_TEXT_LABEL])
    {
        boost::unordered_map<int, Item::SharedTextLabel>::iterator t = player.visibleCell->textLabels.begin();
        while (t != player.visibleCell->textLabels.end())
        {
            boost::unordered_set<CellID>::iterator d = discoveredCells.find(t->second->cell->cellID);
            if (d != discoveredCells.end())
            {
                t = player.visibleCell->textLabels.erase(t);
            }
            else
            {
                ++t;
            }
        }
        playerCells.back()->textLabels.swap(player.visibleCell->textLabels);
    }
    if (player.enabledItems[STREAMER_TYPE_AREA])
    {
        boost::unordered_map<int, Item::SharedArea>::iterator a = player.visibleCell->areas.begin();
        while (a != player.visibleCell->areas.end())
        {
            boost::unordered_set<CellID>::iterator d = discoveredCells.find(a->second->cell->cellID);
            if (d != discoveredCells.end())
            {
                a = player.visibleCell->areas.erase(a);
            }
            else
            {
                ++a;
            }
        }
        playerCells.back()->areas.swap(player.visibleCell->areas);
    }
}
Beispiel #30
0
 inline void Observer::unregisterWithAll() {
     for (iterator i=observables_.begin(); i!=observables_.end(); ++i)
         (*i)->unregisterObserver(this);
     observables_.clear();
 }