changeset 255:8196fdb0fc48

TreeNode: * add clear() to remove all * add removeSame() to remove by testing with operator==
author David Demelier <markand@malikania.fr>
date Thu, 02 Oct 2014 17:14:51 +0200
parents 812dd806f803
children 0080762c8983
files C++/Tests/TreeNode/main.cpp C++/TreeNode.h
diffstat 2 files changed, 57 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Tests/TreeNode/main.cpp	Thu Oct 02 14:10:28 2014 +0200
+++ b/C++/Tests/TreeNode/main.cpp	Thu Oct 02 17:14:51 2014 +0200
@@ -1244,6 +1244,18 @@
  * Remove functions
  * -------------------------------------------------------- */
 
+TEST(Remove, clear)
+{
+	Object root("root");
+
+	root.append(Object("a"));
+	root.append(Object("b"));
+	root.append(Object("c"));
+	root.clear();
+
+	ASSERT_EQ(0, root.countChildren());
+}
+
 TEST(Remove, index)
 {
 	Object root("root");
@@ -1272,6 +1284,20 @@
 	ASSERT_EQ("b", root[1].name());
 }
 
+TEST(Remove, same)
+{
+	Object root("root");
+
+	root.append(Object("a"));
+	root.append(Object("b"));
+	root.append(Object("c"));
+	root.removeSame(Object("c"));
+
+	ASSERT_EQ(2, root.countChildren());
+	ASSERT_EQ("a", root[0].name());
+	ASSERT_EQ("b", root[1].name());
+}
+
 /* --------------------------------------------------------
  * Miscellaneous
  * -------------------------------------------------------- */
--- a/C++/TreeNode.h	Thu Oct 02 14:10:28 2014 +0200
+++ b/C++/TreeNode.h	Thu Oct 02 17:14:51 2014 +0200
@@ -308,20 +308,25 @@
 	}
 
 	/**
-	 * Find the index of a node that is equality comparable to value but may be not in the node.
+	 * Remove a child from the node, the value is tested using operator== and therefore may not exist in the container.
 	 *
-	 * @param value the value to compare
-	 * @return the index or -1 if not found
-	 * @see indexOf
+	 * @param value the value that can be compared
+	 * @warn the removed object must not be used after the call
 	 */
 	template <typename Value>
-	int indexOfSame(const Value &value, typename std::enable_if<TypeTraits<Value>::equalityComparable>::type * = nullptr)
+	void removeSame(const Value &value, typename std::enable_if<TypeTraits<Value>::equalityComparable>::type * = nullptr)
 	{
-		for (unsigned i = 0; i < m_children.size(); ++i)
-			if (*m_children[i] == value)
-				return i;
+		m_children.erase(std::remove_if(m_children.begin(), m_children.end(), [&] (auto &p) {
+			return *p == value;
+		}), m_children.end());
+	}
 
-		return -1;
+	/**
+	 * Remove all children.
+	 */
+	void clear()
+	{
+		m_children.clear();
 	}
 
 	/**
@@ -341,6 +346,23 @@
 	}
 
 	/**
+	 * Find the index of a node that is equality comparable to value but may be not in the node.
+	 *
+	 * @param value the value to compare
+	 * @return the index or -1 if not found
+	 * @see indexOf
+	 */
+	template <typename Value>
+	int indexOfSame(const Value &value, typename std::enable_if<TypeTraits<Value>::equalityComparable>::type * = nullptr)
+	{
+		for (unsigned i = 0; i < m_children.size(); ++i)
+			if (*m_children[i] == value)
+				return i;
+
+		return -1;
+	}
+
+	/**
 	 * Access a child.
 	 *
 	 * @param index the index