Esempio n. 1
0
bool IndexerImpl::incorporate(string url, WordBag& wb)
{
    // adding to urlToId map, adding to idToUrl map
    int* temp = urlToId.find(url);
    if (temp != nullptr) return false;
    
    string word;
    int retrieveCount;
    bool getCount = wb.getFirstWord(word, retrieveCount);
    if (getCount == false) return false;
    
    int ID = rand() %100000;
    while (idToUrl.find(ID) != nullptr) { ID = rand() %100000; }
    urlToId.associate(url, ID); // add to urlToId map
    idToUrl.associate(ID, url); // add to idToUrl map
    
    while (getCount) {
        if (wordToIdCounts.find(word) != nullptr) {
            vector<Pair> *x = wordToIdCounts.find(word);
            x->push_back(Pair(ID, retrieveCount));
        }
        else {
            vector<Pair> y;
            y.push_back(Pair(ID, retrieveCount));
            wordToIdCounts.associate(word, y);
        }
        getCount = wb.getNextWord(word, retrieveCount);
    }
    return true;
//    The user calls the incorporate() method to add all the words in the provided WordBag argument to the index. If incorporate() has previously been called with the same url argument, it returns false, doing nothing. Otherwise, it updates the index by adding all the words and returns true. Incorporating a WordBag containing W distinct words to an index that already contains N words must take far less than O(WN) time, because adding one word mapping (e.g., "fun" → { "www.b.com", 1 }) to an index that already contains N words must take far less than O(N) time.
}
Esempio n. 2
0
void VS10Inst::ValidateDestMask()
{
    char temp[256];
    typedef std::map<char, int> MyMap;
    typedef MyMap::value_type MyPair;
    static const MyPair pairs[] = 
    {
        MyPair('x',1),
        MyPair('y',2),
        MyPair('z',3),
        MyPair('w',4),
    };
    static const MyMap swizzleMap(pairs, pairs+(sizeof(pairs)/sizeof(pairs[0])));

    if ( dst.mask[0] == 0 ) return;
    int i = 1;
    while ( i < 4 && dst.mask[i] != 0 )
    {
        MyMap::const_iterator lastMaskIt = swizzleMap.find(dst.mask[i-1]);
        MyMap::const_iterator curMaskIt = swizzleMap.find(dst.mask[i]);
        if (lastMaskIt == swizzleMap.end() || curMaskIt == swizzleMap.end() ||
            lastMaskIt->second >= curMaskIt->second)
//        if ( dst.mask[i-1] >= dst.mask[i] )
        {
            char mask[5];
            strncpy( mask, dst.mask, 4 );
            mask[4] = 0;
            sprintf( temp, "(%d) Error: destination register has invalid mask: %s\n", line, mask );
            errors.set( temp );
            break;
        }
        i++;
    }
}
Esempio n. 3
0
int main ()
{
  MyMap amap;
  amap.insert("one", 1);
  amap.insert("two", 2);
  amap.insert("three", 3);
  //amap["three"].push_back(3);
  //amap["three"].push_back(3);
  //cout << amap["one"];
  amap.find("one");
  amap.find("two");
  amap.find("three");

  return 0;
}
 static typename DerivedF::Scalar  add_vertex(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
                                              const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
                                              unsigned int i0,
                                              unsigned int i1,
                                              PointMatrixType &vertices,
                                              int &num_vertices,
                                              MyMap &edge2vertex)
 {
   // find vertex if it has been computed already
   MyMapIterator it = edge2vertex.find(EdgeKey(i0, i1));
   if (it != edge2vertex.end()) 
     return it->second;
   ;
   
   // generate new vertex
   const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> & p0 = points.row(i0);
   const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> & p1 = points.row(i1);
   
   typename DerivedV::Scalar s0 = fabs(values[i0]);
   typename DerivedV::Scalar s1 = fabs(values[i1]);
   typename DerivedV::Scalar t  = s0 / (s0+s1);
   
   
   num_vertices++;
   if (num_vertices > vertices.rows())
     vertices.conservativeResize(vertices.rows()+10000, Eigen::NoChange);
   
   vertices.row(num_vertices-1)  = (1.0f-t)*p0 + t*p1;
   edge2vertex[EdgeKey(i0, i1)] = num_vertices-1;
   
   return num_vertices-1;
 }
Esempio n. 5
0
vector<UrlCount> IndexerImpl::getUrlCounts(string word)
{
    vector<UrlCount> x;
    strToLower(word);
    if (wordToIdCounts.find(word) == nullptr) return x;
    vector<Pair> y = *wordToIdCounts.find(word);
    for (int i=0; i<y.size(); i++) {
        string Url = *(idToUrl.find(y[i].m_ID));
        int tempCount = y[i].m_count;
        UrlCount temp;
        temp.url = Url;
        temp.count = tempCount;
        x.push_back(temp);
    }
    return x;
    
//    The getUrlCounts() method returns a vector of UrlCounts for the specified word. The getUrlCounts() method must be case-insensitive, so it must return the same result for, e.g., "zits", "ZITS", or "ZitS" as the word argument. There is no requirement that the UrlCounts in the vector returned by this method be in any particular order. Each UrlCount in the vector holds a URL string and the count associated with that word in the WordBag incorporated with that URL (representing the number of times that word appeared on the web page at that URL). A given URL string must occur only once within the vector of matches. If your index contains N words, your getUrlCounts() method must return a vector of UrlCounts in far less than O(N) time.
}
Esempio n. 6
0
bool IndexerImpl::incorporate(string url, WordBag& wb)
{

    int hash;
    //Generates a hash of url as well as index for linear probing
    int modhash = hashurl(url,hash);
    //If the head bucket of the array is not initialized, initialize it
    if (idToUrl[modhash]==NULL)
        idToUrl[modhash]=new MyMap<int,string>;
    //Checks to see if this url has already been passed to incorporate
    if (idToUrl[modhash]->find(hash)!=NULL)
        return false;
    //Adds url to hash-url map
    if (idToUrl[modhash]->size()==0)
        indexes.push_back(modhash);
    idToUrl[modhash]->associate(hash,url);
    
    
    string word;
    int count;
    //Iterates through W distinct words in wordbag. O(W)
    bool gotAWord = wb.getFirstWord(word, count);
    while (gotAWord)
    {
        //Accounts for case-insensitive indexing.
        strToLower(word);
        point bucket = point(hash,count);
        //Find if word already exists. O(log N).
        vector<point>* temp = urlToCount->find(word);
        if (temp==NULL)
        {
            //The word does not yet exist in the index
            //Create a temp vector
            temp = new vector<point>;
        }
        temp->push_back(bucket);
        //Map word to temp Map of urlhash to wordcount
        urlToCount->associate(word, *temp);
        gotAWord = wb.getNextWord(word,count);
    }
    //Final BigO is O(WlogN) which is less than O(WN)
    return true;
}
Esempio n. 7
0
int main()
{
    MyMap<string, int> months;

    months["january"] = 31;
    months["february"] = 28;
    months["march"] = 31;
    months["april"] = 30;
    months["may"] = 31;
    months["june"] = 30;
    months["july"] = 31;
    months["august"] = 31;
    months["september"] = 30;
    months["october"] = 31;
    months["november"] = 30;
    months["december"] = 31;

    cout << "Map size: " << months.size() << endl;
    if (months.empty())
        cout << "Map is empty!" << endl;
    else
        cout << "Map is not empty!" << endl;

    cout << "june -> " << months["june"] << endl;
    cout << "blabla -> " << months["blabla"] << endl;
    cout << endl;

    display(months);

    MyMap<string, int>::iterator it;
    it = months.find("blabla");
    months.erase(it);

    display(months);

    return 0;
}
Esempio n. 8
0
vector<UrlCount> IndexerImpl::getUrlCounts(string word)
{
    strToLower(word);
    vector<UrlCount> output;
    //First locates the word in urlToCount Map. O(log N) steps
    vector<point>* temp = urlToCount->find(word);
    //If word is not found in index, return empty vector
    if (temp==NULL)
        return output;
    vector<point>::iterator it = temp->begin();
    //Iterates through the vector mapped to the word. O(log N) + V steps where V is the size of the vector
    while (it!=temp->end())
    {
        UrlCount copy;
        copy.count=it->m_count;
        int id = it->m_hash;
        string url = *(idToUrl[id%MAX_MAP_SIZE]->find(id));
        copy.url=url;
        output.push_back(copy);
        it++;
    }
    //Final BigO is O(logN + V) which is less than O(N + V)
    return output;  
}
Esempio n. 9
0
int main()
{
  //using namespace std;
  int number1 = 100;
  int number2 = 20000;
  string str1 = IntToString(number1);
  string str2 = IntToString(number2);
  string comma(", ");
  str1 += comma;
  str1 += str2;
  cout << str1 << endl;
  MyMap amap;
  amap.insert(str1,1);
  amap.find(str1);
  const char* strL;
  strL = str1.c_str();
  regex_t preg;
  size_t nmatch = 3;
  regmatch_t pmatch[nmatch];
  int i, j;
  string restring1;
  string restring2;
  if (regcomp(&preg, "([[:digit:]]+), ([[:digit:]]+)", REG_EXTENDED|REG_NEWLINE) != 0) {
    printf("regex compile failed.\n");
    exit(1);
  }
  printf("String = %s\n", strL);
  if (regexec(&preg, strL, nmatch, pmatch, 0) != 0) {
    printf("No match.\n");
  } else {
    for (i = 0; i < nmatch; i++) { /* nmatch にマッチした件数が入る */
      printf("Match position = %d, %d , str = ", (int)pmatch[i].rm_so, (int)pmatch[i].rm_eo);
      if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0) {
	for (j = pmatch[i].rm_so ; j < pmatch[i].rm_eo; j++) {
	  //putchar(strL[j]);
          if(i==1){
          restring1+=strL[j];
	  }
          if(i==2){
	   restring2+=strL[j];
	  }
	}
      }
      printf("\n");
    }
  }

  regfree(&preg);
  cout << restring1 <<endl;
  cout << restring2 <<endl;
  assert(typeid(restring1) == typeid(string));
  assert(typeid(restring2) == typeid(string));
  int renumber1,renumber2;
  istringstream istr1(restring1);
  istr1 >> renumber1;
  cout << renumber1 <<endl;
  assert(typeid(renumber2) == typeid(int));
  istringstream istr2(restring2);
  istr2 >> renumber2;
  cout << renumber2 <<endl;
  assert(typeid(renumber2) == typeid(int));
  return 0;
}