libept
|
00001 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*- 00002 #ifndef EPT_POPCON_POPCON_H 00003 #define EPT_POPCON_POPCON_H 00004 00010 /* 00011 * Copyright (C) 2007 Enrico Zini <enrico@debian.org> 00012 * 00013 * This program is free software; you can redistribute it and/or modify 00014 * it under the terms of the GNU General Public License as published by 00015 * the Free Software Foundation; either version 2 of the License, or 00016 * (at your option) any later version. 00017 * 00018 * This program is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 * GNU General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License 00024 * along with this program; if not, write to the Free Software 00025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00026 */ 00027 00028 #include <tagcoll/diskindex/mmap.h> 00029 #include <string> 00030 00031 namespace ept { 00032 namespace apt { 00033 class Apt; 00034 } 00035 00036 namespace popcon { 00037 00043 class Score 00044 { 00045 protected: 00046 unsigned offset; 00047 00048 public: 00049 float score; 00050 00051 Score(float score) : offset(offset), score(score) {} 00052 00053 friend class Popcon; 00054 friend class PopconIndexer; 00055 friend class PopconGenerator; 00056 }; 00057 00072 class Popcon : public tagcoll::diskindex::MMap 00073 { 00074 struct GeneralInfo : public tagcoll::diskindex::MMap 00075 { 00076 size_t submissions() const; 00077 }; 00078 00079 tagcoll::diskindex::MasterMMap mastermmap; 00080 time_t m_timestamp; 00081 00082 GeneralInfo m_info; 00083 00085 const Score* structByIndex(size_t idx) const 00086 { 00087 if (idx >= 0 && idx < size()) 00088 return (Score*)m_buf + idx; 00089 return 0; 00090 } 00091 00092 public: 00093 Popcon(); 00094 00096 time_t timestamp() const { return m_timestamp; } 00097 00099 bool hasData() const { return m_timestamp != 0; } 00100 00102 size_t submissions() const { return m_info.submissions(); } 00103 00105 size_t size() const 00106 { 00107 if (m_buf) 00108 return ((Score*)m_buf)->offset / sizeof(Score); 00109 else 00110 return 0; 00111 } 00112 00118 std::string name(size_t idx) const 00119 { 00120 const Score* s = structByIndex(idx); 00121 if (s == 0) return std::string(); 00122 return std::string(m_buf + s->offset); 00123 } 00124 00126 float scoreByIndex(size_t idx) const 00127 { 00128 const Score* s = structByIndex(idx); 00129 if (!s) return 0; 00130 return s->score; 00131 } 00132 00134 float scoreByName(const std::string& name) const; 00135 00137 float score(size_t idx) const { return scoreByIndex(idx); } 00138 00140 float operator[](int idx) const { return scoreByIndex(idx); } 00141 00143 float score(const std::string& name) const { return scoreByName(name); } 00144 00146 float operator[](const std::string& name) const { return scoreByName(name); } 00147 }; 00148 00149 } 00150 } 00151 00152 // vim:set ts=4 sw=4: 00153 #endif