Example #1
0
std::pair<unit_map::unit_iterator, bool> unit_map::move(const map_location &src, const map_location &dst) {
	self_check();
	DBG_NG << "Unit map: Moving unit from " << src << " to " << dst << "\n";

	//Find the unit at the src location
	t_lmap::iterator i = lmap_.find(src);
	if(i == lmap_.end()) { return std::make_pair(make_unit_iterator(i), false);}

	t_umap::iterator uit(i->second);

	if(src == dst){ return std::make_pair(make_unit_iterator(uit), true);}

	//Fail if there is no unit to move
	unit_ptr p = uit->second.unit;
	if(!p){ return std::make_pair(make_unit_iterator(uit), false);}

	p->set_location(dst);

	lmap_.erase(i);

	std::pair<t_lmap::iterator,bool> res = lmap_.insert(std::make_pair(dst, uit));

	//Fail and don't move if the destination is already occupied
	if(res.second == false) {
		p->set_location(src);
		lmap_.insert(std::make_pair(src, uit));
		return std::make_pair(make_unit_iterator(uit), false);
	}

	self_check();

	return std::make_pair(make_unit_iterator(uit), true);
}
/** error_recovery_externally_changed_uid is called when an attempt is made to
	delete a umap_ element, which fails because the underlying_id() has been changed externally.
	It searches the entire umap_ to for a matching ilist_ element to erase.
 */
void unit_map::error_recovery_externally_changed_uid(t_ilist::iterator const & lit) const {
	std::string name, id ;
	size_t uid;
	if(lit->unit != NULL){
		unit const * u(lit->unit);
		name = u->name();
		id = u->id();
		uid = u->underlying_id();
	} else {
		name = "unknown";
		id = "unknown";
		uid = lit->deleted_uid;
	}
	t_umap::iterator uit(umap_.begin());
	for(; uit != umap_.end(); ++uit){
		if(uit->second == lit){ break; }
	}
	std::stringstream oldidss;
	if (uit != umap_.end()) {
		size_t olduid =  uit->first ;
		umap_.erase(uit);
		oldidss << olduid;
	} else {
		oldidss << "unknown";
	}

	ERR_NG << "Extracting " << name << " - " << id
		   << " from unit map. Underlying ID changed from "<< oldidss.str()  << " to " << uid
			<< " between insertion and extraction, requiring an exhaustive search of umap_.\n";
}
Example #3
0
unit_ptr unit_map::extract(const map_location &loc) {
	self_check();
	t_lmap::iterator i = lmap_.find(loc);
	if (i == lmap_.end()) { return unit_ptr(); }

	t_umap::iterator uit(i->second);

	unit_ptr u = uit->second.unit;
	size_t uid( u->underlying_id() );

	DBG_NG << "Extract unit " << uid << " - " << u->id()
			<< " from location: (" << loc << ")\n";

	assert(uit->first == uit->second.unit->underlying_id());
	if(uit->second.ref_count == 0){
		umap_.erase(uit);
	} else {
		//Soft extraction keeps the old lit item if any iterators reference it
		uit->second.unit.reset();
	}

	lmap_.erase(i);
	self_check();

	return u;
}
Example #4
0
int UTF8::icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, const std::string::value_type* ptr)
{
    static UTF8Encoding utf8;

    poco_check_ptr (ptr);
    std::string::size_type sz = str.size();
    if (pos > sz) pos = sz;
    if (pos + n > sz) n = sz - pos;
    TextIterator uit(str.begin() + pos, str.begin() + pos + n, utf8);
    TextIterator uend(str.begin() + pos + n);
    while (uit != uend && *ptr)
    {
        int c1 = Unicode::toLower(*uit);
        int c2 = Unicode::toLower(*ptr);
        if (c1 < c2)
            return -1;
        else if (c1 > c2)
            return 1;
        ++uit;
        ++ptr;
    }

    if (uit == uend)
        return *ptr == 0 ? 0 : -1;
    else
        return 1;
}
bool unit_map::self_check() const {
	bool found_the_end(false), good(true);
	t_ilist::const_iterator lit(ilist_.begin());
	for(; lit != ilist_.end(); ++lit){
		if(lit == the_end_){ found_the_end = true; continue; }
		if(lit->ref_count < 0){
			good=false;
			ERR_NG << "unit_map list element ref_count <0 is " << lit->ref_count<<"\n"; }
		if(lit->unit != NULL){
			lit->unit->id(); //crash if bad pointer
		} else {
			if(lit->ref_count <= 0){
				good=false;
				ERR_NG << "unit_map list element ref_count <=0 is " << lit->ref_count<<", when unit deleted.\n"; }
			if(lit->deleted_uid <= 0 ){
				good=false;
				ERR_NG << "unit_map list element deleted_uid <=0 is " << lit->deleted_uid<<"\n"; }
		}
	}

	if(!found_the_end){
		good=false;
		ERR_NG << "unit_map list the_end_ is missing. " <<"\n";}

	t_umap::const_iterator uit(umap_.begin());
	for(; uit != umap_.end(); ++uit){
		if(uit->first <= 0){
			good=false;
			ERR_NG << "unit_map umap uid <=0 is " << uit->first <<"\n"; }
		if(uit->second == the_end_ ){
			good=false;
			ERR_NG << "unit_map umap element == the_end_ "<<"\n"; }
		if(uit->second->unit == NULL && uit->second->ref_count == 0 ){
			good=false;
			ERR_NG << "unit_map umap unit==NULL when refcount == 0 uid="<<uit->second->deleted_uid<<"\n";
		}
		if(uit->second->unit && uit->second->unit->underlying_id() != uit->first){
			good=false;
			ERR_NG << "unit_map umap uid("<<uit->first<<") != underlying_id()["<< uit->second->unit->underlying_id()<< "]\n"; }
	}
	t_lmap::const_iterator locit(lmap_.begin());
	for(; locit != lmap_.end(); ++locit){
		if(locit->second == the_end_ ){
			good=false;
			ERR_NG << "unit_map lmap element == the_end_ "<<"\n"; }
		if(locit->first != locit->second->unit->get_location()){
			good=false;
			ERR_NG << "unit_map lmap location != unit->get_location() " <<"\n"; }
	}
	//assert(good);
	return good;
}
Example #6
0
bool unit_map::self_check() const
{
#ifdef DEBUG_UNIT_MAP
	bool good(true);

	umap::const_iterator uit(umap_.begin());
	for(; uit != umap_.end(); ++uit) {
		if(uit->second.ref_count < 0) {
			good = false;
			ERR_NG << "unit_map pod ref_count <0 is " << uit->second.ref_count << std::endl;
		}

		if(uit->second.unit) {
			uit->second.unit->id(); // crash if bad pointer
		}

		if(uit->first <= 0) {
			good = false;
			ERR_NG << "unit_map umap uid <=0 is " << uit->first << std::endl;
		}

		if(!uit->second.unit && uit->second.ref_count == 0) {
			good = false;
			ERR_NG << "unit_map umap unit==nullptr when refcount == 0" << std::endl;
		}

		if(uit->second.unit && uit->second.unit->underlying_id() != uit->first) {
			good = false;
			ERR_NG << "unit_map umap uid(" << uit->first << ") != underlying_id()[" << uit->second.unit->underlying_id()
				   << "]" << std::endl;
		}
	}

	lmap::const_iterator locit(lmap_.begin());
	for(; locit != lmap_.end(); ++locit) {
		if(locit->second == umap_.end()) {
			good = false;
			ERR_NG << "unit_map lmap element == umap_.end() " << std::endl;
		}
		if(locit->first != locit->second->second.unit->get_location()) {
			good = false;
			ERR_NG << "unit_map lmap location != unit->get_location() " << std::endl;
		}
	}

	// assert(good);
	return good;
#else
	return true;
#endif
}
Example #7
0
RsHash binsvd_pred::predict(RsHash valid, bool verbose)
{
    if (verbose) printf("Binsvd predicting...\n");

    RsHash new_valid(valid);

    // iterate through users (valid)
    RsHashIter uit(valid);
    int users_n = valid.size(), n = 0;
    while (uit.hasNext()) {
        uit.next();
        n++;
        int u = uit.key();
        // iterate through user valid ratings
        QHashIterator<int, float> iit(uit.value());
        while (iit.hasNext()) {
            iit.next();
            int i = iit.key();

            // compute "rating" as dot product of factor vectors
            float r = 0;
            QVector<float> v;
            if (!item_factors.contains(i))
            {
                for(int j = 0; j < user_factors[u].count(); j++)
                {
                    v.append(1);
                }
                r = dot_product(user_factors[u], v, user_factors[u].count());
            }
            else
                r = dot_product(user_factors[u], item_factors[i], user_factors[u].count());
            new_valid[u][i] = r;
        }
        if (verbose) printf("%3.3f %% complited\r", float(n) / users_n * 100);
    }
    valid = new_valid;
    
    if (verbose) printf("ok\n");

    return valid;
}
Example #8
0
void Focus::MakeVarianceImage()
{

	UCharImageType::Pointer img;
	if(imgIn)
		img = imgIn;
	else
	{
		typedef itk::RGBToLuminanceImageFilter< RGBImageType, UCharImageType > ConvertFilterType;
		ConvertFilterType::Pointer convert = ConvertFilterType::New();
		convert->SetInput( imgInColor );
		convert->Update();
		img = convert->GetOutput();
	}

	typedef itk::VarianceImageFunction< UCharImageType2D > VarianceFunctionType;
	typedef itk::ExtractImageFilter< UCharImageType, UShortImageType2D > ExtractFilterType;

	std::cout << "Making Variance Image";

	int size1 = (int)img->GetLargestPossibleRegion().GetSize()[0];
	int size2 = (int)img->GetLargestPossibleRegion().GetSize()[1];
	int size3 = (int)img->GetLargestPossibleRegion().GetSize()[2];

	varImg = FloatImageType::New();

	FloatImageType::PointType origin;
	origin[0] = 0;
	origin[1] = 0;
	origin[2] = 0;
	varImg->SetOrigin( origin );

	FloatImageType::IndexType start = {{ 0,0,0 }};
	FloatImageType::SizeType  size = {{ size1, size2, size3 }};
	FloatImageType::RegionType region;
	region.SetSize( size );
	region.SetIndex( start );

	varImg->SetRegions( region ); 
	varImg->Allocate();

	for(int z = 0; z < size3; ++z)
	{
		std::cout << ".";
		UCharImageType::IndexType index = { {0,0,z} };
		UCharImageType::SizeType size = { {size1, size2, 0} };
		UCharImageType::RegionType region;
		region.SetSize(size);
		region.SetIndex(index);

		ExtractFilterType::Pointer extract = ExtractFilterType::New();
		extract->SetInput( img );
		extract->SetExtractionRegion( region );
		extract->Update();

		UShortImageType2D::Pointer im2 = extract->GetOutput();

		typedef itk::MeanImageFilter<UShortImageType2D,UShortImageType2D> MeanFilterType;
		typedef itk::SquareImageFilter<UShortImageType2D,UShortImageType2D> SquareFilterType;
		typedef itk::SubtractImageFilter<UShortImageType2D,UShortImageType2D> SubtractFilterType;

		UShortImageType2D::SizeType radiussize = {{radius, radius}};
		MeanFilterType::Pointer mean1 = MeanFilterType::New();
		mean1->SetInput(im2);
		mean1->SetRadius(radiussize);

		SquareFilterType::Pointer sq1 = SquareFilterType::New();
		sq1->SetInput(mean1->GetOutput());

		SquareFilterType::Pointer sq2 = SquareFilterType::New();
		sq2->SetInput(im2);

		MeanFilterType::Pointer mean2 = MeanFilterType::New();
		mean2->SetInput(sq2->GetOutput());
		mean2->SetRadius(radiussize);

		SubtractFilterType::Pointer sb1 = SubtractFilterType::New();
		sb1->SetInput1(mean2->GetOutput());
		sb1->SetInput2(sq1->GetOutput());

		sb1->Update();

		typedef itk::ImageRegionIterator<FloatImageType> FloatIteratorType;
		typedef itk::ImageRegionIterator<UShortImageType2D> UShortIteratorType;

		FloatIteratorType fit(varImg,region);
		UShortIteratorType uit(sb1->GetOutput(),sb1->GetOutput()->GetLargestPossibleRegion());
		for(fit.GoToBegin(),uit.GoToBegin();!uit.IsAtEnd(); ++fit, ++uit)
		{
			//printf("%d ", int(uit.Get()));
			fit.Set(uit.Get());
		}


		/* // DONT USE THIS BECAUSE IT IS VERY SLOW
		VarianceFunctionType::Pointer varFunction = VarianceFunctionType::New();
		varFunction->SetInputImage( im2 );
		varFunction->SetNeighborhoodRadius( radius );

		for(int x=0; x<size1; ++x)
		{
		for(int y=0; y<size2; ++y)
		{
		UCharImageType2D::IndexType ind;
		ind[0] = x;
		ind[1] = y;
		float var = (float)varFunction->EvaluateAtIndex( ind );

		FloatImageType::IndexType ind3;
		ind3[0] = x;
		ind3[1] = y;
		ind3[2] = z;
		varImg->SetPixel(ind3, var);
		}
		}
		*/

	}

	typedef itk::ImageFileWriter<FloatImageType> FloatWriter;
	FloatWriter::Pointer fwriter = FloatWriter::New();
	fwriter->SetInput(varImg);
	fwriter->SetFileName("test_fwriter.mhd");
	fwriter->Update();
	/*
	//Rescale:
	typedef itk::RescaleIntensityImageFilter< FloatImageType, UCharImageType > RescaleType;
	RescaleType::Pointer rescale = RescaleType::New();
	rescale->SetOutputMaximum( 255 );
	rescale->SetOutputMinimum( 0 );
	rescale->SetInput( varImg );
	rescale->Update();
	*/
	std::cout << "done\n";
}