void update(MAP & p , pii x){
	
	MAP::iterator it = p.lower_bound(x.F), ini = it;
	if( it != p.end() && it->F == x.F && it->S <= x.S) return;
	if( it != p.begin() && (--it)->S <= x.S) return;
	it = ini;
	while( it != p.end() && it->S >= x.S) it++;
	p.erase( ini, it);
	p[x.F] = x.S;
}
Example #2
0
	CSession*		Find( UINT id ){
		ITERATOR	it = m_map.find( id );
		if( it != m_map.end() ){
			return(it->second);
		}
		return NULL;
	}
Example #3
0
Dart Topo3PrimalRender<PFP>::raySelection(MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float dmax)
{
	float AB2 = rayAB*rayAB;
	Dart dFinal;
	float dm2 = dmax*dmax;
	double dist2 = std::numeric_limits<double>::max();

	for(Dart d = map.begin(); d!=map.end(); map.next(d))
	{
		// get back position of segment PQ
		const Geom::Vec3f& P = m_bufferDartPosition[m_attIndex[d]];
		const Geom::Vec3f& Q =m_bufferDartPosition[m_attIndex[d]+1];
		float ld2 = Geom::squaredDistanceLine2Seg(rayA, rayAB, AB2, P, Q);
		if (ld2 < dm2)
		{
			Geom::Vec3f V = (P+Q)/2.0f - rayA;
			double d2 = double(V*V);
			if (d2 < dist2)
			{
				dist2 = d2;
				dFinal = d;
			}
		}
	}
	return dFinal;
}
Example #4
0
int main(){
#ifdef LOCAL_TEST
  std::ifstream in("input.txt",std::ifstream::in);
#else
  std::istream& in=std::cin;
#endif
  std::string line;
  std::vector<long> nums;
  std::vector<long> allnums;
  typedef  std::tr1::unordered_map<long,int> MAP;
  MAP count;
  while(std::getline(in,line)){
    lineToNumbers(line,nums);
    for(size_t i=0;i!=nums.size();++i){
      long v=nums[i];
      MAP::iterator iter=count.find(v);
      if(iter==count.end()){
	count[v]=1;
	allnums.push_back(v);
      } else {
	iter->second++;
      }
    }
  }
  for(size_t i=0;i!=allnums.size();++i){
    long v= allnums[i];
    std::cout<<v<<" "<<count[v]<<"\n";
  }
#ifdef LOCAL_TEST
  in.close();
#endif
  return 0;
}
Example #5
0
 UndirectedGraphNode *cloneGraph1(UndirectedGraphNode *node) { //BFS
     if (!node) return NULL;
     queue<UndirectedGraphNode *> q; q.push(node);
     
     MAP map;
     map[node] = new UndirectedGraphNode(node->label);
     
     while (!q.empty()) {
         UndirectedGraphNode *origNode = q.front(); q.pop();
         UndirectedGraphNode *newNode = map[origNode];
         for (int i = 0; i < origNode->neighbors.size(); ++i) {
             UndirectedGraphNode *origNb = origNode->neighbors[i];
             if (map.find(origNb) != map.end()) { //already cloned
                 newNode->neighbors.push_back(map[origNb]); //push map[origNb]
                 continue;
             }
             UndirectedGraphNode *newNb = new UndirectedGraphNode(origNb->label);
             newNode->neighbors.push_back(newNb);
             map[origNb] = newNb;
             q.push(origNb);
         }
     }
     
     return map[node];
 }
Example #6
0
		static CSession*		Find( const CSocket& sock ){
			ITERATOR	it	= m_Sessions.find( sock.GetHandle() );
			if( it != m_Sessions.end() ){
				return(it->second);
			}
			return NULL;
		}
Example #7
0
void print_map(MAP& map) {
    std::cout << "MAP { " << std::endl;
    for (MAP::const_iterator it = map.begin(); it != map.end(); it++) {
        std::cout << it->first << " => " << it->second << std::endl;
    }
    std::cout << " }" << std::endl;
}
Example #8
0
GraphNode *cloneGraph(GraphNode *node) {
    if (!node) return NULL;
    MAP exist;
    exist[node] = new GraphNode(node->data);
    queue<GraphNode *> q;
    q.push(node);
    while (!q.empty())
    {
        GraphNode *orig = q.front();
        GraphNode *newNode = exist[orig];
        q.pop();
        for (int i = 0; i < orig->neighbors.size(); ++i)
        {
            if (exist.find(orig->neighbors[i]) != exist.end()) {
                newNode->neighbors.push_back(exist[orig->neighbors[i]]);
                continue;
            }
            GraphNode *newNeighbor = new GraphNode(orig->neighbors[i]->data);
            newNode->neighbors.push_back(newNeighbor);
            exist[orig->neighbors[i]] = newNeighbor;
            q.push(orig->neighbors[i]);
        }
    }
    return exist[node];
}
Example #9
0
 double substitute(double x, double y) const {
   double val=0;
   for(const_iterator i(terms.begin());i!=terms.end();++i) {   
     val += i->first.substitute(x,y) * i->second;
   }
   return val;
 }
Example #10
0
long default_find(long key, long defaultValue, MAP& map){
    MAP::const_iterator it = map.find(key);
    if (it == map.end()) {
        return defaultValue;
    } else {
        return it->second;
    }
}
Example #11
0
 typename _MapT<MAP>::KeyIter
 eachDistinctKey (MAP& map)
 {
   typedef RangeIter<typename MAP::iterator> Range;
   
   Range contents (map.begin(), map.end());
   return wrapIter (filterRepetitions (takePairFirst(contents)));
 }
Example #12
0
          // TREE STUFF
   // Does there exist a root with variable v?
 bool FindTree(const Variable & v,SFSGNode *& result) const {
   MAP::const_iterator w = d_tree_map.find(v);
   bool b = w!=d_tree_map.end();
   if(b) {
     result = (*w).second; 
   };
   return b;
 };
Example #13
0
 typename _MapT<MAP>::ValIter
 eachMapVal (MAP& map)
 {
   typedef RangeIter<typename MAP::iterator> Range;
   
   Range contents (map.begin(), map.end());
   return wrapIter (takePairSecond(contents));
 }
Example #14
0
 typename _MapT<MAP>::KeyIter
 eachMapKey (MAP& map)
 {
   typedef RangeIter<typename MAP::iterator> Range;
   
   Range contents (map.begin(), map.end());
   return wrapIter (takePairFirst (contents));
 }
Example #15
0
 inline typename MAP::mapped_type
 getValue_or_default (MAP& map, typename MAP::key_type const& key
                              , typename MAP::mapped_type defaultVal)
 {
   typename MAP::const_iterator pos = map.find (key);
   if (pos != map.end())
     return pos->second;
   else
     return defaultVal;
 }
Example #16
0
  void operator+=(simple_poly const &p1) {    
    for(const_iterator i(p1.terms.begin());i!=p1.terms.end();++i) {      
      iterator j = terms.find(i->first);
      if(j != terms.end()) {
	j->second += i->second;
      } else {
	terms.insert(std::make_pair(i->first,i->second));
      }
    }
  }
Example #17
0
long lcm(MAP& map) {
    long m = 1;
    for (MAP::const_iterator it = map.begin(); it != map.end(); it++) {
        long factor = it->first;
        long power = it->second;
        for (; power > 0; power--) {
            m *= factor; 
        }
    }
    return m;
}
Example #18
0
int __map_hasKey(SEXP m, SEXP k) {
    MAP* list = (MAP*)R_ExternalPtrAddr(m);
    if (!list) return 0;
    
    if (!Rf_isString(k)) 
        return 0;
    
    std::string keystr = as<std::string>(k);
    
    MAP::iterator it = list->find(keystr);
    return (it != list->end());
}
Example #19
0
 static void s_listNames(MyOstream & os,
        const char * sep = "\n",const char * sep2 = "\n") {
   MAP::const_iterator w = s_map.begin(), e = s_map.end();
   if(w!=e)  {
     --e;
     while(w!=e) {
       os << (*w).first << sep; 
       ++w;
     };
     os << (*w).first << sep2;
   };
 };
Example #20
0
void Topo3PrimalRender<PFP>::computeDartMiddlePositions(MAP& map, DartAttribute<VEC3, MAP>& posExpl)
{
	m_vbo0->bind();
	Geom::Vec3f* positionsPtr = reinterpret_cast<Geom::Vec3f*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY));

	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		const Geom::Vec3f& v =(positionsPtr[m_attIndex[d]] + positionsPtr[m_attIndex[d]+1])*0.5f;
		posExpl[d] = PFP::toVec3f(v);
	}

	m_vbo0->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
}
Example #21
0
// [[Rcpp::export]]
SEXP map_getVal(SEXP m, SEXP k) {
    MAP* list = (MAP*)R_ExternalPtrAddr(m);
    if (!list) return R_NilValue;
    
    if (!Rf_isString(k)) 
        return R_NilValue;
    
    std::string keystr = as<std::string>(k);
    
    MAP::iterator it = list->find(keystr);
    if (it == list->end()) 
        return R_NilValue;
    else
        return local_ptr_toRObject(it->second);
}
Example #22
0
 const std::string str() const {
   std::stringstream ss;
   // start with xs
   bool firstTime=true;
   for(const_iterator i(terms.begin());i!=terms.end();++i) {      
     if(!firstTime) { ss << " + "; }
     firstTime=false;
     simple_poly_term const &t(i->first);
     int count = i->second;
     if(count > 1) { ss << count; }
     ss << i->first.str();
   }
   
   return ss.str(); 
 }
void map_histogram(std::ostream &out, const MAP &map) {
  std::vector<int> hist;
  for (typename MAP::const_iterator i = map.begin(); i != map.end(); ++i) {
    size_t n = (*i).second.size();
    if (hist.size() <= n) {
      hist.resize(n + 1);
    }
    hist[n]++;
  }
  int total = map.size();
  std::string bar(50, '*');
  for (size_t i = 0; i < hist.size(); i++) {
    if (hist[i] > 0) {
      out << std::setw(5) << i << " : " << std::setw(5) << hist[i] << " " << bar.substr(50 - hist[i] * 50 / total) << std::endl;
    }
  }
}
Example #24
0
 bool GpXmlStateMachine::FindAttribute(MAP& attribs, const string& compstr, string& retstr)
 {
   bool                retval = false;
   MAP::const_iterator search = attribs.find(compstr);
   #ifdef DEBUG
   cout << "Searching for attribute: " << compstr;
   #endif
   if (search != attribs.end() )
   {
     retstr = attribs[compstr];
     #ifdef DEBUG
     cout << " Found value: " << retstr;
     #endif
     retval = true;
   }
   #ifdef DEBUG
   cout << "\n";
   #endif
   return retval;
 }
Example #25
0
  // this is gary's shift operation
  void operator*=(xy_term const &p2) {
    if(p2.ypowerend == p2.ypower) {
      // I don't think the STL strictly would allow this,
      // but it doesn't hurt!      
      for(const_iterator i(terms.begin());i!=terms.end();++i) {      
	simple_poly_term &t((simple_poly_term &)i->first); // ouch ;)
	t.xpower += p2.xpower;
	t.ypower += p2.ypower;
      }
    } else {
      // recursive case (this is an ugly hack)
      simple_poly p(*this);

      for(unsigned int i=p2.ypower;i!=p2.ypowerend-1;++i) {
	*this += p;
	p *= xy_term(0,1);
      }
      
      *this += p;
    }
  }
Example #26
0
void Topo3PrimalRender<PFP>::updateColors(MAP& map, const VertexAttribute<VEC3, MAP>& colors)
{
	m_vbo2->bind();
	Geom::Vec3f* colorBuffer =  reinterpret_cast<Geom::Vec3f*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));
	unsigned int nb=0;

	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (nb < m_nbDarts)
		{
			colorBuffer[m_attIndex[d]] = colors[d];
			nb++;
		}
		else
		{
			CGoGNerr << "Error buffer too small for color picking (change the selector parameter ?)" << CGoGNendl;
			break;
		}
	}
	glUnmapBuffer(GL_ARRAY_BUFFER);
}
int main(){
	MAP member;
	member.insert(make_pair(1000000000,1));		//使用strength做key关键字,因为题目已经给出了 实力值不可能相等 的条件
	int strength,id;							//也可使用id做key关键字,但这样每一次都需要完整遍历一次map,容易造成TLE
	int n;
	int low,high;
	cin >> n;
	while(n--){
		cin >> id >> strength;
		member.insert(make_pair(strength,id));
		MAP::iterator i,j,k;
		i = member.find(strength);
		if(i != member.end()){
			if(i == member.begin()){
				j = i;
				j++;
				cout << i->second << " " << j->second << endl;
			}
			else{
				j = i;
				k = i;
				j++;
				k--;
				low = i->first - k->first;
				high = j->first - i->first;
				if(low <= high){
					cout << i->second << " " << k->second << endl;
				}
				else{
					cout << i->second << " " << j->second << endl;
				}
			}
		}
		else continue;
	}
	return 0;
}
Example #28
0
int main()
{
    MAP table;

    string name;

    /******************************************************
* PHASE 0: Load the words in the text file *
* into a the table *
*******************************************************/

    cout << "File name? ";
    getline(cin, name);

    ifstream textFile(name.c_str());

    if (!textFile)
    {
        cerr << "Text file could not be opened!!" << endl;
        return 0;
    }

    string word;
    int nWords = 0;

    while(textFile >> word)
    {
        nWords++;
        //remove non-alpha chars
        word.erase(remove_if(word.begin(), word.end(), isNotAlpha), word.end());
        //convert to lower-case letters
        transform(word.begin(), word.end(), word.begin(), ::tolower);
        if (word == "") continue;

        ELEMENT e = make_pair(word,1);
        cout << word << endl;
        table.BST_threaded::insert(e);
    }

    textFile.close();

    /******************************************************
* PHASE 1: Display *
* - number of words in the text *
* - number of unique words (occurring only once) *
* - frequency table *
*******************************************************/
    //number of words in the text
    cout << "The number of words in the text file is: " << nWords << endl;
    cout << "Number of unique words in the file: " << table.size() << endl;

    cout << "\n\nTable sorted alphabetically:"
         << endl << endl;

    BiIterator it = table.begin();

    cout << " \tKEY" << "\tCOUNTER" << endl;
    cout << "==============================\n";
    for( ; it != table.end(); it++)
    {
         cout << setw(15) << it->first
              << setw(15) << it->second << endl;
    }


    /******************************************************
* PHASE 3: remove all words with counter 1 *
* and display table again *
*******************************************************/

    it = table.begin();
    vector<string> temp;
    for( ; it != table.end(); it++){
        if(it->second == 1){
            temp.push_back(it->first);
        }
    }
    for(int i = 0; i != temp.size(); i ++){
        table.remove(temp[i]);
    }


    cout << "Number of words after remove: " << table.size() << endl;

    cout << "Un-unique table sorted alphabetically:" << endl;

    cout << " \tKEY" << "\tCOUNTER" << endl;
    cout << "==============================\n";
    for(it = table.begin(); it != table.end(); it++) {
         cout << setw(15) << it->first
              << setw(15) << it->second << endl;
    }


    /***********************************************************
* PHASE 4: request two words to the user w1 and w2 *
* then display all words in the interval [w1,w2] *
************************************************************/
    //We assume that the words entered by the user exists in the table
    cout << "Please enter two words from the list " << endl;
    string first, last;
    cin >> first >> last;
    cout << "First: " << first << " and last: " << last << endl;
    BiIterator itr = table.find(first);
    BiIterator stop = table.find(last);
    itr--; //a dummy to include the last node
    for(stop; stop!=itr; stop--){
        cout << setw(15) << stop->first
        << setw(15) << stop->second << endl;
    }
    return 0;
}
Example #29
0
main(int argc,const char **argv)
{
  typedef  map<A*,A*,lessPtr<A> > MAP;
  typedef  MAP::iterator IT;

  MAP m;

  A aa(1);
  A bb(2);
  A cc(3);

  m[&aa] = &bb;
  m[&bb] = &cc;
  m[&cc] = &aa; 

  IT it;
  for(it=m.begin(); it!=m.end(); it++)
  {
     pair<A*,A*> dat = *it;
     printf("%d --> %d\n",dat.first->X,dat.second->X);
  }

//  return 0;

   const char *fname = (argc>1) ? argv[1] : "rismmol1d.prm"; 
   const char *str = readFile(fname); //distances = {(O,H1) : 1 [Angstr], (O,H2) : 1 [Angstr], (H1,H2) : 1.63[Angstr] };";
   StringParser sp(str);

   Parameters prm;
   ParseParameters pp("Bohr","Hartree",&prm);

   pp.parse(&sp);

   printf("%s\n",prm.toString());
   

   ParametersAdaptor1DRISM pa(&prm);
   pa.init();

   Matrix<Real> *dd =pa.getDistances();
   int ii,jj;
   for(ii=0;ii<dd->getNumRows();ii++)
   {
      for(jj=0;jj<dd->getNumCols();jj++)
      {
          printf("%lf ",dd->getAt(ii,jj));
      }
      printf("\n");
   }

   free((void *)str);
   return 0;

   int N = 4096;
   double dR = 0.05;
   SphericalFFT *fft = SphericalFFT::getInstance(); 
   Grid1DRISM grd(N,dR);

   Real *a = new Real[N]; 
   Real *fa = new Real[N];
   Real *b = new Real[N];
   Real *fb = new Real[N];
   Real *c = new Real[N];
   Real *fc = new Real[N];

   int i;
   for(i=0;i<N;i++) 
   { 
     a[i] = i % 20; 
     b[i] = exp(-(i*dR)*(i*dR));
   }
   
   fft->d3fft(a,fa,&grd); 
   fft->d3fft(b,fb,&grd);

   copyArray(fc,fa,N); // fc=fa
   mulArray(fc,fb,N);  // fc=fa*fb
   
   fft->d3ifft(fc,c,&grd);
   
   writeDoubleArrayToFile("a.dat",a,N);
   writeDoubleArrayToFile("fa.dat",fa,N);

   writeDoubleArrayToFile("b.dat",b,N);
   writeDoubleArrayToFile("fb.dat",fb,N);

   writeDoubleArrayToFile("c.dat",c,N);
   writeDoubleArrayToFile("fc.dat",fc,N);

   delete [] a;
   delete [] b;
   delete [] c;
   delete [] fa;
   delete [] fb;
   delete [] fc;
   

}
Example #30
0
 const_iterator end() const { return terms.end(); }