[niepce] std;:map utilities with lambda. And a test.



commit 83c4f7f7651cc655ca44eef475c8e5cdd276bd91
Author: Hubert Figuière <hub figuiere net>
Date:   Fri Jun 7 19:50:35 2013 -0400

    std;:map utilities with lambda. And a test.

 .gitignore                 |    1 +
 src/fwk/base/Makefile.am   |    7 ++++-
 src/fwk/base/map.hpp       |   44 ++++++++++++++++++++++++++---------------
 src/fwk/base/t/testmap.cpp |   46 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 18 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index ed19e7b..eb740fc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@ src/fwk/base/testmoniker
 src/fwk/base/testgeometry
 src/fwk/base/testfractions
 src/fwk/base/testdate
+src/fwk/base/testmap
 src/fwk/utils/test_db
 src/fwk/utils/test_db2
 src/fwk/utils/test_db3
diff --git a/src/fwk/base/Makefile.am b/src/fwk/base/Makefile.am
index 2a6be56..3d40322 100644
--- a/src/fwk/base/Makefile.am
+++ b/src/fwk/base/Makefile.am
@@ -7,9 +7,9 @@ INCLUDES = -I$(top_srcdir)/src \
 noinst_LIBRARIES = libfwkbase.a
 
 
-TESTS = testmoniker testgeometry testfractions testdate
+TESTS = testmoniker testgeometry testfractions testdate testmap
 
-check_PROGRAMS = testmoniker testgeometry testfractions testdate
+check_PROGRAMS = testmoniker testgeometry testfractions testdate testmap
 
 testdate_SOURCES = t/testdate.cpp
 testdate_LDADD = libfwkbase.a \
@@ -27,6 +27,9 @@ testfractions_SOURCES = t/testfractions.cpp
 testfractions_LDADD = libfwkbase.a \
        @LIBGLIBMM_LIBS@
 
+testmap_SOURCES = t/testmap.cpp
+testmap_LDADD = libfwkbase.a \
+       @LIBGLIBMM_LIBS@
 
 libfwkbase_a_SOURCES = colour.hpp colour.cpp \
        autoflag.hpp \
diff --git a/src/fwk/base/map.hpp b/src/fwk/base/map.hpp
index c4f54cf..d82840c 100644
--- a/src/fwk/base/map.hpp
+++ b/src/fwk/base/map.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2013 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -27,7 +27,7 @@
 #ifndef __FWK_MAP_HPP_
 #define __FWK_MAP_HPP_
 
-#include <list>
+#include <algorithm>
 #include <vector>
 #include <map>
 
@@ -35,25 +35,27 @@ namespace fwk {
 
   /** get all the keys from the map. */
   template <typename _Map>
-  void map_get_keys(const _Map & m, std::vector<typename _Map::mapped_type> & l) 
+  void map_get_keys(const _Map & m, std::vector<typename _Map::key_type> & l)
   {
     l.clear();
-    for(typename _Map::const_iterator iter = m.begin();
-        iter != m.end(); ++iter) {
-      l.push_back(iter->first);
-    }
+    std::for_each(m.begin(), m.end(),
+                  [&l] (const typename _Map::value_type & p) {
+                      l.push_back(p.first);
+                  }
+        );
   }
 
 
   /** get all the mapped elements from the map. */
   template <typename _Map>
-  void map_get_values(const _Map & m, std::list<typename _Map::mapped_type> & l) 
+  void map_get_values(const _Map & m, std::vector<typename _Map::mapped_type> & l)
   {
     l.clear();
-    for(typename _Map::const_iterator iter = m.begin();
-        iter != m.end(); ++iter) {
-      l.push_back(iter->second);
-    }
+    std::for_each(m.begin(), m.end(),
+                  [&l] (const typename _Map::value_type & p) {
+                      l.push_back(p.second);
+                  }
+        );
   }
 
 
@@ -61,13 +63,23 @@ namespace fwk {
   template <typename _Map>
   void map_delete_all_second(const _Map & m)
   {
-    for(typename _Map::const_iterator iter = m.begin();
-        iter != m.end(); ++iter) {
-      delete iter->second;
-    }    
+    std::for_each(m.begin(), m.end(),
+                  [] (const typename _Map::value_type & p) {
+                      delete p.second;
+                  }
+        );
   }
 
 }
 
 
 #endif
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
diff --git a/src/fwk/base/t/testmap.cpp b/src/fwk/base/t/testmap.cpp
new file mode 100644
index 0000000..349593b
--- /dev/null
+++ b/src/fwk/base/t/testmap.cpp
@@ -0,0 +1,46 @@
+/*
+ * niepce - base/t/testmap.cpp
+ *
+ * Copyright (C) 2013 Hubert Figuiere
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+/** @brief unit test for fractions */
+
+#include <boost/test/minimal.hpp>
+
+#include <stdlib.h>
+#include <map>
+#include "fwk/base/map.hpp"
+
+int test_main( int, char *[] )             // note the name!
+{
+
+       std::map<std::string, int> n { { "one", 1 }, { "two", 2 } };
+
+       BOOST_CHECK(n.size() == 2);
+
+       std::vector<std::string> keys;
+       fwk::map_get_keys(n, keys);
+       BOOST_CHECK(n.size() == keys.size());
+       BOOST_CHECK(keys[0] == "one");
+
+       std::vector<int> values;
+       fwk::map_get_values(n, values);
+       BOOST_CHECK(n.size() == values.size());
+       BOOST_CHECK(values[0] == 1);
+
+       return 0;
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]