diff --git a/cpp/cpp11/exception_context.cpp b/cpp/cpp11/exception_context.cpp new file mode 100644 index 0000000..31e3a2e --- /dev/null +++ b/cpp/cpp11/exception_context.cpp @@ -0,0 +1,57 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +void print_exception() +{ + try { + std::rethrow_exception(std::current_exception()); + } catch ( const std::exception& e ) { + std::cout << e.what() << std::endl; + } +} + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + try { + throw std::runtime_error("first exception"); + } catch(...) { + try { + throw std::runtime_error("second exception"); + } catch (...) { + print_exception(); + } + print_exception(); + } + + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + diff --git a/cpp/stl/multiple_shared_future_get.cpp b/cpp/stl/multiple_shared_future_get.cpp new file mode 100644 index 0000000..fd08061 --- /dev/null +++ b/cpp/stl/multiple_shared_future_get.cpp @@ -0,0 +1,54 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + std::promise p; + auto f = p.get_future().share(); + + std::thread t1([f](){ + std::cout << ' ' << f.get() << std::endl; + }); + + std::thread t2([f](){ + std::cout << ' ' << f.get() << std::endl; + }); + + + p.set_value(55); + + t1.join(); + t2.join(); + + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + diff --git a/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp b/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp new file mode 100644 index 0000000..b22b618 --- /dev/null +++ b/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp @@ -0,0 +1,121 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:argv="" +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +/* + * Complete the function below. + */ +bool match(const string& pattern, const string& string) { + + auto s = string.begin(); + auto p = pattern.begin(); + + const std::string::const_iterator empty; + auto prev_p = empty; + auto prev_s = s; + while (s != string.end()) { + while ( p != pattern.end() && *p == '*' ) { + p++; + prev_p = p; + prev_s = s; + } + if ( p == pattern.end() ) { + if ( prev_p == p ){ + return true; + } else if ( prev_p != empty ) { + p = prev_p; + s = ++prev_s; + } else { + return false; + } + } + else if ( *s == *p ){ + s++; + p++; + } + else if ( *p == '?' ){ + s++; + p++; + } + else if ( prev_p != empty ) { + p = prev_p; + s = ++prev_s; + } + else { + return false; + } + } + while ( *p == '*' ) { + p++; + } + return p == pattern.end(); +} + + +void test( const std::string& str, const std::string& pattern ){ + bool res = match(pattern, str); + std::cout << str << " - " << pattern << " -> " << res < +#include +#include + + +template +struct power_of +{ + enum { + value = power_of<(N+1)/2>::value * 2 + }; +}; + +template <> +struct power_of<2> +{ + enum { + value = 2 + }; +}; + +template <> +struct power_of<1> +{ + enum { + value = 1 + }; +}; + +template <> +struct power_of<0> +{ + enum { + value = 0 + }; +}; + +template +void test() +{ + std::cout << N << "=> " << power_of::value <(); + test<1>(); + test<2>(); + test<3>(); + test<4>(); + test<5>(); + test<6>(); + test<7>(); + test<8>(); + test<9>(); + test<127>(); + test<128>(); + test<129>(); + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + diff --git a/puzzles/interviews/training/simple_wildcard_match.cpp b/puzzles/interviews/training/simple_wildcard_match.cpp new file mode 100644 index 0000000..9ffc822 --- /dev/null +++ b/puzzles/interviews/training/simple_wildcard_match.cpp @@ -0,0 +1,95 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +/* +Simple wildcard match. + +*/ + +bool wildcard_match(const std::string& input, const std::string& pattern) +{ + int j = 0; + int i = 0; + int restart_idx = -1; + while ( i < input.size() ) + { + while ( j < pattern.size() && pattern[j] == '*' ){ + restart_idx = ++j; + } + if ( j= 0 && pattern[j-1] == '*' ) { + ++i; + } + else if ( restart_idx > 0 ) { + i -= j - restart_idx -1; + j = restart_idx; + } + else { + return false; + } + } + while ( j < pattern.size() && pattern[j] == '*' ){ + ++j; + } + return j == pattern.size(); +} + +void test(const std::string& input, const std::string& pattern) +{ + std::cout << input << " x " << pattern + << " == " << wildcard_match(input,pattern) + << std::endl; +} + + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + test("zebra", "dog"); + test("zebra", "zebra"); + test("zebra", "*br*"); + test("zebra", "*bi*"); + test("zebra", "*br*a"); + test("zebra", "*br*b"); + test("zebra", "z**a"); + test("zebra", "z*a"); + test("zebra", "x**a"); + test("zebrabrb", "*brb*"); + test("zebra", "*bra*"); + test("zebrazebrazebra", "*bra*zeb*zeb*"); + test("zebrbzebrazebra", "*bra*zeb*zeb*"); + test("zebrbzebrazebra", "*bra*zeb*"); + test("zebrbzebrazebra", "*****bra*zeb*****"); + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} +