From 721e183dc989d11d811131358c412a984ab458c9 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Thu, 25 Mar 2021 08:15:36 -0400 Subject: [PATCH] last changes from bloomberg. --- cpp/boost/multi_index.cpp | 87 +++++++ cpp/cpp11/forwarding_reference.cpp | 102 ++++++++ cpp/cpp11/initializer_list.cpp | 72 ++++++ cpp/cpp11/overload.cpp | 42 ++++ cpp/cpp11/test1.cpp | 93 ++++++++ cpp/cpp11/trnx_order.h | 75 ++++++ cpp/cpp11/trnx_vector.h | 224 ++++++++++++++++++ cpp/cpp11/trnx_vector_impl.h | 106 +++++++++ cpp/different_linkage.cpp | 31 +++ cpp/hidden_method_call.cpp | 64 +++++ cpp/p.cpp | 55 +++++ cpp/shared_ptr.cpp | 34 +++ cpp/shared_ptr_cast.cpp | 23 ++ cpp/stl/istringstream_fail_eof.cpp | 77 ++++++ cpp/tbb/message_flow_graph_example.cpp | 73 ++++++ cpp/tbb/simple_flow_graph.cpp | 70 ++++++ cpp/tbb/tbb_unspawned_child.cpp | 97 ++++++++ cpp/test_const_cast_string.cpp | 38 +++ .../training/two_segments_intersect.cpp | 22 ++ py/python_path.py | 6 + 20 files changed, 1391 insertions(+) create mode 100644 cpp/boost/multi_index.cpp create mode 100644 cpp/cpp11/forwarding_reference.cpp create mode 100644 cpp/cpp11/initializer_list.cpp create mode 100644 cpp/cpp11/overload.cpp create mode 100644 cpp/cpp11/test1.cpp create mode 100644 cpp/cpp11/trnx_order.h create mode 100644 cpp/cpp11/trnx_vector.h create mode 100644 cpp/cpp11/trnx_vector_impl.h create mode 100644 cpp/different_linkage.cpp create mode 100644 cpp/hidden_method_call.cpp create mode 100644 cpp/p.cpp create mode 100644 cpp/shared_ptr.cpp create mode 100644 cpp/shared_ptr_cast.cpp create mode 100644 cpp/stl/istringstream_fail_eof.cpp create mode 100644 cpp/tbb/message_flow_graph_example.cpp create mode 100644 cpp/tbb/simple_flow_graph.cpp create mode 100644 cpp/tbb/tbb_unspawned_child.cpp create mode 100644 cpp/test_const_cast_string.cpp create mode 100644 puzzles/interviews/training/two_segments_intersect.cpp create mode 100644 py/python_path.py diff --git a/cpp/boost/multi_index.cpp b/cpp/boost/multi_index.cpp new file mode 100644 index 0000000..a6375b8 --- /dev/null +++ b/cpp/boost/multi_index.cpp @@ -0,0 +1,87 @@ +/* Check cf5-opt.vim defs. +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:cppflags=g:Iboost.g:Itbb +VIM: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc.g:tbbmproxy +VIM: let g:ldlibpath=g:Bboost.g:Btbb +VIM: let g:argv="" +VIM-: let g:cf5output=0 +*/ +#include +#include + +#include +//#include +#include +#include +#include +#include + +struct entry +{ + std::string d_sessionId; + std::string d_curveId; + std::string key3; +}; + +typedef boost::multi_index::ordered_unique< + boost::multi_index::identity + > index_by_entry; + +struct ByCurveId {}; +typedef boost::multi_index::hashed_unique< + boost::multi_index::tag, + boost::multi_index::member< + entry, + std::string, + &entry::d_curveId + > + > index_ByCurveId; + +struct BySessionId {}; +typedef boost::multi_index::hashed_unique< + boost::multi_index::tag, + boost::multi_index::member< + entry, + std::string, + &entry::d_sessionId + > + > index_BySessionId; + +typedef boost::multi_index_container< + entry, + boost::multi_index::indexed_by< + //boost::multi_index::sequenced<>, + //index_by_entry, + index_BySessionId, + index_ByCurveId + > + > my_multi_index_container_type; + +int main ( void ) +{try{ + + my_multi_index_container_type c; + + if ( c.get().find( "tutuc" ) + == c.get().end() ) + { + std::cout << "no tutuc\n"; + } + + + 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/cpp11/forwarding_reference.cpp b/cpp/cpp11/forwarding_reference.cpp new file mode 100644 index 0000000..04de529 --- /dev/null +++ b/cpp/cpp11/forwarding_reference.cpp @@ -0,0 +1,102 @@ +/* +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 + +class A +{ +public: + int i; + + A() { + std::cout << "default ctr" << std::endl; + } + + A(const A&) { + std::cout << "copy ctr" << std::endl; + } + + A(A&&) { + std::cout << "move ctr" << std::endl; + } + + void operator = (const A&) { + std::cout << "copy" << std::endl; + } + + void operator = (A&&) { + std::cout << "move" << std::endl; + } +}; + +void test3( A a ){ + std::cout << "by value" << std::endl; +} +void test3( A&& a ){ + std::cout << "rvalue" << std::endl; +} +void test3( const A& a ){ + std::cout << "const lvalue&" << std::endl; +} +void test3( A& a ){ + std::cout << "lvalue &" << std::endl; +} + + +template +void test2( T&& t ){ + test3(std::forward(t)); +} + +template +void test( T t ){ + //test2(std::forward(t)); + test3(t); +} + +A rvalue() +{ + return A(); +} + +A& lvalue() +{ + static A a; + return a; +} + +const A& clvalue() +{ + static A a; + return a; +} + + +int main ( void ) +{try{ + + test(rvalue()); + test(lvalue()); + test(clvalue()); + + 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/cpp11/initializer_list.cpp b/cpp/cpp11/initializer_list.cpp new file mode 100644 index 0000000..2a99ab5 --- /dev/null +++ b/cpp/cpp11/initializer_list.cpp @@ -0,0 +1,72 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread" +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM: let b:argv="" +*/ +#include +#include +#include +//#include + +template +void print( CONT&& cont ) +{ + std::cout << "template "; + for( auto n : cont ){ + std::cout << n << ' '; + } + std::cout << std::endl; +} + +#if 1 +void print( const std::initializer_list& cont ) +{ + std::cout << "initializer_list "; + for( auto n : cont ){ + std::cout << n << ' '; + } + std::cout << std::endl; +} + +#if 1 +void print( const std::initializer_list& cont ) +{ + std::cout << "initializer_list "; + for( auto n : cont ){ + std::cout << n << ' '; + } + std::cout << std::endl; +} +#endif +#endif + +int main ( void ) +{try{ + + std::vector v = {1,2,3}; + + print(v); + + + print({3,4,5}); + + print({3LL,4LL,5LL}); + + print({3LL,4,5}); + + + 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/cpp11/overload.cpp b/cpp/cpp11/overload.cpp new file mode 100644 index 0000000..e93726c --- /dev/null +++ b/cpp/cpp11/overload.cpp @@ -0,0 +1,42 @@ +/* +VIM: let b:lcppflags="-std=c++11 -O2 -pthread" +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM: let b:argv="" +*/ +#include +#include +#include + +void a( std::string&& s ) +{ + std::cout << " by value " << std::endl; +} + +void a( const std::string& s ) +{ + std::cout << " const ref " << std::endl; +} + + +int main ( void ) +{try{ + std::string s = "bbbbbb"; + + a(s); + a(std::string("aaaa")); + + 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/cpp11/test1.cpp b/cpp/cpp11/test1.cpp new file mode 100644 index 0000000..290903e --- /dev/null +++ b/cpp/cpp11/test1.cpp @@ -0,0 +1,93 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM: let b:argv="" +*/ +#include +#include +#include + +namespace trnx { + +struct A{ + int a1; +}; + +void test() +{ + trnx::vector v; + v.push_back(A{0}); + v.push_back(A{1}); + v.push_back(A{2}); + + for(const auto& x : v) + { + std::cout << x.a1 << ' '; + } + std::cout << std::endl; +} + +#if 1 +struct MoveOnly +{ + int d_datum; + + // Allow moves + MoveOnly(MoveOnly&&) = default; + MoveOnly& operator=(MoveOnly&&) = default; + + // Prevent copies + MoveOnly(const MoveOnly&) = delete; + MoveOnly& operator=(const MoveOnly&) = delete; +}; + +void lab1() +{ + trnx::vector v; + v.push_back(MoveOnly{0}); + v.push_back(MoveOnly{1}); + v.push_back(MoveOnly{2}); + + for(const auto& x : v) + { + std::cout << x.d_datum << ' '; + } + std::cout << std::endl; +} +#endif + + + + + + +void main() +{ + test(); + lab1(); +} + + + +} // close namespace trnx + +int main ( void ) +{try{ + + trnx::main(); + + 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/cpp11/trnx_order.h b/cpp/cpp11/trnx_order.h new file mode 100644 index 0000000..fc64a5f --- /dev/null +++ b/cpp/cpp11/trnx_order.h @@ -0,0 +1,75 @@ +#ifndef INCLUDED_TRNX_ORDER +#define INCLUDED_TRNX_ORDER + +#include +#include + +#include + +namespace BloombergLP { +namespace trnx { + +class Order +{ +public: + Order() + { + BAEL_LOG_SET_CATEGORY("ORDER"); + BAEL_LOG_INFO << "Order::Order()" << BAEL_LOG_END; + }; + + Order(const bsl::string& symbol, double price, int amount, + char side) + : d_symbol(symbol), d_price(price), d_amount(amount), + d_side(side) + { + BAEL_LOG_SET_CATEGORY("ORDER"); + BAEL_LOG_INFO << "Order::Order(string, double, int, char)" + << BAEL_LOG_END; + } + + Order(const Order& other) + : d_symbol(other.d_symbol), d_price(other.d_price), + d_amount(other.d_amount), d_side(other.d_side) + { + BAEL_LOG_SET_CATEGORY("ORDER"); + BAEL_LOG_INFO << "Order::Order(const Order&)" << BAEL_LOG_END; + } + + ~Order() + { + BAEL_LOG_SET_CATEGORY("ORDER"); + BAEL_LOG_INFO << "Order::~Order()" << BAEL_LOG_END; + } + + bool operator==(const Order& other) const + { + return (d_symbol == other.d_symbol && + d_price == other.d_price && + d_amount == other.d_amount && d_side == other.d_side); + } + + const bsl::string& symbol() const { return d_symbol; } + double price() const { return d_price; } + int amount() const { return d_amount; } + char side() const { return d_side; } + +private: + bsl::string d_symbol; + double d_price; + int d_amount; + char d_side; + friend bsl::ostream& operator<<(bsl::ostream& os, const Order& o); +}; + +inline bsl::ostream& operator<<(bsl::ostream& os, const Order& o) +{ + os << "[" << o.d_symbol << ',' << o.d_price << ',' << o.d_amount + << ',' << o.d_side << "]"; + return os; +} + +} // close namespace trnx +} // close namespace BloombergLP + +#endif diff --git a/cpp/cpp11/trnx_vector.h b/cpp/cpp11/trnx_vector.h new file mode 100644 index 0000000..8bef70e --- /dev/null +++ b/cpp/cpp11/trnx_vector.h @@ -0,0 +1,224 @@ +#ifndef INCLUDED_TRNX_VECTOR +#define INCLUDED_TRNX_VECTOR + +#include +#include +#include +#include +#include +#include + +namespace trnx { + +template +class vector +{ +public: + typedef T value_type; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef T& reference; + typedef const T& const_reference; + typedef T* pointer; + typedef const T* const_pointer; + +private: + size_type d_size; + size_type d_capacity; + typedef detail::uninitialized buff_type[]; + std::unique_ptr d_buff; + // `uninitialized` represents enough uninitialized storage for an + // instance of `T`. The lifetime of the `T` instance is controlled + // manually via the `.construct` and `.destroy` member functions. + // An initialized instance can be accessed through `.get`. + +public: + typedef detail::iter_impl > iterator; //CHG + typedef detail::iter_impl > const_iterator; //CHG + // `iter_impl` is an iterator type that automatically transforms + // `uninitialized*` into `T*` when dereferencing. + + vector() : d_size(0), d_capacity(0), d_buff(nullptr) + { + // A default-constructed vector is empty and has no allocated buffer. + } + + vector(const vector& rhs) + : d_size(rhs.d_size), + d_capacity(rhs.d_capacity), + + // Allocate enough space for `rhs.d_size` items. + d_buff(std::make_unique(rhs.d_size)) + { + // Copy-construct all of `rhs`'s elements into the current buffer. + for(size_type i = 0; i < d_size; ++i) + { + d_buff[i].construct(rhs.d_buff[i].get()); + } + } + + ~vector() + { + // Destroy all elements in the vector. In the Standard, the order of + // destruction is unspecified. + for(size_type i = 0; i < d_size; ++i) + { + d_buff[i].destroy(); + } + } + + vector& operator=(const vector& rhs); + // The copy-constructor allocates enough memory to store `rhs`'s + // elements in a new buffer, copies over the current elements to the + // new buffer, and finally sets `d_buff` to point to the new buffer. + + size_type size() const { return d_size; } + size_type capacity() const { return d_capacity; } + bool empty() const { return d_size == 0; } + + iterator begin() { return iterator(d_buff.get()); } + iterator begin() const { return iterator(d_buff.get()); } + + iterator end() { return iterator(d_buff.get() + d_size); } + iterator end() const { return iterator(d_buff.get() + d_size); } + + const_iterator cbegin() const { return const_iterator(d_buff.get()); } + const_iterator cend() const { return const_iterator(d_buff.get() + d_size); } + + reference front() { return d_buff[0].get(); } + const_reference front() const { return d_buff[0].get(); } + + reference back() { return d_buff[d_size - 1].get(); } + const_reference back() const { return d_buff[d_size - 1].get(); } + + reference operator[](size_type i) { return d_buff[i].get(); } + const_reference operator[](size_type i) const { return d_buff[i].get(); } + + reference at(size_type i) + { + if(i >= d_size) + { + throw std::out_of_range("Index out of range"); + } + else + { + return d_buff[i].get(); + } + } + + const_reference at(size_type i) const + { + // The following use of `const_cast` is legal and simply prevents code + // repetition with the non-`const` version of `at`. + return const_cast&>(*this).at(i); + } + + void push_back(const T& item); + void push_back(T&& item); + void pop_back(); + void clear(); + void reserve(size_type new_capacity); +}; + +template +void vector::push_back(const T& item) +{ + if(d_size >= d_capacity) + { + const size_type new_capacity = (d_capacity == 0) ? 1 + : d_capacity * 2; + + reserve(new_capacity); + } + + d_buff[d_size++].construct(item); +} + +template +void vector::push_back(T&& item) +{ + if(d_size >= d_capacity) + { + const size_type new_capacity = (d_capacity == 0) ? 1 + : d_capacity * 2; + + reserve(new_capacity); + } + + d_buff[d_size++].construct(std::move(item)); +} + +template +void vector::pop_back() +{ + BSLS_ASSERT(!empty()); + + d_buff[d_size - 1].destroy(); + --d_size; +} + +template +void vector::clear() +{ + // Destroy all existing elements + for(size_type i = 0; i < d_size; ++i) + { + d_buff[i].destroy(); + } + + // Set size to zero, but leave capacity unchanged + d_size = 0; +} + +template +vector& vector::operator=(const vector& rhs) +{ + // Prevent self-assignment + if(&rhs != this) + { + // Destroy all existing elements and set size to zero + clear(); + + // Reserve if necessary + reserve(rhs.d_size); + + // Copy elements from `rhs` + for(size_type i = 0; i < rhs.d_size; ++i) + { + d_buff[i].construct(rhs.d_buff[i].get()); + } + + // Update size + d_size = rhs.d_size; + } + + return *this; +} + +template +void vector::reserve(size_type new_capacity) +{ + // Exit early if there's no need to reserve more memory + if(new_capacity <= d_capacity) { return; } + + // Allocate a new buffer + auto buff = std::make_unique(new_capacity); + + // Copy-construct existing elements into the new buffer and destroy them + // in the old one + for(size_type i = 0; i < d_size; ++i) + { + buff[i].construct(std::move(d_buff[i].get())); + d_buff[i].destroy(); + } + + // Deallocate old buffer, set the owned buffer to `buff` + d_buff = std::move(buff); + + // Update capacity + d_capacity = new_capacity; +} + +} // close namespace trnx + +#endif diff --git a/cpp/cpp11/trnx_vector_impl.h b/cpp/cpp11/trnx_vector_impl.h new file mode 100644 index 0000000..d6a04a7 --- /dev/null +++ b/cpp/cpp11/trnx_vector_impl.h @@ -0,0 +1,106 @@ +#ifndef INCLUDED_TRNX_VECTOR_IMPL +#define INCLUDED_TRNX_VECTOR_IMPL + +#include +#include +#include +#include + +namespace trnx { +namespace detail { + +// This union is not meant to be shown/completely understood from the students. +// +// They should know that `uninitialized` means "enough storage for a `T` +// instance, and that they can manually control the lifetime of that instance +// through the `.construct` and `.destroy` member functions. +// +// It is undefined behavior to: +// * Call `construct` on an already-initialized instance. +// * Call `destroy` on a non-initialized instance. +// * Access the reference returned by `get` on a non-initialized instance. +// +template +union uninitialized +{ +private: + T d_datum; + +public: + using inner_type = T; + + uninitialized() { } + ~uninitialized() { } + + template + void construct(Ts&&... xs) + { + new (&d_datum) T(std::forward(xs)...); + } + + void destroy() + { + d_datum.~T(); + } + + T& get() { return d_datum; } + const T& get() const { return d_datum; } +}; + +// This class is not meant to be shown/completely understood from the students. +// +// It basically unwraps `uninitialized` upon dereference, behaving as any +// other normal random access iterator, transparently to the user. +// +template +class iter_impl +{ +private: + T* d_ptr; + using inner_type = typename T::inner_type; + +public: + using difference_type = std::ptrdiff_t; + using value_type = inner_type; + using pointer = inner_type*; + using reference = inner_type&; + using iterator_category = std::random_access_iterator_tag; + + iter_impl() : d_ptr(nullptr) { } + iter_impl(T* rhs) : d_ptr(rhs) { } + + iter_impl(const iter_impl& rhs) : d_ptr(rhs.d_ptr) { } + iter_impl& operator=(const iter_impl &rhs) { d_ptr = rhs.d_ptr; return *this; } + + iter_impl& operator+=(difference_type rhs) { d_ptr += rhs; return *this; } + iter_impl& operator-=(difference_type rhs) { d_ptr -= rhs; return *this; } + + inner_type& operator*() const { return d_ptr->get(); } + inner_type* operator->() const { return &d_ptr->get(); } + inner_type& operator[](difference_type rhs) const { return d_ptr[rhs]; } + + iter_impl& operator++() { ++d_ptr; return *this; } + iter_impl& operator--() { --d_ptr; return *this; } + + iter_impl operator++(int) { iter_impl tmp(*this); ++d_ptr; return tmp; } + iter_impl operator--(int) { iter_impl tmp(*this); --d_ptr; return tmp; } + + difference_type operator-(const iter_impl& rhs) const { return iter_impl(d_ptr - rhs.ptr); } + + iter_impl operator+(difference_type rhs) const { return iter_impl(d_ptr + rhs); } + iter_impl operator-(difference_type rhs) const { return iter_impl(d_ptr - rhs); } + friend iter_impl operator+(difference_type lhs, const iter_impl& rhs) { return iter_impl(lhs + rhs.d_ptr); } + friend iter_impl operator-(difference_type lhs, const iter_impl& rhs) { return iter_impl(lhs - rhs.d_ptr); } + + bool operator==(const iter_impl& rhs) const { return d_ptr == rhs.d_ptr; } + bool operator!=(const iter_impl& rhs) const { return d_ptr != rhs.d_ptr; } + bool operator>(const iter_impl& rhs) const { return d_ptr > rhs.d_ptr; } + bool operator<(const iter_impl& rhs) const { return d_ptr < rhs.d_ptr; } + bool operator>=(const iter_impl& rhs) const { return d_ptr >= rhs.d_ptr; } + bool operator<=(const iter_impl& rhs) const { return d_ptr <= rhs.d_ptr; } +}; + +} // close namespace detail +} // close namespace trnx + +#endif diff --git a/cpp/different_linkage.cpp b/cpp/different_linkage.cpp new file mode 100644 index 0000000..fc34a38 --- /dev/null +++ b/cpp/different_linkage.cpp @@ -0,0 +1,31 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +*/ +#include +#include + +class Test { +public: + virtual int increment(int num) = 0; + + static int increment(Test& t, int num) {return t.increment(num);} +}; + +class TestImpl : public Test +{ +public: + virtual int increment(int num) { + std::cout << "Test " << num; + return num; + } +}; + +int main ( void ) +{ + TestImpl t; + Test::increment(t, 2); + + return 0; +} + diff --git a/cpp/hidden_method_call.cpp b/cpp/hidden_method_call.cpp new file mode 100644 index 0000000..96324c7 --- /dev/null +++ b/cpp/hidden_method_call.cpp @@ -0,0 +1,64 @@ +/* Check cf5-opt.vim defs. +VIM: let g:lcppflags="-D__USE_XOPEN2K8 -O2 -pthread" +VIM-: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let g:cf5output=0 +*/ +#include +#include +#include + +struct A +{ + A() + {} + ~A() + {} + + virtual void fun() + { + std::cout << "I am a hidden function." << std::endl; + } +}; + +struct B : public A +{ + B() + {} + ~B() + {} + + void fun() + { + std::cout << "I am the visible function." << std::endl; + } +}; + +int main ( void ) +{try{ + + B o; + + o.fun(); + + o.A::fun(); + + typedef A a_type; + o.a_type::fun(); + + + 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/p.cpp b/cpp/p.cpp new file mode 100644 index 0000000..1eb08c6 --- /dev/null +++ b/cpp/p.cpp @@ -0,0 +1,55 @@ +/* Check cf5-opt.vim defs. +VIM: let g:lcppflags="-O2 -pthread -D__USE_XOPEN2K8" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:cppflags="" +VIM: let g:ldflags="" +VIM: let g:ldlibpath="" +VIM: let g:argv="" +VIM-: let g:cf5output=0 +*/ +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +extern "C" { +#endif + + int some_c_function_( int param1 ); + +#ifdef __cplusplus +} +#endif +#ifdef __cplusplus +} +#endif +#ifdef __cplusplus +} +#endif + + +int main ( void ) +{try{ + some_c_function_( 99 ); + if (int i = 1) + std::cout << "lkkk" << i << 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/shared_ptr.cpp b/cpp/shared_ptr.cpp new file mode 100644 index 0000000..df8e22f --- /dev/null +++ b/cpp/shared_ptr.cpp @@ -0,0 +1,34 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:argv="" +*/ +#include +#include +#include + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + + //...... + + 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/shared_ptr_cast.cpp b/cpp/shared_ptr_cast.cpp new file mode 100644 index 0000000..310a607 --- /dev/null +++ b/cpp/shared_ptr_cast.cpp @@ -0,0 +1,23 @@ +/* Check cf5-opt.vim defs. +VIM: let b:lcppflags="-std=c++11 -O2 -pthread" +*/ + +#include + +class A +{ + int i; +}; + +class B : public A +{ + int j; +}; + +int main() +{ + std::shared_ptr a = std::make_shared(); + + std::shared_ptr b = std::static_pointer_cast(a); +} + diff --git a/cpp/stl/istringstream_fail_eof.cpp b/cpp/stl/istringstream_fail_eof.cpp new file mode 100644 index 0000000..483ddeb --- /dev/null +++ b/cpp/stl/istringstream_fail_eof.cpp @@ -0,0 +1,77 @@ +/* Check cf5-opt.vim defs. +-VIM-: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:argv="" +*/ +#include +#include +#include + +void test( const std::string& str_to_parse ) +{ + std::stringstream ss(str_to_parse); + std::string str; + int v; + ss >> str >> v; + + std::cout << "str=\"" << str_to_parse << '"' < +#include +#include + +#include + +using namespace tbb::flow; + +struct square { + int operator()(int v) { return v*v; } +}; + +struct cube { + int operator()(int v) { return v*v*v; } +}; + +class sum { + int &my_sum; +public: + sum( int &s ) : my_sum(s) {} + int operator()( tuple< int, int > v ) { + my_sum += get<0>(v) + get<1>(v); + return my_sum; + } +}; + +int main() +{try{ + int result = 0; + + graph g; + broadcast_node input(g); + function_node squarer( g, unlimited, square() ); + function_node cuber( g, unlimited, cube() ); + join_node< tuple, queueing > join( g ); + function_node,int> + summer( g, serial, sum(result) ); + + make_edge( input, squarer ); + make_edge( input, cuber ); + make_edge( squarer, get<0>( join.input_ports() ) ); + make_edge( cuber, get<1>( join.input_ports() ) ); + make_edge( join, summer ); + + for (int i = 1; i <= 10; ++i) + input.try_put(i); + g.wait_for_all(); + + printf("Final result is %d\n", result); + + 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/tbb/simple_flow_graph.cpp b/cpp/tbb/simple_flow_graph.cpp new file mode 100644 index 0000000..805384a --- /dev/null +++ b/cpp/tbb/simple_flow_graph.cpp @@ -0,0 +1,70 @@ +/* +VIM: let b:lcppflags="-std=c++11 -O2 -pthread" +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 + +#include "tbb/flow_graph.h" + +using namespace tbb::flow; + +typedef std::shared_ptr msg_type; + +struct body { + std::string my_name; + body( const char *name ) : my_name(name) {} + void operator()( msg_type msg ) const { + std::stringstream ss; + ss << my_name << " got " << *msg << std::endl; + std::cout << ss.str(); + } +}; + +int main() +{try{ + graph g; + + broadcast_node start( g ); + continue_node a( g, body("A")); + continue_node b( g, body("B")); + continue_node c( g, body("C")); + continue_node d( g, body("D")); + continue_node e( g, body("E")); + + make_edge( start, a ); + make_edge( start, b ); + make_edge( a, c ); + make_edge( b, c ); + make_edge( c, d ); + make_edge( a, e ); + + for (int i = 0; i < 3; ++i ) { + std::stringstream ss; + ss << i << std::endl; + + start.try_put( std::make_shared( ss.str() ) ); + g.wait_for_all(); + } + + 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/tbb/tbb_unspawned_child.cpp b/cpp/tbb/tbb_unspawned_child.cpp new file mode 100644 index 0000000..0971dd4 --- /dev/null +++ b/cpp/tbb/tbb_unspawned_child.cpp @@ -0,0 +1,97 @@ +/* +VIM: let b:lcppflags="-std=c++11 -O2 -pthread" +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM: let b:cppflags=g:Iboost.g:Itbb +VIM: let b:ldflags=g:Lboost.g:LtbbD +VIM: let b:ldlibpath=g:Bboost.g:Btbb +VIM: let b:argv="" +*/ +#include +#include +#include +#include +#include + +#include + +/* + * Conclusion: + * The first task_sheduler_init is configuring number of threads in + * the thread pool. If the task scheduler initialised implicitly then + * the number of threads is the number of cores. To ensure uncomment + * the commented line below. + */ + +class ChildTask : public tbb::task +{ + tbb::task * execute() + { + std::printf( "ChildTask is running.\n" ); + return nullptr; + } +}; + +class ContTask : public tbb::task +{ + tbb::task * execute() + { + std::printf( "ContTask is running.\n" ); + return nullptr; + } +}; + +tbb::task * t; + +struct RootTask : public tbb::task +{ + tbb::task * execute() + { + std::printf( "RootTask is running.\n" ); + + auto& c = *new( allocate_continuation()) ContTask(); + t = &c; + + tbb::task& ctask1 = *new( c.allocate_child() ) ChildTask(); + tbb::task& ctask2 = *new( c.allocate_child() ) ChildTask(); + c.set_ref_count( 3 ); + spawn( ctask1 ); + spawn( ctask2 ); + + + std::printf( "RootTask is done.\n" ); + + return nullptr; + } +}; + + +int main ( void ) +{try{ + + auto& rtask = *new( tbb::task::allocate_root()) RootTask(); + + tbb::task::enqueue( rtask ); + + std::this_thread::sleep_for( std::chrono::seconds(1) ); + std::printf( "After sleep.\n" ); + + tbb::task::spawn( *new (t->allocate_child()) tbb::empty_task() ); + + std::this_thread::sleep_for( std::chrono::seconds(1) ); + std::printf( "After second sleep.\n" ); + + 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/test_const_cast_string.cpp b/cpp/test_const_cast_string.cpp new file mode 100644 index 0000000..83d082d --- /dev/null +++ b/cpp/test_const_cast_string.cpp @@ -0,0 +1,38 @@ +/* +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 + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + static const std::string str = "initial"; + const_cast(str) = "test"; + std::cout << str << std::endl; + + 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/two_segments_intersect.cpp b/puzzles/interviews/training/two_segments_intersect.cpp new file mode 100644 index 0000000..046789f --- /dev/null +++ b/puzzles/interviews/training/two_segments_intersect.cpp @@ -0,0 +1,22 @@ +CareerCup +Questions + +Forum + +Salaries + +Resume Tips + +RSS + +Sign In + +Microsoft Interview Question for SDE1s +microsoft-interview-questions0 +of 0 votes +0 +Answers +Given n line segments, find if any two segments intersect +http://www.geeksforgeeks.org/given-a-set-of-line-segments-find-if-any-two-segments-intersect/ + +- anonymous December 10, 2017 i \ No newline at end of file diff --git a/py/python_path.py b/py/python_path.py new file mode 100644 index 0000000..72d4b2d --- /dev/null +++ b/py/python_path.py @@ -0,0 +1,6 @@ +#VIM: let b:lcppflags="-std=c++11 -O2 -pthread" + +import sys + +if __name__ == "__main__": + print( "sys.path= {}".format(sys.path))