diff options
author | Sven Gothel <[email protected]> | 2021-01-09 13:57:08 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-01-09 13:57:08 +0100 |
commit | 967b99d8290fb783be9ae0b62ca53dce272519c8 (patch) | |
tree | 400fd3f7c8817df13e4ae6ff5925429d874d1085 /include | |
parent | b3d989845415392719019871d865fd3e79bede9a (diff) |
cow_darray, darray: Add toString() and free std::ostream << (toString)
Diffstat (limited to 'include')
-rw-r--r-- | include/jau/cow_darray.hpp | 20 | ||||
-rw-r--r-- | include/jau/cow_vector.hpp | 20 | ||||
-rw-r--r-- | include/jau/darray.hpp | 20 |
3 files changed, 60 insertions, 0 deletions
diff --git a/include/jau/cow_darray.hpp b/include/jau/cow_darray.hpp index b4a3acb..7bbf7c4 100644 --- a/include/jau/cow_darray.hpp +++ b/include/jau/cow_darray.hpp @@ -912,8 +912,28 @@ namespace jau { } // else throw away new_store_ref return count; } + + __constexpr_cxx20_ std::string toString() const noexcept { + std::string res("{ " + std::to_string( size() ) + ": "); + int i=0; + jau::for_each_const(*this, [&res, &i](const value_type & e) { + if( 1 < ++i ) { res.append(", "); } + res.append( jau::to_string(e) ); + } ); + res.append(" }"); + return res; + } }; + /**************************************************************************************** + ****************************************************************************************/ + + template<typename Value_type, typename Alloc_type> + std::ostream & operator << (std::ostream &out, const cow_darray<Value_type, Alloc_type> &c) { + out << c.toString(); + return out; + } + } /* namespace jau */ /** \example test_cow_iterator_01.cpp diff --git a/include/jau/cow_vector.hpp b/include/jau/cow_vector.hpp index 0a5dc94..5a55073 100644 --- a/include/jau/cow_vector.hpp +++ b/include/jau/cow_vector.hpp @@ -618,8 +618,28 @@ namespace jau { } // else throw away new_store_ref return count; } + + __constexpr_cxx20_ std::string toString() const noexcept { + std::string res("{ " + std::to_string( size() ) + ": "); + int i=0; + jau::for_each_const(*this, [&res, &i](const value_type & e) { + if( 1 < ++i ) { res.append(", "); } + res.append( jau::to_string(e) ); + } ); + res.append(" }"); + return res; + } }; + /**************************************************************************************** + ****************************************************************************************/ + + template<typename Value_type, typename Alloc_type> + std::ostream & operator << (std::ostream &out, const cow_vector<Value_type, Alloc_type> &c) { + out << c.toString(); + return out; + } + } /* namespace jau */ #endif /* JAU_COW_VECTOR_HPP_ */ diff --git a/include/jau/darray.hpp b/include/jau/darray.hpp index c8dbb8c..e4a11b2 100644 --- a/include/jau/darray.hpp +++ b/include/jau/darray.hpp @@ -1000,7 +1000,27 @@ namespace jau { } return count; } + + __constexpr_cxx20_ std::string toString() const noexcept { + std::string res("{ " + std::to_string( size() ) + ": "); + int i=0; + jau::for_each_const(*this, [&res, &i](const value_type & e) { + if( 1 < ++i ) { res.append(", "); } + res.append( jau::to_string(e) ); + } ); + res.append(" }"); + return res; + } }; + + /**************************************************************************************** + ****************************************************************************************/ + + template<typename Value_type, typename Alloc_type> + std::ostream & operator << (std::ostream &out, const darray<Value_type, Alloc_type> &c) { + out << c.toString(); + return out; + } } /* namespace jau */ #endif /* JAU_DYN_ARRAY_HPP_ */ |