LCOV - code coverage report
Current view: top level - src - namebase.h (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 16 16 100.0 %
Date: 2017-06-21 14:32:49 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             : * Copyright (c) 2001  Shane Hudson.
       3             : * Copyright (C) 2014-2017 Fulvio Benini
       4             : 
       5             : * This file is part of Scid (Shane's Chess Information Database).
       6             : *
       7             : * Scid is free software: you can redistribute it and/or modify
       8             : * it under the terms of the GNU General Public License as published by
       9             : * the Free Software Foundation.
      10             : *
      11             : * Scid is distributed in the hope that it will be useful,
      12             : * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : * GNU General Public License for more details.
      15             : *
      16             : * You should have received a copy of the GNU General Public License
      17             : * along with Scid.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #ifndef SCID_NAMEBASE_H
      21             : #define SCID_NAMEBASE_H
      22             : 
      23             : #include "common.h"
      24             : #include "misc.h"
      25             : #include <vector>
      26             : #include <map>
      27             : #include <algorithm>
      28             : 
      29             : class Index;
      30             : 
      31             : // There are four NameBases, one each for PLAYER, EVENT , SITE and ROUND tags.
      32             : typedef uint nameT;
      33             : enum {
      34             :     NAME_PLAYER, NAME_EVENT, NAME_SITE, NAME_ROUND,
      35             :     NUM_NAME_TYPES,
      36             :     NAME_INVALID = 99
      37             : };
      38             : 
      39             : typedef uint idNumberT;
      40             : 
      41             : const char NAME_TYPE_STRING [NUM_NAME_TYPES][8] = {
      42             :     "player",
      43             :     "event",
      44             :     "site",
      45             :     "round"
      46             : };
      47             : 
      48             : 
      49             : class NameBase
      50             : {
      51             :     static const char* NAMEBASE_MAGIC;
      52             :     static const char* NAMEBASE_SUFFIX;
      53             : 
      54             :     std::string filename_;
      55             :     std::vector<const char*> names_[NUM_NAME_TYPES];
      56             :     std::vector<eloT> eloV_;
      57             :     struct idxCmp {
      58             :         bool operator() (const char* str1, const char* str2) const {
      59             :             // *** Compatibility ***
      60             :             // Older code used a custom StrTree class with a peculiar sorting:
      61             :             // - the first char was interpreted as an unsigned char;
      62             :             // - the remaining part was compared with the function strComapare(),
      63             :             //   which converts the chars to ints, and is not consistent with
      64             :             //   the standard function strcmp().
      65             :             // The old StrTree class did also have unpredictable behaviors when
      66             :             // fed with names not sorted according to that criteria, for example
      67             :             // it could create Namebase objects with duplicate entries.
      68             :             // ***
      69   178597650 :             if (*str1 == *str2)
      70   148701900 :                 return strCompare(str1, str2) < 0;
      71             : 
      72    29895750 :             return static_cast<uint>(*str1) < static_cast<uint>(*str2);
      73             :         }
      74             :     };
      75             :     std::map<const char*, idNumberT, idxCmp> idx_[NUM_NAME_TYPES];
      76             :     bool modified_;
      77             : 
      78             : public:
      79             :     static bool IsValidNameType (nameT nt) { return (nt < NUM_NAME_TYPES); }
      80             :     static nameT NameTypeFromString (const char * str);
      81          10 :     static const char* Suffix() { return NAMEBASE_SUFFIX; }
      82             : 
      83         616 :     NameBase() : modified_(false) {}
      84         280 :     ~NameBase() { Clear(); }
      85             :     void Clear();
      86             : 
      87             :     errorT    Create (const char* filename);
      88             :     errorT    ReadEntireFile (const char* filename);
      89          32 :     errorT flush(const Index* idx) {
      90          32 :         errorT err = OK;
      91          64 :         if (modified_ && !filename_.empty()) err = WriteNameFile(idx);
      92          32 :         if (err == OK) modified_ = false;
      93          32 :         return err;
      94             :     }
      95           9 :     void hackedNameFreq() { modified_ = true; }
      96             : 
      97     5461624 :     const char* GetName (nameT nt, idNumberT id) const { return names_[nt][id]; }
      98      145750 :     eloT GetElo (idNumberT id) const { return eloV_[id]; }
      99             : 
     100             :     errorT AddName (nameT nt, const char * str, idNumberT * idPtr);
     101       48654 :     void AddElo (idNumberT id, eloT elo) { if (elo > eloV_[id]) eloV_[id] = elo; }
     102             : 
     103             :     errorT    FindExactName   (nameT nt, const char * str, idNumberT * idPtr) const;
     104             :     uint      GetFirstMatches (nameT nt, const char * str, uint maxMatches,
     105             :                                idNumberT * array) const;
     106             : 
     107         266 :     idNumberT GetNumNames (nameT n) const  { return names_[n].size(); }
     108             : 
     109             :     std::vector<uint32_t> generateHashMap(nameT nt) const {
     110             :         std::vector<uint32_t> res(names_[nt].size());
     111             :         std::transform(names_[nt].begin(), names_[nt].end(), res.begin(),
     112             :                        strStartHash);
     113             :         return res;
     114             :     }
     115             : 
     116             : private:
     117             :     typedef std::map<const char*, idNumberT, idxCmp>::const_iterator iterator;
     118             : 
     119             :     NameBase(const NameBase&);
     120             :     NameBase& operator=(const NameBase&);
     121             :     bool setFileName(const char* filename);
     122             :     errorT WriteNameFile(const Index* idx);
     123             : };
     124             : 
     125             : #endif  // #ifdef SCID_NAMEBASE_H
     126             : 
     127             : //////////////////////////////////////////////////////////////////////
     128             : //  EOF: namebase.h
     129             : //////////////////////////////////////////////////////////////////////
     130             : 

Generated by: LCOV version 1.12