Permalink
Browse files

clean up itemmap code

  • Loading branch information...
1 parent 6bbf04c commit d9b3315f93f026ae56f99f0594b17dc30f3b072d Le Zhang committed Feb 9, 2012
Showing with 35 additions and 33 deletions.
  1. +3 −3 INSTALL
  2. +9 −6 src/itemmap.hpp
  3. +17 −16 src/itemmap.tcc
  4. +6 −8 src/lbfgstrainer.hpp
View
@@ -13,9 +13,9 @@ The following build dependancies are required:
* zlib library and headers (optional)
* a fortran compiler (optional but highly recommended)
-The boost lib shipped with this package is a subset of the full boost lib:
-only headers used during compilation with gcc 3.2 on Linux/FreeBSD/Cygwin are
-included.
+The boost lib shipped with this package is a subset of the full boost lib. If
+you encounter building errors then please download the latest boost lib and
+try again.
You need to have a working Fortran compiler in order to take advantage of
LBFGS code. On OS X you can get gfortran installed using Homebrew:
View
@@ -6,7 +6,7 @@
*
* Copyright (C) 2002 by Zhang Le <ejoy@users.sourceforge.net>
* Begin : 31-Dec-2002
- * Last Change : 08-Feb-2012.
+ * Last Change : 09-Feb-2012.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -37,13 +37,14 @@
#include <tr1/unordered_map>
-template <typename T>
+template <typename T,
+ typename _Hash = std::tr1::hash<T>,
+ typename _Pred = std::equal_to<T> >
class ItemMap {
public:
typedef T item_type;
typedef size_t id_type;
- typedef std::tr1::unordered_map <T, id_type> hash_map_type;
- // static const size_t null_id = ~(ItemMap::id_type)0;
+ typedef std::tr1::unordered_map <T, id_type, _Hash, _Pred> hash_map_type;
static const size_t null_id;
typedef typename std::vector<T>::iterator iterator;
typedef typename std::vector<T>::const_iterator const_iterator;
@@ -97,7 +98,9 @@ class ItemMap {
std::vector<T> m_index;
};
-template <typename T>
-const size_t ItemMap<T>::null_id = ~(typename ItemMap<T>::id_type)(0); // -1 is null_id
+
+template <typename T, typename _Hash, typename _Pred>
+const size_t ItemMap<T, _Hash, _Pred>::null_id =
+~(typename ItemMap<T, _Hash, _Pred>::id_type)(0); // -1 is null_id
#include "itemmap.tcc"
#endif /* ifndef ITEMMAP_H */
View
@@ -6,7 +6,7 @@
*
* Copyright (C) 2002 by Zhang Le <ejoy@users.sourceforge.net>
* Begin : 31-Dec-2002
- * Last Change : 08-Feb-2012.
+ * Last Change : 09-Feb-2012.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -31,17 +31,29 @@
using namespace std;
-template<typename T>
-ItemMap<T>::~ItemMap() {
+template <typename T, typename _Hash, typename _Pred>
+ItemMap<T, _Hash, _Pred>::~ItemMap() {
clear();
}
-template<typename T>
-void ItemMap<T>::clear() {
+template <typename T, typename _Hash, typename _Pred>
+void ItemMap<T, _Hash, _Pred>::clear() {
m_index.clear();
m_hashdict.clear();
}
+template <typename T, typename _Hash, typename _Pred>
+typename ItemMap<T, _Hash, _Pred>::id_type ItemMap<T, _Hash, _Pred>::add(const T& f) {
+ typename hash_map_type::const_iterator it = m_hashdict.find(f);
+ if (it != m_hashdict.end())
+ return it->second;
+
+ id_type id = m_index.size();
+ m_hashdict[f] = id;
+ m_index.push_back(f);
+ return id;
+}
+
/**
* load feature map of T from a binary file.
*/
@@ -111,15 +123,4 @@ void ItemMap<string>::save(ostream& os) {
// save(out);
//}
-template<typename T>
-typename ItemMap<T>::id_type ItemMap<T>::add(const T& f) {
- typename hash_map_type::const_iterator it = m_hashdict.find(f);
- if (it != m_hashdict.end())
- return it->second;
-
- id_type id = m_index.size();
- m_hashdict[f] = id;
- m_index.push_back(f);
- return id;
-}
View
@@ -7,7 +7,7 @@
*
* Copyright (C) 2003 by Zhang Le <ejoy@users.sourceforge.net>
* Begin : 01-Jun-2003
- * Last Change : 08-Feb-2012.
+ * Last Change : 09-Feb-2012.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -44,13 +44,11 @@ class LBFGSTrainer : public Trainer {
void init_trainer();
double heldout_accuracy() const;
- struct featid_hasher {
- size_t operator()(const pair<size_t, size_t>& p) const {
- return p.first + p.second;
- }
- };
-
-#endif
+ struct featid_hasher {
+ size_t operator()(const pair<size_t, size_t>& p) const {
+ return p.first + p.second;
+ }
+ };
shared_array<double> m_observed_expects;
};

0 comments on commit d9b3315

Please sign in to comment.