libept
popcon.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*-
2 #ifndef EPT_POPCON_POPCON_H
3 #define EPT_POPCON_POPCON_H
4 
10 /*
11  * Copyright (C) 2007 Enrico Zini <enrico@debian.org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  */
27 
28 #include <tagcoll/diskindex/mmap.h>
29 #include <string>
30 
31 namespace ept {
32 namespace apt {
33 class Apt;
34 }
35 
36 namespace popcon {
37 
43 class Score
44 {
45 protected:
46  unsigned offset;
47 
48 public:
49  float score;
50 
51  Score(float score) : offset(offset), score(score) {}
52 
53  friend class Popcon;
54  friend class PopconIndexer;
55  friend class PopconGenerator;
56 };
57 
72 class Popcon : public tagcoll::diskindex::MMap
73 {
74  struct GeneralInfo : public tagcoll::diskindex::MMap
75  {
76  size_t submissions() const;
77  };
78 
79  tagcoll::diskindex::MasterMMap mastermmap;
80  time_t m_timestamp;
81 
82  GeneralInfo m_info;
83 
85  const Score* structByIndex(size_t idx) const
86  {
87  if (idx >= 0 && idx < size())
88  return (Score*)m_buf + idx;
89  return 0;
90  }
91 
92 public:
93  Popcon();
94 
96  time_t timestamp() const { return m_timestamp; }
97 
99  bool hasData() const { return m_timestamp != 0; }
100 
102  size_t submissions() const { return m_info.submissions(); }
103 
105  size_t size() const
106  {
107  if (m_buf)
108  return ((Score*)m_buf)->offset / sizeof(Score);
109  else
110  return 0;
111  }
112 
118  std::string name(size_t idx) const
119  {
120  const Score* s = structByIndex(idx);
121  if (s == 0) return std::string();
122  return std::string(m_buf + s->offset);
123  }
124 
126  float scoreByIndex(size_t idx) const
127  {
128  const Score* s = structByIndex(idx);
129  if (!s) return 0;
130  return s->score;
131  }
132 
134  float scoreByName(const std::string& name) const;
135 
137  float score(size_t idx) const { return scoreByIndex(idx); }
138 
140  float operator[](int idx) const { return scoreByIndex(idx); }
141 
143  float score(const std::string& name) const { return scoreByName(name); }
144 
146  float operator[](const std::string& name) const { return scoreByName(name); }
147 };
148 
149 }
150 }
151 
152 // vim:set ts=4 sw=4:
153 #endif