root/lang/ruby/ruby-bayon/trunk/bayon/byvector.h @ 36827

Revision 36827, 6.6 kB (checked in by winebarrel, 3 years ago)
  • Property svn:eol-style set to native
Line 
1//
2// Utility class for vector operation
3//
4// Copyright(C) 2009  Mizuki Fujisawa <fujisawa@bayon.cc>
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; version 2 of the License.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18//
19
20#ifndef BAYON_BYVECTOR_H
21#define BAYON_BYVECTOR_H
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <cmath>
28#include <algorithm>
29#include <iostream>
30#include <utility>
31#include <vector>
32#include "util.h"
33
34namespace bayon {
35
36/********************************************************************
37 * Typedef
38 *******************************************************************/
39typedef long                            VecKey;   // key of vector
40typedef double                          VecValue; // value of vector
41typedef std::pair<VecKey, VecValue>     VecItem;  // key-value pair
42typedef HashMap<VecKey, VecValue>::type VecHashMap;
43
44
45/********************************************************************
46 * Constants
47 *******************************************************************/
48const VecKey   VECTOR_EMPTY_KEY   = -1;
49const VecKey   VECTOR_DELETED_KEY = -2;
50const VecValue VECTOR_NULL_VALUE  = 0.0;
51
52
53/********************************************************************
54 * Classes
55 *******************************************************************/
56/**
57 * Vector class
58 *
59 * This is utility class for vector operations.
60 */
61class Vector {
62 private:
63  /**
64   * internal hash_map object
65   */
66  VecHashMap vec_;
67
68 public:
69  /**
70   * Constructor
71   */
72  Vector() {
73    init_hash_map(VECTOR_EMPTY_KEY, vec_);
74  }
75
76  /**
77   * Constructor
78   *
79   * @param vec Vector object
80   */
81  Vector(const Vector &vec) {
82    init_hash_map(VECTOR_EMPTY_KEY, vec_);
83    for (VecHashMap::const_iterator it = vec.hash_map()->begin();
84         it != vec.hash_map()->end(); ++it) {
85      vec_[it->first] = it->second;
86    }
87  }
88
89  /**
90   * Destructor
91   */
92  ~Vector() { }
93
94  /**
95   * Set bucket count of internal hash_map object
96   *
97   * @param n bucket count
98   * @return void
99   */
100  void set_bucket_count(size_t n) {
101#if defined(HAVE_GOOGLE_DENSE_HASH_MAP) || defined(HAVE_EXT_HASH_MAP)
102    vec_.resize(n);
103#endif
104  }
105
106  /**
107   * Copy vector
108   *
109   * @param vec output vector
110   * @return void
111   */
112  void copy(Vector &vec) const {
113    vec.clear();
114    for (VecHashMap::const_iterator it = vec_.begin();
115         it != vec_.end(); ++it) {
116      vec.set(it->first, it->second);
117    }
118  }
119
120  /**
121   * Get value
122   *
123   * @param key key
124   * @return VecValue value
125   */
126  VecValue get(VecKey key) const {
127    VecHashMap::const_iterator it = vec_.find(key);
128    if (it != vec_.end()) return it->second;
129    else                  return VECTOR_NULL_VALUE;
130  }
131
132  /**
133   * Set value
134   *
135   * @param key key
136   * @param value value
137   * @return void
138   */
139  void set(VecKey key, VecValue value) {
140    vec_[key] = value;
141  }
142
143  /**
144   * Get size of vector
145   *
146   * @return size_t size of vector
147   */
148  size_t size() const {
149    return vec_.size();
150  }
151
152  /**
153   * Clear all items in vector
154   *
155   * @return void
156   */
157  void clear() {
158    vec_.clear();
159  }
160
161  /**
162   * Get pointer of internal hash_map object
163   *
164   * @return const VecHashMap* pointer of hash_map object
165   */
166  const VecHashMap *hash_map() const {
167    return &vec_;
168  }
169
170  /**
171   * Get pointer of internal hash_map object
172   *
173   * @return VecHashMap* pointer of hash_map object
174   */
175  VecHashMap *hash_map() {
176    return &vec_;
177  }
178
179  /**
180   * Get items sorted by value (desc order)
181   *
182   * @param items sorted keys
183   * @return void
184   */
185  void sorted_items(std::vector<VecItem> &items) const;
186
187  /**
188   * Get items sorted by absolute value (desc order)
189   *
190   * @param items sorted keys
191   * @return void
192   */
193  void sorted_items_abs(std::vector<VecItem> &items) const;
194
195  /**
196   * Normalize the vector
197   *
198   * @return void
199   */
200  void normalize();
201
202  /**
203   * Resize the vector
204   *
205   * @param size resized size
206   * @return void
207   */
208  void resize(size_t size);
209
210  /**
211   * Calculate squared norm of the vector
212   *
213   * @return double squared norm of the vector
214   */
215  double norm_squared() const;
216
217  /**
218   * Calculate norm of the vector
219   *
220   * @return double norm of the vector
221   */
222  double norm() const;
223
224  /**
225   * Multiply each value of vector by constant
226   *
227   * @param x constant value
228   * @return void
229   */
230  void multiply_constant(double x);
231
232  /**
233   * Add other vector
234   *
235   * @param vec input vector
236   * @return void
237   */
238  void add_vector(const Vector &vec);
239
240  /**
241   * Delete other vector
242   *
243   * @param vec input vector
244   * @return void
245   */
246  void delete_vector(const Vector &vec);
247
248  /**
249   * Calculate squared euclid distance between vectors
250   *
251   * @param vec1 input vector
252   * @param vec2 input vector
253   * @return double squared distance
254   */
255  static double euclid_distance_squared(const Vector &vec1, const Vector &vec2);
256
257  /**
258   * Calculate euclid distance bewteen vectors
259   *
260   * @param vec1 input vector
261   * @param vec2 input vector
262   * @return double distance
263   */
264  static double euclid_distance(const Vector &vec1, const Vector &vec2);
265
266  /**
267   * Calculate inner product value between vectors
268   *
269   * @param vec1 input vector
270   * @param vec2 input vector
271   * @return double inner product value
272   */
273  static double inner_product(const Vector &vec1, const Vector &vec2);
274
275  /**
276   * Calculate cosine value between vectors
277   *
278   * @param vec1 input vector
279   * @param vec2 input vector
280   * @return double cosine value
281   */
282  static double cosine(const Vector &vec1, const Vector &vec2);
283
284  /**
285   * Calculate Jaccard coefficient value between vectors
286   *
287   * @param vec1 input vector
288   * @param vec2 input vector
289   * @return double jaccard coefficient value
290   */
291  static double jaccard(const Vector &vec1, const Vector &vec2);
292
293  /**
294   * Output stream
295   */
296  friend std::ostream &operator <<(std::ostream &os, const Vector &vec) {
297    os.precision(4);
298    for (VecHashMap::const_iterator it = vec.vec_.begin();
299         it != vec.vec_.end(); ++it) {
300      if (it != vec.vec_.begin()) os << DELIMITER;
301      os << it->first << DELIMITER << it->second;
302    }
303    return os;
304  }
305};
306
307} /* namespace bayon */
308
309#endif
Note: See TracBrowser for help on using the browser.