Skip to content

Commit

Permalink
Merge pull request HIT-SCIR#90 from endyul/osx_compile
Browse files Browse the repository at this point in the history
portable unordered map
  • Loading branch information
Oneplus committed Jan 19, 2015
2 parents 89465ae + f46e3f4 commit 884992c
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 88 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,39 @@ ltp_data/
# running folder #
##################
dummy/

#########
# CMake #
#########
CMakeCache.txt
CMakeFiles
Makefile
cmake_install.cmake
install_manifest.txt

#######
# OSX #
#######
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear on external disk
.Spotlight-V100
.Trashes

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

*.swp
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ project ("LTP - Language Technology Platform")
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

if (APPLE)
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libstdc++ -Wno-error=c++11-narrowing")
add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wno-c++11-narrowing")
endif(APPLE)

if (MINGW)
Expand Down
1 change: 0 additions & 1 deletion doc/ltp-document-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ Linux、Mac OSX(*)和Cygwin的用户,可以直接在项目根目录下使用
./configure
make

(注:Mac OSX如果要编译example下的示例程序,请加入-std=c++11 -stdlib=libstdc++ -Wno-error=c++11-narrowing选项)

进行编译。

Expand Down
9 changes: 2 additions & 7 deletions src/utils/cfgparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ class ConfigParser {
int _num_entries;
bool _valid;

#if defined(_MSC_VER)
typedef stdext::hash_map<std::string, std::string> internal_entries_t;
typedef stdext::hash_map<std::string, internal_entries_t> internal_sections_t;
#else
typedef std::tr1::unordered_map<std::string, std::string,
typedef std::unordered_map<std::string, std::string,
__Default_String_HashFunction> internal_entries_t;
typedef std::tr1::unordered_map<std::string, internal_entries_t,
typedef std::unordered_map<std::string, internal_entries_t,
__Default_String_HashFunction> internal_sections_t;
#endif // end for _WIN32

internal_sections_t sec;

Expand Down
7 changes: 1 addition & 6 deletions src/utils/chartypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,9 @@ class __chartype_collections {
}

private:
#if defined(_MSC_VER)
typedef stdext::hash_map<const char *, int,
utility::__Default_CharArray_HashFunction> internal_collection_t;
#else
typedef std::tr1::unordered_map<const char *, int,
typedef std::unordered_map<const char *, int,
utility::__Default_CharArray_HashFunction,
utility::__Default_CharArray_EqualFunction> internal_collection_t;
#endif // end for _WIN32

static __chartype_collections * instance_;
internal_collection_t collections;
Expand Down
6 changes: 0 additions & 6 deletions src/utils/hasher.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
#ifndef __HASHER_HPP__
#define __HASHER_HPP__

#if defined(_MSC_VER)
#include <hash_map>
#endif

#include <cstring>

namespace ltp {
namespace utility {

struct __Default_CharArray_HashFunction
#if defined(_MSC_VER)
: public stdext::hash_compare<const char *>
#endif
{
size_t operator () (const char* s) const {
unsigned int hash = 0;
Expand Down
9 changes: 3 additions & 6 deletions src/utils/math/sparsevec.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ namespace math {

class SparseVec {
public:
#if defined(_MSC_VER)
typedef stdext::hash_map<int,double> internal_sparsevec_t;
#else
typedef std::tr1::unordered_map<int, double> internal_sparsevec_t;
// typedef __gnu_cxx::hash_map<int, double> internal_sparsevec_t;
#endif // end for _WIN32

typedef std::unordered_map<int,double> internal_sparsevec_t;

typedef internal_sparsevec_t::iterator iterator;
typedef internal_sparsevec_t::const_iterator const_iterator;

Expand Down
7 changes: 1 addition & 6 deletions src/utils/stringmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@ namespace utility {
template <class T>
class StringMap {
public:
#if defined(_MSC_VER)
typedef stdext::hash_map<const char *, T,
__Default_CharArray_HashFunction> internal_map_t;
#else
typedef std::tr1::unordered_map<const char *, T,
typedef std::unordered_map<const char *, T,
__Default_CharArray_HashFunction,
__Default_CharArray_EqualFunction> internal_map_t;
#endif // end for _WIN32

typedef typename internal_map_t::iterator iterator;
typedef typename internal_map_t::const_iterator const_iterator;
Expand Down
8 changes: 1 addition & 7 deletions src/utils/template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ namespace utility {
template <typename T>
class __Template_Token_Cache {
private:
#if defined(_MSC_VER)
typedef stdext::hash_map<const char *, int,
__Default_CharArray_HashFunction> indexing_t;
#else
typedef std::tr1::unordered_map<const char *, int,
typedef std::unordered_map<const char *, int,
__Default_CharArray_HashFunction,
__Default_CharArray_EqualFunction> indexing_t;
#endif // end for _WIN32

public:
/**
* The entry function for the singleton. It's a typical get_instance
Expand Down
96 changes: 95 additions & 1 deletion src/utils/unordered_map.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,105 @@
// Portable STL hashmap include file.
#ifndef __UTILS_UNORDERED_MAP_HPP__
#define __UTILS_UNORDERED_MAP_HPP__
// Portable header for std::unordered_map<K,V> template. Welcome to C++. Enjoy!
// - rlyeh / BOOST licensed

/*
* Just in case somebody else defined `unordered_map` before us.
*/

#ifdef unordered_map
#undef unordered_map
#endif

/* Headers (in order)
* - std >= C++11: GCC <4.7.X defines __cplusplus as 1, use __GXX_EXPERIMENTAL_CXX0X__ instead
* - ICC
* - G++ >= 4.3.X
* - G++ >= 3.X.X
* - MSVC++ >= 9.0
* - OTHERS
*/

#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#include <unordered_map>
#elif defined(__INTEL_COMPILER)
#include <ext/hash_map>
#elif defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#include <tr1/unordered_map>
#elif defined(__GNUC__) && __GNUC__ >= 3
#include <ext/hash_map>
#elif defined(_MSC_VER) && ( ( _MSC_VER >= 1500 && _HAS_TR1 ) || ( _MSC_VER >= 1600 ) )
#include <unordered_map>
#else
#include <hash_map>
#endif

/* Namespace and type (in order)
* - C++11, C++0X (std::unordered_map)
* - STLPORT (std::hash_map)
* - MSVC++ 2010 (std::unordered_map)
* - MSVC++ 9.0 (std::tr1::unordered_map)
* - MSVC++ 7.0 (stdext::hash_map)
* - G++ 4.3.X (std::tr1::unordered_map)
* - G++ 3.X.X, ICC (__gnu_cxx::hash_map)
* - OTHERS (std::hash_map)
*/

#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
// ok

#elif defined(_STLPORT_VERSION)
#define unordered_map hash_map
namespace std { using std::hash_map; }

#elif defined(_MSC_VER) && _MSC_VER >= 1600
// ok

#elif defined(_MSC_VER) && _MSC_VER >= 1500 && _HAS_TR1
namespace std { using std::tr1::unordered_map; }

#elif defined(_MSC_VER) && _MSC_VER >= 1300
#define unordered_map hash_map
namespace std { using stdext::hash_map; }

#elif defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
namespace std { using std::tr1::unordered_map; }

#elif (defined(__GNUC__) && __GNUC__ >= 3) || defined(__INTEL_COMPILER)
#include <string>
#define unordered_map hash_map
namespace std { using __gnu_cxx::hash_map; }

namespace __gnu_cxx {
template<> struct hash<unsigned long long> {
size_t operator()(const unsigned long long &__x) const {
return (size_t)__x;
}
};
template<typename T> struct hash<T *> {
size_t operator()(T * const &__x) const {
return (size_t)__x;
}
};
template<> struct hash<std::string> {
size_t operator()(const std::string &__x) const {
return hash<const char *>()(__x.c_str());
}
};
};

#else
#define unordered_map hash_map
namespace std { using std::hash_map; }

#endif

/*
#if defined(_MSC_VER)
#include <hash_map>
#else
#include <tr1/unordered_map>
#endif

*/
#endif // end for __UTILS_UNORDERED_MAP_HPP__
38 changes: 19 additions & 19 deletions test/multi_cws_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "tinythread.h"
#include "segment_dll.h"

using namespace std;
using namespace tthread;
//using namespace std;
//using namespace tthread;

const int MAX_LEN = 1024;

Expand All @@ -35,20 +35,20 @@ class Dispatcher {
_model = model;
}

int next(string &sentence) {
int next(std::string &sentence) {
sentence = "";
lock_guard<mutex> guard(_mutex);
if (!getline(cin, sentence, '\n')) {
tthread::lock_guard<tthread::mutex> guard(_mutex);
if (!getline(std::cin, sentence, '\n')) {
return -1;
}
return 0;
}

void output(const vector<string> &result) {
lock_guard<mutex> guard(_mutex);
void output(const std::vector<std::string> &result) {
tthread::lock_guard<tthread::mutex> guard(_mutex);
for (int i = 0; i < result.size(); ++ i) {
cout << result[i];
cout << (i == result.size() - 1 ? '\n' : '\t');
std::cout << result[i];
std::cout << (i == result.size() - 1 ? '\n' : '\t');
}
return;
}
Expand All @@ -58,14 +58,14 @@ class Dispatcher {
}

private:
mutex _mutex;
tthread::mutex _mutex;
void * _model;
string _sentence;
std::string _sentence;
};

void multithreaded_segment( void * args) {
string sentence;
vector<string> result;
std::string sentence;
std::vector<std::string> result;

Dispatcher * dispatcher = (Dispatcher *)args;
void * model = dispatcher->model();
Expand Down Expand Up @@ -108,8 +108,8 @@ int main(int argc, char ** argv) {
return -1;
}

if(num_threads < 0 || num_threads > thread::hardware_concurrency()) {
num_threads = thread::hardware_concurrency();
if(num_threads < 0 || num_threads > tthread::thread::hardware_concurrency()) {
num_threads = tthread::thread::hardware_concurrency();
}

std::cerr << "TRACE: Model is loaded" << std::endl;
Expand All @@ -118,15 +118,15 @@ int main(int argc, char ** argv) {
Dispatcher* dispatcher = new Dispatcher( engine );

double tm = ltp::utility::get_time();
list<thread *> thread_list;
std::list<tthread::thread *> thread_list;
for (int i = 0; i < num_threads; ++ i) {
thread* t = new thread( multithreaded_segment, (void *)dispatcher );
tthread::thread* t = new tthread::thread( multithreaded_segment, (void *)dispatcher );
thread_list.push_back( t );
}

for (list<thread *>::iterator i = thread_list.begin();
for (std::list<tthread::thread *>::iterator i = thread_list.begin();
i != thread_list.end(); ++ i) {
thread * t = *i;
tthread::thread * t = *i;
t->join();
delete t;
}
Expand Down
Loading

0 comments on commit 884992c

Please sign in to comment.