Ejemplo n.º 1
0
void AccountMap::Existing( const ID& id ) {
  vector<ID*> list_id(10);
  vector<int> list_score(10);
  int target_score = 1;
  int num_compare = id.size() - 1;
  int score_temp;
  int size = 0;
  SkipListNode* front;
  SkipListNode* back;

  // Set the iteratro go to front and back
  Find(id, front);
  back = Next(front);

  // Stop when get enough data, or two iterator are at the end
  while ((front->data_id.front() != '!' || back->data_id.front() != '{') && \
         (size != 10 || list_score[10] > target_score)
  ) {

    // First move front iteratro
    while (front->data_id.front() != '!' && \
          (num_compare == 0 || id.compare(0, num_compare, front->data_id, 0, num_compare) == 0)
    ) {
      score_temp = calculate_score(id, front->data_id);
      if (size < 10 || score_temp <= list_score.back()) {
        insert_id_to_vector(list_id, list_score, 0, front->data_id, score_temp, size);
      }
      front = Previous(front);
    }
    
    // If get enough data, stop
    if (size == 10 && list_score[10] <= target_score) break;
    
    // If data is not enough, move back iterator
    while (back->data_id.front() != '{' && \
          (num_compare == 0 || id.compare(0, num_compare, back->data_id, 0, num_compare) == 0)
          ) {
      score_temp = calculate_score(id, back->data_id);
      if (size < 10 || score_temp < list_score.back()) {
        insert_id_to_vector(list_id, list_score, 1, back->data_id, score_temp, size);
      }
      back = Next(back);
    }

    // Add the tolerent to score, adjust the number of string to compare
    target_score += 1;
    if (num_compare > 0) num_compare -= 1;
  }

  // Print the result
  bool comma = false;
  for (int i = 0; i < (int)list_score.size(); ++i) {
    if (comma) {
      cout << ',';
    }
    cout << *(list_id[i]);
    comma = true;
  }

}