diff --git a/qse/include/qse/cmn/Array.hpp b/qse/include/qse/cmn/Array.hpp index d5391cf6..f343d421 100644 --- a/qse/include/qse/cmn/Array.hpp +++ b/qse/include/qse/cmn/Array.hpp @@ -122,6 +122,26 @@ public: } } +#if (__cplusplus >= 201103L) // C++11 + + Array (SelfType&& array): + Mmged(array.getMmgr()), + count(0), capacity(0), buffer(QSE_NULL) + { + if (array.buffer) + { + this->buffer = array.buffer; + this->count = array.count; + this->capacity = array.capacity; + + array.buffer = QSE_NULL; + array.count = 0; + array.capacity = 0; + } + } + +#endif + ~Array () { this->clear (true); @@ -142,6 +162,33 @@ public: return *this; } +#if (__cplusplus >= 201103L) // C++11 + SelfType& operator= (SelfType&& array) + { + if (this != &array) + { + this->clear (true); + + if (array.buffer) + { + // TODO: show i block move if mmgrs are differnt + // between *this and array? + + this->setMmgr (array.getMmgr()); // copy over mmgr. + + this->buffer = array.buffer; + this->count = array.count; + this->capacity = array.capacity; + + array.buffer = QSE_NULL; + array.count = 0; + array.capacity = 0; + } + } + return *this; + } +#endif + protected: T* clone_buffer (const T* srcbuf, qse_size_t capa, qse_size_t cnt) { @@ -157,7 +204,7 @@ protected: { for (index = 0; index < cnt; index++) { - // copy-construct each element. + // copy-construct each element. new((QSE::Mmgr*)QSE_NULL, &tmp[index]) T(srcbuf[index]); this->_positioner (tmp[index], index); } @@ -173,12 +220,61 @@ protected: tmp[index].~T (); } ::operator delete (tmp, this->getMmgr()); + throw; } return tmp; } +#if (__cplusplus >= 201103L) // C++11 + T* clone_buffer_by_moving (T* srcbuf, qse_size_t capa, qse_size_t cnt) + { + QSE_ASSERT (capa > 0); + QSE_ASSERT (cnt <= capa); + + qse_size_t index; + + //T* tmp = new T[capa]; + T* tmp = (T*)::operator new (capa * QSE_SIZEOF(*tmp), this->getMmgr()); + + try + { + for (index = 0; index < cnt; index++) + { + // move-construct(or copy-construct) each element. + new((QSE::Mmgr*)QSE_NULL, &tmp[index]) T((T&&)srcbuf[index]); + this->_positioner (tmp[index], index); + } + } + catch (...) + { + // in case move-constructor(or copy-constructor) raises an exception. + QSE_ASSERT (tmp != QSE_NULL); + while (index > 0) + { + --index; + + // if move-contruction ended up with an exception, + // the original array can get into an unknown state eventually. + // i don't attempt to restore the moved object as an exception + // may be raised during restoration. + // an exception + //try { new((QSE::Mmgr*)QSE_NULL, &srcbuf[index]) T((T&&)tmp[index]); } + //catch (...) {} + + this->_positioner (tmp[index], INVALID_INDEX); + tmp[index].~T (); + } + ::operator delete (tmp, this->getMmgr()); + + throw; + } + + return tmp; + } +#endif + void put_item (qse_size_t index, const T& value) { if (index >= this->count) @@ -190,12 +286,31 @@ protected: } else { - // there is an old value in the position. + // there is an old value in the position. do classic-assignment this->buffer[index] = value; this->_positioner (this->buffer[index], index); } } +#if (__cplusplus >= 201103L) // C++11 + void put_item_by_moving (qse_size_t index, T&& value) + { + if (index >= this->count) + { + // no value exists in the given position. + // i can move-construct the value. + new((QSE::Mmgr*)QSE_NULL, &this->buffer[index]) T((T&&)value); + this->_positioner (this->buffer[index], index); + } + else + { + // there is an old value in the position. do move-assignment. + this->buffer[index] = (T&&)value; + this->_positioner (this->buffer[index], index); + } + } +#endif + void clear_all_items () { QSE_ASSERT (this->count <= 0 || (this->count >= 1 && this->buffer)); @@ -303,14 +418,9 @@ public: this->update (index, value); } - qse_size_t insert (qse_size_t index, const T& value) +protected: + void secure_slot (qse_size_t index) { - // Unlike insert() in RedBlackTree and HashList, - // it inserts an item when index exists in the existing array. - // It is because array allows duplicate items. - // RedBlckTree::insert() and HashList::insert() return failure - // if existing item exists. - if (index >= this->capacity) { // the position to add the element is beyond the @@ -335,8 +445,11 @@ public: // shift the existing elements to the back by one slot. for (qse_size_t i = this->count; i > index; i--) { - //this->buffer[i] = this->buffer[i - 1]; - this->put_item (i, this->buffer[i - 1]); + #if (__cplusplus >= 201103L) // C++11 + this->put_item_by_moving (i, (T&&)this->buffer[i - 1]); + #else + this->put_item (i, this->buffer[i - 1]); + #endif } } else if (index > this->count) @@ -349,6 +462,17 @@ public: this->_positioner (this->buffer[i], i); } } + } + +public: + qse_size_t insert (qse_size_t index, const T& value) + { + // Unlike insert() in RedBlackTree and HashList, + // it inserts an item when index exists in the existing array. + // It is because array allows duplicate items. + // RedBlckTree::insert() and HashList::insert() return failure + // if existing item exists. + this->secure_slot (index); //this->buffer[index] = value; this->put_item (index, value); @@ -358,6 +482,25 @@ public: return index; } +#if (__cplusplus >= 201103L) // C++11 + qse_size_t insert (qse_size_t index, T&& value) + { + // Unlike insert() in RedBlackTree and HashList, + // it inserts an item when index exists in the existing array. + // It is because array allows duplicate items. + // RedBlckTree::insert() and HashList::insert() return failure + // if existing item exists. + this->secure_slot (index); + + //this->buffer[index] = value; + this->put_item_by_moving (index, (T&&)value); + if (index > this->count) this->count = index + 1; + else this->count++; + + return index; + } +#endif + qse_size_t update (qse_size_t index, const T& value) { QSE_ASSERT (index < this->count); @@ -366,6 +509,16 @@ public: return index; } +#if (__cplusplus >= 201103L) // C++11 + qse_size_t update (qse_size_t index, T&& value) + { + QSE_ASSERT (index < this->count); + this->buffer[index] = (T&&)value; + this->_positioner (this->buffer[index], index); + return index; + } +#endif + qse_size_t upsert (qse_size_t index, const T& value) { if (index < this->count) @@ -374,6 +527,16 @@ public: return this->insert (index, value); } +#if (__cplusplus >= 201103L) // C++11 + qse_size_t upsert (qse_size_t index, T&& value) + { + if (index < this->count) + return this->update (index, (T&&)value); + else + return this->insert (index, (T&&)value); + } +#endif + qse_size_t ensert (qse_size_t index, const T& value) { if (index < this->count) @@ -382,15 +545,27 @@ public: return this->insert (index, value); } +#if (__cplusplus >= 201103L) // C++11 + qse_size_t ensert (qse_size_t index, T&& value) + { + if (index < this->count) + return index; // no update + else + return this->insert (index, (T&&)value); + } +#endif + void remove (qse_size_t index) { - this->remove (index, index); + this->remove (index, 1); } - void remove (qse_size_t from_index, qse_size_t to_index) + void remove (qse_size_t from_index, qse_size_t size) { - QSE_ASSERT (from_index < this->count); - QSE_ASSERT (to_index < this->count); + if (size <= 0 || this->count <= 0 || from_index >= this->count) return; + + qse_size_t to_index = from_index + size - 1; + if (to_index >= this->count) to_index = this->count - 1; qse_size_t j = from_index; qse_size_t i = to_index + 1; @@ -409,7 +584,11 @@ public: //this->_positioner (this->buffer[j], j); // 2. operator assignment + #if (__cplusplus >= 201103L) // C++11 + this->buffer[j] = (T&&)this->buffer[i]; + #else this->buffer[j] = this->buffer[i]; + #endif this->_positioner (this->buffer[j], j); j++; i++; @@ -505,7 +684,11 @@ public: qse_size_t cnt = this->count; if (cnt > capa) cnt = capa; + #if (__cplusplus >= 201103L) // C++11 + T* tmp = this->clone_buffer_by_moving (this->buffer, capa, cnt); + #else T* tmp = this->clone_buffer (this->buffer, capa, cnt); + #endif this->clear (true); this->buffer = tmp; this->capacity = capa; @@ -565,7 +748,6 @@ public: } #endif - void rotate (int dir, qse_size_t n) { qse_size_t first, last, cnt, index, nk; diff --git a/qse/include/qse/cmn/BinaryHeap.hpp b/qse/include/qse/cmn/BinaryHeap.hpp index 047f5a7e..3bbb0233 100644 --- a/qse/include/qse/cmn/BinaryHeap.hpp +++ b/qse/include/qse/cmn/BinaryHeap.hpp @@ -135,6 +135,12 @@ public: { } +#if (__cplusplus >= 201103L) // C++11 + BinaryHeap (SelfType& heap): ParentType (heap) + { + } +#endif + ~BinaryHeap () { } @@ -148,6 +154,17 @@ public: return *this; } +#if (__cplusplus >= 201103L) // C++11 + SelfType& operator= (SelfType&& heap) + { + if (this != &heap) + { + ParentType::operator= (heap); + } + return *this; + } +#endif + /// The isEmpty() function returns true if the binary heap contains no /// item and false otherwise. bool isEmpty() const { return ParentType::isEmpty(); } @@ -195,6 +212,19 @@ public: return this->sift_up(index); } +#if (__cplusplus >= 201103L) // C++11 + qse_size_t insert (T&& value) + { + qse_size_t index = this->count; + + // add the item at the back of the array + ParentType::insert (index, (T&&)value); + + // move the item up to the top if it's greater than the up item + return this->sift_up(index); + } +#endif + /// The update() function changes the item at the specified \a index /// to a new item \a value. qse_size_t update (qse_size_t index, const T& value) @@ -206,15 +236,29 @@ public: return (this->greater_than(value, old))? this->sift_up(index): this->sift_down(index); } +#if (__cplusplus >= 201103L) // C++11 + qse_size_t update (qse_size_t index, T&& value) + { + T old = this->buffer[index]; + + ParentType::update (index, (T&&)value); + + return (this->greater_than(value, old))? this->sift_up(index): this->sift_down(index); + } +#endif + /// The remove() function removes an item at the specified \a index. void remove (qse_size_t index) { QSE_ASSERT (index < this->count); +// TODO: move semantics herr +//BEGIN // copy the last item to the position to remove T old = this->buffer[index]; ParentType::update (index, this->buffer[this->count - 1]); +// END.. // delete the last item ParentType::remove (this->count - 1); diff --git a/qse/include/qse/cmn/SharedPtr.hpp b/qse/include/qse/cmn/SharedPtr.hpp index 98b3e895..3db3cb7a 100644 --- a/qse/include/qse/cmn/SharedPtr.hpp +++ b/qse/include/qse/cmn/SharedPtr.hpp @@ -123,7 +123,7 @@ public: // must copy the memory manager pointer as the item // to be copied is allocated using the memory manager of sp. - this->setMmgr (sp.getMmgr()); + this->setMmgr (sp.getMmgr()); // copy over mmgr this->_item = sp._item; this->_item->ref (); diff --git a/qse/samples/cmn/Makefile.am b/qse/samples/cmn/Makefile.am index 9d842ec1..47cb30c0 100644 --- a/qse/samples/cmn/Makefile.am +++ b/qse/samples/cmn/Makefile.am @@ -98,11 +98,14 @@ if ENABLE_CXX #rex02_CXXFLAGS = -I/usr/lib/wx/include/gtk2-unicode-release-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXGTK__ -pthread #rex02_LDFLAGS = -pthread -Wl,-Bsymbolic-functions -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8 -bin_PROGRAMS += arr01 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02 str02 +bin_PROGRAMS += arr01 arr02 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02 str02 arr01_SOURCES = arr01.cpp arr01_LDADD = $(LDADD) -lqsecmnxx +arr02_SOURCES = arr02.cpp +arr02_LDADD = $(LDADD) -lqsecmnxx + bh01_SOURCES = bh01.cpp bh01_LDADD = $(LDADD) -lqsecmnxx diff --git a/qse/samples/cmn/Makefile.in b/qse/samples/cmn/Makefile.in index 4688f0be..099573a6 100644 --- a/qse/samples/cmn/Makefile.in +++ b/qse/samples/cmn/Makefile.in @@ -68,7 +68,7 @@ bin_PROGRAMS = chr01$(EXEEXT) dir01$(EXEEXT) dll$(EXEEXT) \ #rex02_SOURCES = rex02.cpp #rex02_CXXFLAGS = -I/usr/lib/wx/include/gtk2-unicode-release-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXGTK__ -pthread #rex02_LDFLAGS = -pthread -Wl,-Bsymbolic-functions -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8 -@ENABLE_CXX_TRUE@am__append_2 = arr01 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02 str02 +@ENABLE_CXX_TRUE@am__append_2 = arr01 arr02 bh01 hl01 htb02 rbt02 rbt03 sp01 sp02 str02 subdir = samples/cmn DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -86,10 +86,10 @@ mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/include/qse/config.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = -@ENABLE_CXX_TRUE@am__EXEEXT_1 = arr01$(EXEEXT) bh01$(EXEEXT) \ -@ENABLE_CXX_TRUE@ hl01$(EXEEXT) htb02$(EXEEXT) rbt02$(EXEEXT) \ -@ENABLE_CXX_TRUE@ rbt03$(EXEEXT) sp01$(EXEEXT) sp02$(EXEEXT) \ -@ENABLE_CXX_TRUE@ str02$(EXEEXT) +@ENABLE_CXX_TRUE@am__EXEEXT_1 = arr01$(EXEEXT) arr02$(EXEEXT) \ +@ENABLE_CXX_TRUE@ bh01$(EXEEXT) hl01$(EXEEXT) htb02$(EXEEXT) \ +@ENABLE_CXX_TRUE@ rbt02$(EXEEXT) rbt03$(EXEEXT) sp01$(EXEEXT) \ +@ENABLE_CXX_TRUE@ sp02$(EXEEXT) str02$(EXEEXT) am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) am__arr01_SOURCES_DIST = arr01.cpp @@ -102,6 +102,10 @@ am__DEPENDENCIES_3 = $(am__DEPENDENCIES_2) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) am__v_lt_0 = --silent +am__arr02_SOURCES_DIST = arr02.cpp +@ENABLE_CXX_TRUE@am_arr02_OBJECTS = arr02.$(OBJEXT) +arr02_OBJECTS = $(am_arr02_OBJECTS) +@ENABLE_CXX_TRUE@arr02_DEPENDENCIES = $(am__DEPENDENCIES_3) am__bh01_SOURCES_DIST = bh01.cpp @ENABLE_CXX_TRUE@am_bh01_OBJECTS = bh01.$(OBJEXT) bh01_OBJECTS = $(am_bh01_OBJECTS) @@ -336,11 +340,11 @@ am__v_CXXLD_0 = @echo " CXXLD " $@; AM_V_GEN = $(am__v_GEN_@AM_V@) am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) am__v_GEN_0 = @echo " GEN " $@; -SOURCES = $(arr01_SOURCES) $(bh01_SOURCES) $(chr01_SOURCES) dir01.c \ - $(dll_SOURCES) $(env01_SOURCES) $(fio01_SOURCES) \ - $(fio02_SOURCES) $(fma_SOURCES) $(fmt01_SOURCES) \ - $(fmt02_SOURCES) $(fs01_SOURCES) fs02.c fs03.c \ - $(glob01_SOURCES) $(hl01_SOURCES) $(htb01_SOURCES) \ +SOURCES = $(arr01_SOURCES) $(arr02_SOURCES) $(bh01_SOURCES) \ + $(chr01_SOURCES) dir01.c $(dll_SOURCES) $(env01_SOURCES) \ + $(fio01_SOURCES) $(fio02_SOURCES) $(fma_SOURCES) \ + $(fmt01_SOURCES) $(fmt02_SOURCES) $(fs01_SOURCES) fs02.c \ + fs03.c $(glob01_SOURCES) $(hl01_SOURCES) $(htb01_SOURCES) \ $(htb02_SOURCES) $(ipad01_SOURCES) $(lda_SOURCES) \ $(main01_SOURCES) $(main02_SOURCES) $(mbwc01_SOURCES) \ $(mbwc02_SOURCES) $(nwad01_SOURCES) nwif01.c nwif02.c \ @@ -351,16 +355,17 @@ SOURCES = $(arr01_SOURCES) $(bh01_SOURCES) $(chr01_SOURCES) dir01.c \ $(sp01_SOURCES) $(sp02_SOURCES) $(str01_SOURCES) \ $(str02_SOURCES) task01.c $(time_SOURCES) $(tre01_SOURCES) \ uri01.c $(xma_SOURCES) -DIST_SOURCES = $(am__arr01_SOURCES_DIST) $(am__bh01_SOURCES_DIST) \ - $(chr01_SOURCES) dir01.c $(dll_SOURCES) $(env01_SOURCES) \ - $(fio01_SOURCES) $(fio02_SOURCES) $(fma_SOURCES) \ - $(fmt01_SOURCES) $(fmt02_SOURCES) $(fs01_SOURCES) fs02.c \ - fs03.c $(glob01_SOURCES) $(am__hl01_SOURCES_DIST) \ - $(htb01_SOURCES) $(am__htb02_SOURCES_DIST) $(ipad01_SOURCES) \ - $(lda_SOURCES) $(main01_SOURCES) $(main02_SOURCES) \ - $(mbwc01_SOURCES) $(mbwc02_SOURCES) $(nwad01_SOURCES) nwif01.c \ - nwif02.c $(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) \ - $(pma_SOURCES) $(rbt01_SOURCES) $(am__rbt02_SOURCES_DIST) \ +DIST_SOURCES = $(am__arr01_SOURCES_DIST) $(am__arr02_SOURCES_DIST) \ + $(am__bh01_SOURCES_DIST) $(chr01_SOURCES) dir01.c \ + $(dll_SOURCES) $(env01_SOURCES) $(fio01_SOURCES) \ + $(fio02_SOURCES) $(fma_SOURCES) $(fmt01_SOURCES) \ + $(fmt02_SOURCES) $(fs01_SOURCES) fs02.c fs03.c \ + $(glob01_SOURCES) $(am__hl01_SOURCES_DIST) $(htb01_SOURCES) \ + $(am__htb02_SOURCES_DIST) $(ipad01_SOURCES) $(lda_SOURCES) \ + $(main01_SOURCES) $(main02_SOURCES) $(mbwc01_SOURCES) \ + $(mbwc02_SOURCES) $(nwad01_SOURCES) nwif01.c nwif02.c \ + $(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) \ + $(rbt01_SOURCES) $(am__rbt02_SOURCES_DIST) \ $(am__rbt03_SOURCES_DIST) $(rex01_SOURCES) $(sio01_SOURCES) \ $(sio02_SOURCES) $(sio03_SOURCES) $(sll_SOURCES) \ $(slmb01_SOURCES) $(am__sp01_SOURCES_DIST) \ @@ -592,6 +597,8 @@ tre01_SOURCES = tre01.c xma_SOURCES = xma.c @ENABLE_CXX_TRUE@arr01_SOURCES = arr01.cpp @ENABLE_CXX_TRUE@arr01_LDADD = $(LDADD) -lqsecmnxx +@ENABLE_CXX_TRUE@arr02_SOURCES = arr02.cpp +@ENABLE_CXX_TRUE@arr02_LDADD = $(LDADD) -lqsecmnxx @ENABLE_CXX_TRUE@bh01_SOURCES = bh01.cpp @ENABLE_CXX_TRUE@bh01_LDADD = $(LDADD) -lqsecmnxx @ENABLE_CXX_TRUE@hl01_SOURCES = hl01.cpp @@ -691,6 +698,9 @@ clean-binPROGRAMS: arr01$(EXEEXT): $(arr01_OBJECTS) $(arr01_DEPENDENCIES) $(EXTRA_arr01_DEPENDENCIES) @rm -f arr01$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(arr01_OBJECTS) $(arr01_LDADD) $(LIBS) +arr02$(EXEEXT): $(arr02_OBJECTS) $(arr02_DEPENDENCIES) $(EXTRA_arr02_DEPENDENCIES) + @rm -f arr02$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(arr02_OBJECTS) $(arr02_LDADD) $(LIBS) bh01$(EXEEXT): $(bh01_OBJECTS) $(bh01_DEPENDENCIES) $(EXTRA_bh01_DEPENDENCIES) @rm -f bh01$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(bh01_OBJECTS) $(bh01_LDADD) $(LIBS) @@ -843,6 +853,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arr01.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arr02.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bh01.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr01.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dir01.Po@am__quote@ diff --git a/qse/samples/cmn/arr01.cpp b/qse/samples/cmn/arr01.cpp index 1cc619a8..8c47dbaf 100644 --- a/qse/samples/cmn/arr01.cpp +++ b/qse/samples/cmn/arr01.cpp @@ -1,7 +1,8 @@ #include -#include + #include #include +#include /* typedef QSE::Array IntArray; @@ -24,12 +25,12 @@ struct IntClassComparator */ #if 1 -class PosStr: public std::string +class PosStr: public QSE::MbString { public: - PosStr (const char* str = ""): std::string(str), pos((qse_size_t)-1) {}; + PosStr (const char* str = ""): QSE::MbString(str), pos((qse_size_t)-1) {}; - PosStr (const PosStr& ps): std::string (ps), pos(ps.pos) + PosStr (const PosStr& ps): QSE::MbString (ps), pos(ps.pos) { } @@ -50,7 +51,7 @@ public: strcpy (buf, str); } - const char* c_str() const { return this->buf; } + const char* getBuffer() const { return this->buf; } const char* data() const { return this->buf; } char buf[512]; @@ -60,9 +61,9 @@ public: struct cstr_comparator { - int operator() (const char* v1, const std::string& v2) const + int operator() (const char* v1, const QSE::MbString& v2) const { - return strcmp (v1, v2.c_str()); + return strcmp (v1, v2.getBuffer()); } }; @@ -84,16 +85,16 @@ int main () } #if 0 - printf ("%s\n", h.get(3).c_str()); + printf ("%s\n", h.get(3).getBuffer()); h.remove (3, 5); - printf ("%s\n", h.get(3).c_str()); + printf ("%s\n", h.get(3).getBuffer()); h.remove (3, 5); - printf ("%s\n", h.get(3).c_str()); + printf ("%s\n", h.get(3).getBuffer()); #endif printf ("--------------------\n"); for (qse_size_t i = 0; i < h.getSize(); i++) { - printf ("[%s] at [%lu]\n", h[i].c_str(), (unsigned long int)h.getIndex(h[i])); + printf ("[%s] at [%lu]\n", h[i].getBuffer(), (unsigned long int)h.getIndex(h[i])); } printf ("--------------------\n"); @@ -107,7 +108,7 @@ int main () printf ("--------------------\n"); for (qse_size_t i = 0; i < h2.getSize(); i++) { - printf ("[%s] at [%lu]\n", h2[i].c_str(), (unsigned long int)h2.getIndex(h2[i])); + printf ("[%s] at [%lu]\n", h2[i].getBuffer(), (unsigned long int)h2.getIndex(h2[i])); } printf ("--------------------\n"); @@ -117,21 +118,21 @@ int main () h3.insert (21, "mystery!"); for (qse_size_t i = 0; i < h3.getSize(); i++) { - printf ("[%s] at [%lu]\n", h3[i].c_str(), (unsigned long int)h3.getIndex(h3[i])); + printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i])); } printf ("--------------------\n"); h3.setCapacity (6); h3.insert (6, "good?"); h3.rotate (1, h3.getSize() / 2); - printf ("[%s] [%s]\n", h3.getValueAt(5).c_str(), h3.getValueAt(6).c_str()); + printf ("[%s] [%s]\n", h3.getValueAt(5).getBuffer(), h3.getValueAt(6).getBuffer()); printf ("%d\n", (int)h3.getSize()); printf ("--------------------\n"); h3.insert (1, "bad?"); for (qse_size_t i = 0; i < h3.getSize(); i++) { - printf ("[%s] at [%lu]\n", h3[i].c_str(), (unsigned long int)h3.getIndex(h3[i])); + printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i])); } printf ("--------------------\n"); diff --git a/qse/samples/cmn/arr02.cpp b/qse/samples/cmn/arr02.cpp new file mode 100644 index 00000000..ae9c7a5c --- /dev/null +++ b/qse/samples/cmn/arr02.cpp @@ -0,0 +1,93 @@ +#include + +#include +#include +#include + +class Julia +{ +public: + Julia (int q = 0): x(QSE_NULL) + { + this->x = new int (q); +printf ("constructor %d\n", q); + } + + Julia (const Julia& q): x(QSE_NULL) + { + this->x = new int (*q.x); +printf ("copy constructor %d\n", *q.x); + } + +#if (__cplusplus >= 201103L) // C++11 + Julia (Julia&& q) + { +printf ("move constructor %d\n", *q.x); + this->x = q.x; + q.x = QSE_NULL; + + } +#endif + + ~Julia() + { + if (this->x) + { +printf ("deleting %p %d\n", this, *x); + delete this->x; + } + } + + Julia& operator= (const Julia& q) + { + if (this != &q) + { + if (this->x) { delete this->x; this->x = QSE_NULL; } + this->x = new int (*q.x); +printf ("operator= %d\n", *q.x); + } + + return *this; + } + +#if (__cplusplus >= 201103L) // C++11 + Julia& operator= (Julia&& q) + { + if (this != &q) + { + if (this->x) { delete this->x; this->x = QSE_NULL; } +printf ("move operator= %d\n", *q.x); + this->x = q.x; + q.x = QSE_NULL; + } + + return *this; + } +#endif + + int* x; +}; + +int main () +{ + QSE::Array a0; + + for (int i = 0; i < 256; i++) + { + a0.insert (0, Julia(i)); + } + + a0.setCapacity (1024); + a0.insert (512, Julia (512)); + + a0.remove (2, 5); + + a0.update (5, Julia(9999)); +#if (__cplusplus >= 201103L) // C++11 + QSE::Array a1 ((QSE::Array&&)a0); +#else + QSE::Array a1 (a0); +#endif + printf ("OK: %d %d\n", (int)a0.getSize(), (int)a1.getSize()); + return 0; +} diff --git a/qse/samples/cmn/xma.c b/qse/samples/cmn/xma.c index 1a572c41..58668450 100644 --- a/qse/samples/cmn/xma.c +++ b/qse/samples/cmn/xma.c @@ -23,10 +23,10 @@ static int test1 () ptr[1] = qse_xma_alloc (xma, 1000); ptr[2] = qse_xma_alloc (xma, 3000); ptr[3] = qse_xma_alloc (xma, 1000); - //qse_xma_dump (xma, qse_printf); - //qse_xma_free (xma, ptr[0]); - //qse_xma_free (xma, ptr[2]); - //qse_xma_free (xma, ptr[3]); + /*qse_xma_dump (xma, qse_printf); + qse_xma_free (xma, ptr[0]); + qse_xma_free (xma, ptr[2]); + qse_xma_free (xma, ptr[3]); */ qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT); qse_xma_realloc (xma, ptr[0], 500);