Thread: [Assorted-commits] SF.net SVN: assorted: [349] cpp-commons/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-10 18:28:45
|
Revision: 349 http://assorted.svn.sourceforge.net/assorted/?rev=349&view=rev Author: yangzhang Date: 2008-02-10 10:28:49 -0800 (Sun, 10 Feb 2008) Log Message: ----------- imported code from hash-join; broke autotools Modified Paths: -------------- cpp-commons/trunk/src/Makefile.am Added Paths: ----------- cpp-commons/trunk/src/commons/ cpp-commons/trunk/src/commons/check.h cpp-commons/trunk/src/commons/cppcommons.cpp cpp-commons/trunk/src/commons/files.h cpp-commons/trunk/src/commons/strings.h cpp-commons/trunk/src/commons/threads.h cpp-commons/trunk/src/commons/time.h cpp-commons/trunk/src/test/ Removed Paths: ------------- cpp-commons/trunk/src/cppcommons.cpp cpp-commons/trunk/src/files.h cpp-commons/trunk/src/test.cpp Modified: cpp-commons/trunk/src/Makefile.am =================================================================== --- cpp-commons/trunk/src/Makefile.am 2008-02-10 04:04:44 UTC (rev 348) +++ cpp-commons/trunk/src/Makefile.am 2008-02-10 18:28:49 UTC (rev 349) @@ -6,8 +6,8 @@ # the library search path. cppcommons_LDFLAGS = $(all_libraries) lib_LIBRARIES = libcppcommons.a -cppcommons_SOURCES = test.cpp -libcppcommons_a_SOURCES = cppcommons.cpp files.h -noinst_HEADERS = files.h +cppcommons_SOURCES = test/files.cpp +libcppcommons_a_SOURCES = commons/cppcommons.cpp commons/files.h +noinst_HEADERS = commons/files.h AM_CXXFLAGS = -I/opt/boost-1.34.0/include/ cppcommons_LDADD = -lboost_filesystem-gcc41 Added: cpp-commons/trunk/src/commons/check.h =================================================================== --- cpp-commons/trunk/src/commons/check.h (rev 0) +++ cpp-commons/trunk/src/commons/check.h 2008-02-10 18:28:49 UTC (rev 349) @@ -0,0 +1,47 @@ +#ifndef _COMMONS_CHECK_H +#define _COMMONS_CHECK_H + +#include <exception> +#include <sstream> +#include <string> + +namespace commons +{ + + using namespace std; + + class check_exception : exception + { + public: + check_exception(const string & name) : name(name) {} + virtual ~check_exception() throw() {} + private: + const string name; + }; + + inline void + _check(bool cond, const char *msg, const char *file, int line) + { + if (!cond) { + stringstream ss; + ss << file << ':' << line << ": "; + if (msg != NULL) ss << msg; + ss << endl; + throw check_exception(ss.str()); + } + } + +} + +#define check(cond) _check(cond, NULL, __FILE__, __LINE__) + + /** + * Similar to assert(), but is not conditionally compiled, so this is safe to + * use as a guard against expected failures (such as checking return codes). + */ +#define checkmsg(cond, msg) \ + bool b = cond; \ + if (!b) _check(b, (msg), __FILE__, __LINE__) + +#endif + Added: cpp-commons/trunk/src/commons/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/commons/cppcommons.cpp (rev 0) +++ cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:28:49 UTC (rev 349) @@ -0,0 +1,31 @@ +/*************************************************************************** + * Copyright (C) 2007 by Yang Zhang * + * gmail:yaaang * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "commons/check.h" +#include "commons/files.h" +#include "commons/strings.h" +#include "commons/time.h" +#include "commons/threads.h" + Added: cpp-commons/trunk/src/commons/files.h =================================================================== --- cpp-commons/trunk/src/commons/files.h (rev 0) +++ cpp-commons/trunk/src/commons/files.h 2008-02-10 18:28:49 UTC (rev 349) @@ -0,0 +1,90 @@ +#ifndef _COMMONS_FILES_H +#define _COMMONS_FILES_H + +#include <exception> +#include <fstream> +#include <iostream> +#include <string> +#include <vector> + +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> + +#include "commons/check.h" + +namespace commons +{ + + using namespace std; + + class file_not_found_exception : exception { + public: + file_not_found_exception(const string & name) : name(name) {} + virtual ~file_not_found_exception() throw() {} + private: + const string name; + }; + + /** + * Read in a whole file as a string. + */ + void read_file_as_string ( const string & name, string & out ) { + ifstream in ( name.c_str() ); + if (in.fail()) throw file_not_found_exception( name ); + out = string ( istreambuf_iterator<char> ( in ), istreambuf_iterator<char>() ); + } + + /** + * Read in a whole file as a vector of chars. + */ + void read_file_as_vector ( const string & name, vector<char> & out ) { + ifstream in ( name.c_str() ); + if ( in.fail() ) throw file_not_found_exception( name ); + out = vector<char> ( istreambuf_iterator<char> ( in ), istreambuf_iterator<char>() ); + } + + /** + * Load an entire file directly into buf and also give us the length of the + * buffer (size of the file). + * TODO this probably isn't very safe, since we're demoting an off_t to a + * size_t. Is there a healthier approach? + * TODO move to C99 commons + */ + char * + load_file(const char *path, size_t & len, unsigned int ncpus) { + struct stat sb; + int fd; + + fd = open(path, 0); + check(fd >= 0); + + check(fstat(fd, &sb) == 0); + check(sb.st_size <= 0xffffffff); + + // TODO Why don't we need (static) cast here? Isn't this a lossy cast? + len = sb.st_size; + + char *buf = new char[len + 1]; + check(buf); + + // TODO Use threads to pull data to the correct initial locations? + size_t chunk_len = len / ncpus; + for (unsigned int i = 0; i < ncpus; i++) { + int off = i *chunk_len; + ssize_t status = pread(fd, buf + off, chunk_len, off); + // We read the whole chunk or hit the end. + size_t nread = static_cast<ssize_t>(status); + check(status != -1 && (nread == chunk_len || off + nread == len)); + } + + check(close(fd) == 0); + + buf[len] = '\0'; // don't let strcmp() run off the end + return buf; + } + +} + +#endif Added: cpp-commons/trunk/src/commons/strings.h =================================================================== --- cpp-commons/trunk/src/commons/strings.h (rev 0) +++ cpp-commons/trunk/src/commons/strings.h 2008-02-10 18:28:49 UTC (rev 349) @@ -0,0 +1,82 @@ +// TODO: move whatever you can to C99 Commons + +#ifndef _COMMONS_STRINGS_H +#define _COMMONS_STRINGS_H + +#include <strings.h> + +#include "commons/check.h" + +namespace commons +{ + + using namespace std; + + /** + * Search in p for the nth instance of c and return the character past it. + */ + inline const char * + strchrrep(const char *p, char c, int n) + { + for (int i = 0; i < n; i++) { + p = strchr(p, c); + check(p); + p++; + } + return p; + } + + /** + * Search in p for the nth instance of c and return the character past it. + */ + inline char * + strchrrep(char *p, char c, int n) + { + return const_cast<char *>(strchrrep(const_cast<const char *>(p), c, n)); + } + + /** + * A functor that checks for string equality. Mainly useful as a template + * parameter to the hash data structures in STL extensions. + */ + struct eqstr + { + bool operator()(const char* s1, const char* s2) const + { + return strcmp(s1, s2) == 0; + } + }; + + /** + * Look for a substring, but without null-termination conventions. + */ + inline char * + unsafe_strstr(char *p, const char *q, const char *lim) + { + if (lim == 0) { + while (true) { + for (; !(*p == '\0' && *(p+1) == '\0'); p++); + return p; + } + } else { + check(p < lim); + while (true) { + for (; !(*p == '\0' && *(p+1) == '\0') && p < lim; p++); + if (p == lim) return NULL; + return p; + } + } + } + + /** + * Look for a substring, but without null-termination conventions. + */ + inline const char* + unsafe_strstr(const char *p, const char *q, const char *lim) + { + return unsafe_strstr((char*) p, q, lim); + } + +} + +#endif Added: cpp-commons/trunk/src/commons/threads.h =================================================================== --- cpp-commons/trunk/src/commons/threads.h (rev 0) +++ cpp-commons/trunk/src/commons/threads.h 2008-02-10 18:28:49 UTC (rev 349) @@ -0,0 +1,138 @@ +// TODO: use boost::bind? + +#ifndef _COMMONS_THREADS_H +#define _COMMONS_THREADS_H + +#include <pthread.h> + +namespace commons +{ + + // non-rpc-specific utility to start a thread that runs + // an object method. returns a pthread_t on success, and + // zero on error. + template <class C> pthread_t + method_thread(C *o, void (C::*m)()) + { + class XXX { + public: + C *o; + void (C::*m)(); + static void *yyy(void *vvv) { + XXX *x = (XXX*)vvv; + C *o = x->o; + void (C::*m)() = x->m; + delete x; + (o->*m)(); + return 0; + } + }; + XXX *x = new XXX; + x->o = o; + x->m = m; + pthread_t th; + if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + return th; + } + return 0; + } + + template <class C, class A> pthread_t + method_thread(C *o, void (C::*m)(A), A a) + { + class XXX { + public: + C *o; + void (C::*m)(A a); + A a; + static void *yyy(void *vvv) { + XXX *x = (XXX*)vvv; + C *o = x->o; + void (C::*m)(A ) = x->m; + A a = x->a; + delete x; + (o->*m)(a); + return 0; + } + }; + XXX *x = new XXX; + x->o = o; + x->m = m; + x->a = a; + pthread_t th; + if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + return th; + } + return 0; + } + + template <class C, class A1, class A2> pthread_t + method_thread(C *o, void (C::*m)(A1 , A2 ), A1 a1, A2 a2) + { + class XXX { + public: + C *o; + void (C::*m)(A1 a1, A2 a2); + A1 a1; + A2 a2; + static void *yyy(void *vvv) { + XXX *x = (XXX*)vvv; + C *o = x->o; + void (C::*m)(A1 , A2 ) = x->m; + A1 a1 = x->a1; + A2 a2 = x->a2; + delete x; + (o->*m)(a1, a2); + return 0; + } + }; + XXX *x = new XXX; + x->o = o; + x->m = m; + x->a1 = a1; + x->a2 = a2; + pthread_t th; + if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + return th; + } + return 0; + } + + template <class C, class A1, class A2, class A3> pthread_t + method_thread(C *o, void (C::*m)(A1 , A2, A3), A1 a1, A2 a2, A3 a3) + { + class XXX { + public: + C *o; + void (C::*m)(A1 a1, A2 a2, A3 a3); + A1 a1; + A2 a2; + A3 a3; + static void *yyy(void *vvv) { + XXX *x = (XXX*)vvv; + C *o = x->o; + void (C::*m)(A1, A2, A3) = x->m; + A1 a1 = x->a1; + A2 a2 = x->a2; + A3 a3 = x->a3; + delete x; + (o->*m)(a1, a2, a3); + return 0; + } + }; + XXX *x = new XXX; + x->o = o; + x->m = m; + x->a1 = a1; + x->a2 = a2; + x->a3 = a3; + pthread_t th; + if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ + return th; + } + return 0; + } + +} + +#endif Added: cpp-commons/trunk/src/commons/time.h =================================================================== --- cpp-commons/trunk/src/commons/time.h (rev 0) +++ cpp-commons/trunk/src/commons/time.h 2008-02-10 18:28:49 UTC (rev 349) @@ -0,0 +1,54 @@ +#ifndef _COMMONS_TIME_H +#define _COMMONS_TIME_H + +#include <string> +#include <iostream> + +#include <sys/time.h> +#include <time.h> + +namespace commons +{ + + using namespace std; + + /** + * Get the current time in milliseconds. + * TODO: move to C99 Commons. + */ + inline long long + current_time_millis() + { + long long t; + struct timeval tv; + + gettimeofday(&tv, 0); + + t = tv.tv_sec; + t = (t *1000) + (tv.tv_usec/1000); + + return t; + } + + /** + * Convenience class for performing wall-clock benchmarking. + */ + class timer + { + public: + timer(const string label) : + label(label), start(current_time_millis()), last(start) {} + void print() + { + long long now = current_time_millis(); + cout << label << now - last << endl; + last = now; + } + private: + const string label; + long long start, last; + }; + +} + +#endif Deleted: cpp-commons/trunk/src/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/cppcommons.cpp 2008-02-10 04:04:44 UTC (rev 348) +++ cpp-commons/trunk/src/cppcommons.cpp 2008-02-10 18:28:49 UTC (rev 349) @@ -1,27 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Yang Zhang * - * gmail:yaaang * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU Library General Public License as * - * published by the Free Software Foundation; either version 2 of the * - * License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU Library General Public * - * License along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "files.h" - Deleted: cpp-commons/trunk/src/files.h =================================================================== --- cpp-commons/trunk/src/files.h 2008-02-10 04:04:44 UTC (rev 348) +++ cpp-commons/trunk/src/files.h 2008-02-10 18:28:49 UTC (rev 349) @@ -1,46 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Yang Zhang * - * gmail:yaaang * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU Library General Public License as * - * published by the Free Software Foundation; either version 2 of the * - * License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU Library General Public * - * License along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -#include <vector> -#include <iostream> -#include <fstream> -#include <string> -#include <exception> - -using namespace std; - -class file_not_found_exception : exception { - public: - file_not_found_exception(const string & name) : name(name) {} - virtual ~file_not_found_exception() throw() {} - private: - const string name; -}; - -void read_file_as_string ( const string & name, string & out ) { - ifstream in ( name.c_str() ); - if (in.fail()) throw file_not_found_exception( name ); - out = string ( istreambuf_iterator<char> ( in ), istreambuf_iterator<char>() ); -} - -void read_file_as_vector ( const string & name, vector<char> & out ) { - ifstream in ( name.c_str() ); - if ( in.fail() ) throw file_not_found_exception( name ); - out = vector<char> ( istreambuf_iterator<char> ( in ), istreambuf_iterator<char>() ); -} Deleted: cpp-commons/trunk/src/test.cpp =================================================================== --- cpp-commons/trunk/src/test.cpp 2008-02-10 04:04:44 UTC (rev 348) +++ cpp-commons/trunk/src/test.cpp 2008-02-10 18:28:49 UTC (rev 349) @@ -1,37 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Yang Zhang * - * gmail:yaaang * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU Library General Public License as * - * published by the Free Software Foundation; either version 2 of the * - * License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU Library General Public * - * License along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#include "files.h" - -#include <cstdlib> -#include <boost/filesystem.hpp> - -using namespace boost::filesystem; - -int main ( int argc, char *argv[] ) { - cout << "Hello, world!" << current_path() << endl; - - for ( int i = 0; i < 10; i++ ) { - vector<char> v; - read_file_as_vector("codex.umz", v); - } - - return EXIT_SUCCESS; -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-15 18:46:57
|
Revision: 454 http://assorted.svn.sourceforge.net/assorted/?rev=454&view=rev Author: yangzhang Date: 2008-02-15 10:47:01 -0800 (Fri, 15 Feb 2008) Log Message: ----------- moved cppcommons.cpp to test/all.cc Added Paths: ----------- cpp-commons/trunk/src/test/all.cc Removed Paths: ------------- cpp-commons/trunk/src/commons/cppcommons.cpp Deleted: cpp-commons/trunk/src/commons/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-15 18:46:09 UTC (rev 453) +++ cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-15 18:47:01 UTC (rev 454) @@ -1,35 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Yang Zhang * - * gmail:yaaang * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU Library General Public License as * - * published by the Free Software Foundation; either version 2 of the * - * License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU Library General Public * - * License along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <commons/boost/threads.h> -#include <commons/check.h> -#include <commons/cpuid.h> -#include <commons/files.h> -#include <commons/hash.h> -#include <commons/strings.h> -#include <commons/time.h> -#include <commons/threads.h> -#include <commons/x86asm.h> - Copied: cpp-commons/trunk/src/test/all.cc (from rev 446, cpp-commons/trunk/src/commons/cppcommons.cpp) =================================================================== --- cpp-commons/trunk/src/test/all.cc (rev 0) +++ cpp-commons/trunk/src/test/all.cc 2008-02-15 18:47:01 UTC (rev 454) @@ -0,0 +1,35 @@ +/*************************************************************************** + * Copyright (C) 2007 by Yang Zhang * + * gmail:yaaang * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <commons/boost/threads.h> +#include <commons/check.h> +#include <commons/cpuid.h> +#include <commons/files.h> +#include <commons/hash.h> +#include <commons/strings.h> +#include <commons/time.h> +#include <commons/threads.h> +#include <commons/x86asm.h> + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |