|
From: <den...@us...> - 2010-09-11 22:48:47
|
Revision: 133
http://dsim.svn.sourceforge.net/dsim/?rev=133&view=rev
Author: denis_arnaud
Date: 2010-09-11 22:48:41 +0000 (Sat, 11 Sep 2010)
Log Message:
-----------
[Test] Added a sample for handling Boost.Intrusive Set, with the find() algorithm, to the Boost.Intrusive examples on StdAir typical use.
Modified Paths:
--------------
trunk/dsim/test/boost/intrusive/BomAbstract.hpp
trunk/dsim/test/boost/intrusive/FlightDate.hpp
trunk/dsim/test/boost/intrusive/bom.cpp
Modified: trunk/dsim/test/boost/intrusive/BomAbstract.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 19:59:37 UTC (rev 132)
+++ trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 22:48:41 UTC (rev 133)
@@ -24,6 +24,11 @@
oStr << idx;
_key = oStr.str();
}
+ /** Get the key. */
+ const std::string& getKey() const {
+ return _key;
+ }
+
protected:
/** Default constructors.
<br>They are kept private, so as to forbid their use (only the
@@ -33,18 +38,26 @@
public:
// Comparison operators
- friend bool operator== (const BomAbstract &a, const BomAbstract &b) {
+ friend bool operator< (const BomAbstract& a, const BomAbstract& b) {
+ return a._key < b._key;
+ }
+
+ friend bool operator> (const BomAbstract& a, const BomAbstract& b) {
+ return a._key > b._key;
+ }
+
+ friend bool operator== (const BomAbstract& a, const BomAbstract& b) {
return a._key == b._key;
}
- friend bool operator!= (const BomAbstract &a, const BomAbstract &b) {
- return a._key != b._key;
- }
+ friend bool operator!= (const BomAbstract& a, const BomAbstract& b) {
+ return a._key != b._key;
+ }
- // The hash function
- friend std::size_t hash_value (const BomAbstract &i) {
- return boost::hash<std::string>() (i._key);
- }
+ // The hash function
+ friend std::size_t hash_value (const BomAbstract& iBom) {
+ return boost::hash<std::string>() (iBom._key);
+ }
public:
// /////////// Display support methods /////////
@@ -56,7 +69,7 @@
@param istream& the input stream. */
virtual void fromStream (std::istream& ioIn) = 0;
- /** Get the serialised version of the Business Object. */
+ /** Get the serialised version of the Business Object. */
virtual std::string toString() const = 0;
protected:
@@ -117,4 +130,27 @@
}
};
+// These compare (STL strings) keys of BOM objects
+template <typename BOM>
+struct StrExpComp {
+ bool operator() (const std::string& iKey, const BOM& iBom) const {
+ return (iKey < iBom.getKey());
+ }
+
+ bool operator() (const BOM& iBom, const std::string& iKey) const {
+ return (iBom.getKey() < iKey);
+ }
+};
+
+template <typename BOM>
+struct StrExpEqual {
+ bool operator() (const std::string& iKey, const BOM& iBom) const {
+ return (iKey == iBom.getKey());
+ }
+
+ bool operator() (const BOM& iBom, const std::string& iKey) const {
+ return (iBom.getKey() == iKey);
+ }
+};
+
#endif // __INTRUSIVE_BOM_BOMABSTRACT_HPP
Modified: trunk/dsim/test/boost/intrusive/FlightDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 19:59:37 UTC (rev 132)
+++ trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 22:48:41 UTC (rev 133)
@@ -7,6 +7,9 @@
// STL
#include <cassert>
#include <string>
+// Boost.Intrusive
+#include <boost/intrusive/list.hpp>
+#include <boost/intrusive/set.hpp>
// Local
#include <test/boost/intrusive/BomAbstract.hpp>
@@ -31,7 +34,8 @@
FlightDate (const FlightDate&) {}
public:
- bi::list_member_hook<> _childHook;
+ bi::list_member_hook<> _childListHook;
+ bi::set_member_hook<> _childSetHook;
public:
// /////////// Display support methods /////////
@@ -52,8 +56,11 @@
/** List of child-type FlightDate objects. */
typedef bi::member_hook <FlightDate, bi::list_member_hook<>,
- &FlightDate::_childHook> FlightDateChildMemberOption;
- typedef bi::list<FlightDate, FlightDateChildMemberOption> FlightDateChildren;
+ &FlightDate::_childListHook> FlightDateListMemberOption;
+ typedef bi::list<FlightDate, FlightDateListMemberOption> FlightDateChildList;
+ typedef bi::member_hook <FlightDate, bi::set_member_hook<>,
+ &FlightDate::_childSetHook> FlightDateSetMemberOption;
+ typedef bi::set<FlightDate, FlightDateSetMemberOption> FlightDateChildSet;
}
#endif // __INTRUSIVE_BOM_FLIGHTDATE_HPP
Modified: trunk/dsim/test/boost/intrusive/bom.cpp
===================================================================
--- trunk/dsim/test/boost/intrusive/bom.cpp 2010-09-11 19:59:37 UTC (rev 132)
+++ trunk/dsim/test/boost/intrusive/bom.cpp 2010-09-11 22:48:41 UTC (rev 133)
@@ -1,5 +1,6 @@
// STL
#include <cassert>
+#include <iostream>
#include <sstream>
#include <string>
#include <vector>
@@ -12,11 +13,23 @@
namespace bi = boost::intrusive;
+// Optimized search functions
+stdair::FlightDate* getFromSet(const std::string& iKey,
+ stdair::FlightDateChildSet& ioFlightDateChildSet) {
+ stdair::FlightDate* oFlightDate_ptr = NULL;
+ stdair::FlightDateChildSet::iterator itFlight =
+ ioFlightDateChildSet.find (iKey, StrExpComp<stdair::FlightDate>());
+ if (itFlight == ioFlightDateChildSet.end()) {
+ return oFlightDate_ptr;
+ }
+ oFlightDate_ptr = &*itFlight;
+ return oFlightDate_ptr;
+}
// /////////////////////////// M A I N /////////////////////////
/** Main.
<br>Run with the following command:
- <tt>make check && ./bom && echo "Success"</tt>
+ <tt>make check && ((./bom && echo "Success") || echo "Failure")</tt>
<br>To run the program with Valgrind, type:
<tt>libtool --mode=execute valgrind --leak-check=full ./bom</tt>
*/
@@ -37,7 +50,8 @@
}
// (Boost) Intrusive container
- stdair::FlightDateChildren lFlightDateChildren;
+ stdair::FlightDateChildList lFlightDateChildList;
+ stdair::FlightDateChildSet lFlightDateChildSet;
// Now insert them in the same order as in vector in the member hook list
for (FlightDateVector_T::iterator itFlight (lFlightDateVector.begin()),
@@ -45,13 +59,22 @@
stdair::FlightDate* lFlightDate_ptr = *itFlight;
assert (lFlightDate_ptr != NULL);
- lFlightDateChildren.push_back (*lFlightDate_ptr);
+ lFlightDateChildList.push_back (*lFlightDate_ptr);
+ lFlightDateChildSet.insert (*lFlightDate_ptr);
}
+ // DEBUG
+ /*
+ std::cout << "Size of the Boost.Intrusive list of FlightDate objects: "
+ << lFlightDateChildList.size() << std::endl;
+ std::cout << "Size of the Boost.Intrusive set of FlightDate objects: "
+ << lFlightDateChildSet.size() << std::endl;
+ */
+
// Now test lists
{
- stdair::FlightDateChildren::iterator mit (lFlightDateChildren.begin()),
- mitend (lFlightDateChildren.end());
+ stdair::FlightDateChildList::iterator mit (lFlightDateChildList.begin()),
+ mitend (lFlightDateChildList.end());
FlightDateVector_T::iterator itFlight (lFlightDateVector.begin()),
itend (lFlightDateVector.end());
@@ -68,16 +91,47 @@
}
// Now, test iterator_to()
- stdair::FlightDateChildren::iterator itChild (lFlightDateChildren.begin());
- for (int idx = 0; idx < 100; ++idx, ++itChild) {
- stdair::FlightDate* lFlightDate_ptr = lFlightDateVector.at(idx);
- assert (lFlightDate_ptr != NULL);
+ {
+ stdair::FlightDateChildList::iterator itChild(lFlightDateChildList.begin());
+ for (int idx = 0; idx < 100; ++idx, ++itChild) {
+ stdair::FlightDate* lFlightDate_ptr = lFlightDateVector.at(idx);
+ assert (lFlightDate_ptr != NULL);
+
+ if (lFlightDateChildList.iterator_to (*lFlightDate_ptr) != itChild ||
+ stdair::FlightDateChildList::s_iterator_to(*lFlightDate_ptr) != itChild) {
+ return 1;
+ }
+ }
+ }
- if (lFlightDateChildren.iterator_to (*lFlightDate_ptr) != itChild ||
- stdair::FlightDateChildren::s_iterator_to(*lFlightDate_ptr) != itChild){
- return 1;
+ // Now, test sets
+ {
+ stdair::FlightDateChildSet::iterator itChild (lFlightDateChildSet.begin()),
+ itChildEnd (lFlightDateChildSet.end());
+ for (; itChild != itChildEnd; ++itChild) {
+ const stdair::FlightDate& lFlightDate = *itChild;
+
+ const std::string& lKey = lFlightDate.getKey();
+ stdair::FlightDate* retrievedFlightDate_ptr =
+ getFromSet (lKey, lFlightDateChildSet);
+
+ // DEBUG
+ /*
+ std::cout << "Key = '" << lKey << "', itFD = "
+ << &lFlightDate << ", retrieved: " << retrievedFlightDate_ptr
+ << std::endl;
+ */
+
+ if (retrievedFlightDate_ptr == NULL ||
+ lFlightDateChildSet.iterator_to (lFlightDate) != itChild ||
+ lFlightDateChildSet.iterator_to (*retrievedFlightDate_ptr) != itChild ||
+ stdair::FlightDateChildSet::s_iterator_to (lFlightDate) != itChild ||
+ stdair::FlightDateChildSet::s_iterator_to(*retrievedFlightDate_ptr) != itChild) {
+ return 1;
+ }
}
}
+
/** Some memory cleaning.
<br>Note: the FlightDate objects cannot be simply deleted (with the
@@ -85,8 +139,12 @@
<br>See also, for more details:
- http://www.boost.org/doc/libs/1_44_0/doc/html/intrusive/usage.html#intrusive.usage.usage_lifetime
- http://www.boost.org/doc/libs/1_44_0/doc/html/intrusive/erasing_and_disposing.html
+ <br>First, clear simply all the Boost.Intrusive containers but one. Then,
+ clear the last Boost.Intrusive container while deleting the corresponding
+ hooked objects.
*/
- lFlightDateChildren.clear_and_dispose (delete_disposer<stdair::FlightDate>());
+ lFlightDateChildSet.clear();
+ lFlightDateChildList.clear_and_dispose(delete_disposer<stdair::FlightDate>());
return 0;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|