topical media & game development

talk show tell print

professional-program-17-NameDB-FirstAttempt-NameDB.c

? / professional-program-17-NameDB-FirstAttempt-NameDB.c


  include <NameDB.h>
  
  include <fstream>
  
  using namespace std;
  
  //
  // Reads the names from the file and populates the database.
  // The database is vector of name/count pairs, storing the
  // number of times each name shows up in the raw data.
  //
  NameDB::NameDB(const string& nameFile) throw (invalid_argument)
  {
    // Open the file and check for errors
    ifstream inFile(nameFile.c_str());
    if (!inFile) {
      throw invalid_argument("Unable to open file\n");
    }
  
    // Read the names one at a time.
    string name;
    while (inFile >> name) {
      // Lookup the name in the database so far.
      if (nameExists(name)) {
        // If the name exists in the database, just
        // increment the count
        incrementNameCount(name);
      } else {
        // If the name doesn't yet exist, add it with
        // a count of 1.
        addNewName(name);
      }
    }
    inFile.close();
  }
  
  //
  // nameExists
  //
  // Returns true if the name exists in the database. Returns false otherwise.
  //
  bool NameDB::nameExists(const string& name) const
  {
    // Iterate through the vector of names looking for the name.
    for (vector<pair<string, int> >::const_iterator it = mNames.begin();
         it !=  mNames.end(); ++it) {
      if (it->first == name) {
        return (true);
      }
    }
    return (false);
  }
  
  //
  // incrementNameCount
  //
  // Precondition: name exists in the vector of names.
  // Postcondition: the count associated with name is incremented.
  //
  void NameDB::incrementNameCount(const string& name)
  {
    for (vector<pair<string, int> >::iterator it = mNames.begin();
         it != mNames.end(); ++it) {
      if (it->first == name) {
        it->second++;
        return;
      }
    }
  }
  
  //
  // addNewName
  //
  // Adds a new name to the database.
  //
  void NameDB::addNewName(const string& name)
  {
    mNames.push_back(make_pair<string, int>(name, 1));
  }
  
  //
  // getNameRank
  //
  // Returns the rank of the name.
  // First looks up the name to obtain the number of babies with that name.
  // Then iterates through all the names, counting all the names with a higher
  // count than the specified name. Returns that count as the rank.
  //
  int NameDB::getNameRank(const string& name) const
  {
    // Make use of the getAbsoluteNumber() method
    int num = getAbsoluteNumber(name);
  
    // Check if we found the name
    if (num == -1) {
      return (-1);
    }
  
    //
    // Now count all the names in the vector that have a
    // count higher than this one. If no name has a higher count,
    // this name is rank number 1. Every name with a higher count
    // decreases the rank of this name by 1.
    //
    int rank = 1;
    for (vector<pair<string, int> >::const_iterator it = mNames.begin();
         it != mNames.end(); ++it) {
      if (it->second > num) {
        rank++;
      }
    }
  
    return (rank);
  }
  
  //
  // getAbsoluteNumber
  //
  // Returns the count associated with this name
  //
  int NameDB::getAbsoluteNumber(const string& name) const
  {
    for (vector<pair<string, int> >::const_iterator it = mNames.begin();
         it != mNames.end(); ++it) {
      if (it->first == name) {
        return(it->second);
      }
    }
    return (-1);
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.