From 0f18b430fb39d44e126f9f981462c47fca7b8dba Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Wed, 21 Nov 2018 19:19:15 -0500 Subject: [PATCH 01/13] Add unordered_{set|map} and set to Java Add test suite Add to makefile Revert set change Fix java map test Fix some of the tests Fix unordered map test --- Examples/test-suite/java/Makefile.in | 4 + .../test-suite/java/li_std_map_runme.java | 101 ++++++++++++++++++ .../test-suite/java/li_std_set_runme.java | 40 +++++++ .../java/li_std_unordered_map_runme.java | 101 ++++++++++++++++++ .../java/li_std_unordered_set_runme.java | 40 +++++++ Examples/test-suite/li_std_set.i | 25 ++--- Examples/test-suite/li_std_unordered_map.i | 12 +++ Examples/test-suite/li_std_unordered_set.i | 11 ++ Lib/java/std_set.i | 47 ++++++++ Lib/java/std_unordered_map.i | 65 +++++++++++ Lib/java/std_unordered_set.i | 47 ++++++++ 11 files changed, 481 insertions(+), 12 deletions(-) create mode 100644 Examples/test-suite/java/li_std_map_runme.java create mode 100644 Examples/test-suite/java/li_std_set_runme.java create mode 100644 Examples/test-suite/java/li_std_unordered_map_runme.java create mode 100644 Examples/test-suite/java/li_std_unordered_set_runme.java create mode 100644 Examples/test-suite/li_std_unordered_map.i create mode 100644 Examples/test-suite/li_std_unordered_set.i create mode 100644 Lib/java/std_set.i create mode 100644 Lib/java/std_unordered_map.i create mode 100644 Lib/java/std_unordered_set.i diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index 8a98172ed..acb0ef2e3 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -44,6 +44,10 @@ CPP_TEST_CASES = \ java_typemaps_proxy \ java_typemaps_typewrapper \ li_std_list \ + li_std_map \ + li_std_set \ + li_std_unordered_map \ + li_std_unordered_set \ # li_boost_intrusive_ptr CPP11_TEST_CASES = \ diff --git a/Examples/test-suite/java/li_std_map_runme.java b/Examples/test-suite/java/li_std_map_runme.java new file mode 100644 index 000000000..3cc794ced --- /dev/null +++ b/Examples/test-suite/java/li_std_map_runme.java @@ -0,0 +1,101 @@ +import li_std_map.*; + +public class li_std_map_runme { + + static { + try { + System.loadLibrary("li_std_map"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringIntMap sim = new StringIntMap(); + IntIntMap iim = new IntIntMap(); + + if (!sim.empty()) throw new RuntimeException("Test (1) failed"); + if (!iim.empty()) throw new RuntimeException("Test (2) failed"); + + if (sim.size() != 0) throw new RuntimeException("Test (3) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (4) failed"); + + try { + sim.get("key"); + throw new RuntimeException("Test (5) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.get(1); + throw new RuntimeException("Test (6) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); + + sim.del("key"); + iim.del(1); + + if (sim.has_key("key")) throw new RuntimeException("Test (9) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (10) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (11) failed"); + if (!iim.empty()) throw new RuntimeException("Test (12) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); + + try { + sim.del("key"); + throw new RuntimeException("Test (15) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.del(1); + throw new RuntimeException("Test (16) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); + + sim.clear(); + iim.clear(); + + if (sim.has_key("key")) throw new RuntimeException("Test (19) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (20) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (21) failed"); + if (!iim.empty()) throw new RuntimeException("Test (22) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); + + sim.set("key", 1); + sim.set("key2", 2); + iim.set(1, 1); + iim.set(2, 2); + + if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); + if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); + + sim.set("key", 3); + iim.set(1, 3); + + if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); + if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); + } +} diff --git a/Examples/test-suite/java/li_std_set_runme.java b/Examples/test-suite/java/li_std_set_runme.java new file mode 100644 index 000000000..61edd93dc --- /dev/null +++ b/Examples/test-suite/java/li_std_set_runme.java @@ -0,0 +1,40 @@ +import li_std_set.*; + +public class li_std_set_runme { + + static { + try { + System.loadLibrary("li_std_set"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringSet ss = new StringSet(); + + if (!ss.empty()) throw new RuntimeException("Test (1) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (2) failed"); + if (ss.has("key")) throw new RuntimeException("Test (3) failed"); + if (ss.erase("key")) throw new RuntimeException("Test (4) failed"); + + if (!ss.insert("key")) throw new RuntimeException("Test (5) failed"); + if (ss.insert("key")) throw new RuntimeException("Test (6) failed"); + if (!ss.has("key")) throw new RuntimeException("Test (7) failed"); + + if (!ss.erase("key")) throw new RuntimeException("Test (8) failed"); + if (!ss.empty()) throw new RuntimeException("Test (9) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (10) failed"); + + if (!ss.insert("key1")) throw new RuntimeException("Test (11) failed"); + if (!ss.insert("key2")) throw new RuntimeException("Test (12) failed"); + if (!ss.insert("key3")) throw new RuntimeException("Test (13) failed"); + if (ss.size() != 3) throw new RuntimeException("Test (14) failed"); + + ss.clear(); + if (!ss.empty()) throw new RuntimeException("Test (15) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (16) failed"); + } +} diff --git a/Examples/test-suite/java/li_std_unordered_map_runme.java b/Examples/test-suite/java/li_std_unordered_map_runme.java new file mode 100644 index 000000000..8ab6f23e4 --- /dev/null +++ b/Examples/test-suite/java/li_std_unordered_map_runme.java @@ -0,0 +1,101 @@ +import li_std_unordered_map.*; + +public class li_std_unordered_map_runme { + + static { + try { + System.loadLibrary("li_std_unordered_map"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringIntUnorderedMap sim = new StringIntUnorderedMap(); + IntIntUnorderedMap iim = new IntIntUnorderedMap(); + + if (!sim.empty()) throw new RuntimeException("Test (1) failed"); + if (!iim.empty()) throw new RuntimeException("Test (2) failed"); + + if (sim.size() != 0) throw new RuntimeException("Test (3) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (4) failed"); + + try { + sim.get("key"); + throw new RuntimeException("Test (5) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.get(1); + throw new RuntimeException("Test (6) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); + + sim.del("key"); + iim.del(1); + + if (sim.has_key("key")) throw new RuntimeException("Test (9) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (10) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (11) failed"); + if (!iim.empty()) throw new RuntimeException("Test (12) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); + + try { + sim.del("key"); + throw new RuntimeException("Test (15) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.del(1); + throw new RuntimeException("Test (16) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); + + sim.clear(); + iim.clear(); + + if (sim.has_key("key")) throw new RuntimeException("Test (19) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (20) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (21) failed"); + if (!iim.empty()) throw new RuntimeException("Test (22) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); + + sim.set("key", 1); + sim.set("key2", 2); + iim.set(1, 1); + iim.set(2, 2); + + if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); + if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); + + sim.set("key", 3); + iim.set(1, 3); + + if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); + if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); + } +} diff --git a/Examples/test-suite/java/li_std_unordered_set_runme.java b/Examples/test-suite/java/li_std_unordered_set_runme.java new file mode 100644 index 000000000..ce94f8fe4 --- /dev/null +++ b/Examples/test-suite/java/li_std_unordered_set_runme.java @@ -0,0 +1,40 @@ +import li_std_unordered_set.*; + +public class li_std_unordered_set_runme { + + static { + try { + System.loadLibrary("li_std_unordered_set"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringUnorderedSet ss = new StringUnorderedSet(); + + if (!ss.empty()) throw new RuntimeException("Test (1) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (2) failed"); + if (ss.has("key")) throw new RuntimeException("Test (3) failed"); + if (ss.erase("key")) throw new RuntimeException("Test (4) failed"); + + if (!ss.insert("key")) throw new RuntimeException("Test (5) failed"); + if (ss.insert("key")) throw new RuntimeException("Test (6) failed"); + if (!ss.has("key")) throw new RuntimeException("Test (7) failed"); + + if (!ss.erase("key")) throw new RuntimeException("Test (8) failed"); + if (!ss.empty()) throw new RuntimeException("Test (9) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (10) failed"); + + if (!ss.insert("key1")) throw new RuntimeException("Test (11) failed"); + if (!ss.insert("key2")) throw new RuntimeException("Test (12) failed"); + if (!ss.insert("key3")) throw new RuntimeException("Test (13) failed"); + if (ss.size() != 3) throw new RuntimeException("Test (14) failed"); + + ss.clear(); + if (!ss.empty()) throw new RuntimeException("Test (15) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (16) failed"); + } +} diff --git a/Examples/test-suite/li_std_set.i b/Examples/test-suite/li_std_set.i index 2dcc2f17c..fc9db42a9 100644 --- a/Examples/test-suite/li_std_set.i +++ b/Examples/test-suite/li_std_set.i @@ -1,5 +1,5 @@ /* - * a test of set containers. + * a test of set containers. * Languages should define swig::LANGUAGE_OBJ to be * an entity of their native pointer type which can be * included in a STL container. @@ -13,22 +13,23 @@ %include %include -%include %include -%template(set_string) std::set; -%template(set_int) std::multiset; - - -%template(v_int) std::vector; - - - +// Use language macros since Java doesn't have multiset support (yet) +// and uses different naming conventions. +#if defined(SWIGRUBY) || defined(SWIGPYTHON) + %include + %template(set_int) std::multiset; + %template(v_int) std::vector; + %template(set_string) std::set; +#elif defined(SWIGJAVA) + %template(StringSet) std::set; +#endif #if defined(SWIGRUBY) -%template(LanguageSet) std::set; +%template(LanguageSet) std::set; #endif #if defined(SWIGPYTHON) -%template(pyset) std::set; +%template(pyset) std::set; #endif diff --git a/Examples/test-suite/li_std_unordered_map.i b/Examples/test-suite/li_std_unordered_map.i new file mode 100644 index 000000000..a6869cc0b --- /dev/null +++ b/Examples/test-suite/li_std_unordered_map.i @@ -0,0 +1,12 @@ +/* + * A test of unordered_map containers. + */ + +%module li_std_unordered_map + +%include +%include + +%template(IntIntUnorderedMap) std::unordered_map; +%template(StringIntUnorderedMap) std::unordered_map; + diff --git a/Examples/test-suite/li_std_unordered_set.i b/Examples/test-suite/li_std_unordered_set.i new file mode 100644 index 000000000..a051e9389 --- /dev/null +++ b/Examples/test-suite/li_std_unordered_set.i @@ -0,0 +1,11 @@ +/* + * A test of unordered_set containers. + */ + +%module li_std_unordered_set + +%include +%include + +%template(StringUnorderedSet) std::unordered_set; + diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i new file mode 100644 index 000000000..8c4bb7f17 --- /dev/null +++ b/Lib/java/std_set.i @@ -0,0 +1,47 @@ +/* ----------------------------------------------------------------------------- + * std_set.i + * + * SWIG typemaps for std::set + * ----------------------------------------------------------------------------- */ + +%include + +// ------------------------------------------------------------------------ +// std::set +// ------------------------------------------------------------------------ + +%{ +#include +%} + +namespace std { + +template +class set { + public: + set(); + set(const set&); + + unsigned int size() const; + bool empty() const; + void clear(); + + %extend { + // Returns whether item was inserted. + bool insert(const KeyType& key) { + return self->insert(key).second; + } + + // Returns whether set contains key. + bool has(const KeyType& key) { + return (self->count(key) > 0); + } + + // Returns whether the item was erased. + bool erase(const KeyType& key) { + return (self->erase(key) > 0); + } + } +}; + +} // namespace std \ No newline at end of file diff --git a/Lib/java/std_unordered_map.i b/Lib/java/std_unordered_map.i new file mode 100644 index 000000000..434292b07 --- /dev/null +++ b/Lib/java/std_unordered_map.i @@ -0,0 +1,65 @@ +/* ----------------------------------------------------------------------------- + * std_unordered_map.i + * + * SWIG typemaps for std::unordered_map + * ----------------------------------------------------------------------------- */ + +%include + +// ------------------------------------------------------------------------ +// std::unordered_map +// ------------------------------------------------------------------------ + +%{ +#include +#include +%} + +namespace std { + +template class unordered_map { + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef KeyType key_type; + typedef MappedType mapped_type; + unordered_map(); + unordered_map(const unordered_map&); + + unsigned int size() const; + bool empty() const; + void clear(); + + %extend { + const MappedType& get(const KeyType& key) throw (std::out_of_range) { + std::unordered_map::iterator i = self->find(key); + + if (i != self->end()) { + return i->second; + } else { + throw std::out_of_range("key not found"); + } + } + + void set(const KeyType& key, const MappedType& x) { + (*self)[key] = x; + } + + void del(const KeyType& key) throw (std::out_of_range) { + std::unordered_map::iterator i = self->find(key); + + if (i != self->end()) { + self->erase(i); + } else { + throw std::out_of_range("key not found"); + } + } + + bool has_key(const KeyType& key) { + std::unordered_map::iterator i = self->find(key); + return i != self->end(); + } + } +}; + +} // namespace std diff --git a/Lib/java/std_unordered_set.i b/Lib/java/std_unordered_set.i new file mode 100644 index 000000000..a9fac0f82 --- /dev/null +++ b/Lib/java/std_unordered_set.i @@ -0,0 +1,47 @@ +/* ----------------------------------------------------------------------------- + * std_unordered_set.i + * + * SWIG typemaps for std::unordered_set + * ----------------------------------------------------------------------------- */ + +%include + +// ------------------------------------------------------------------------ +// std::unordered_set +// ------------------------------------------------------------------------ + +%{ +#include +%} + +namespace std { + +template +class unordered_set { + public: + unordered_set(); + unordered_set(const unordered_set&); + + unsigned int size() const; + bool empty() const; + void clear(); + + %extend { + // Returns whether item was inserted. + bool insert(const KeyType& key) { + return self->insert(key).second; + } + + // Returns whether set contains key. + bool has(const KeyType& key) { + return (self->count(key) > 0); + } + + // Returns whether the item was erased. + bool erase(const KeyType& key) { + return (self->erase(key) > 0); + } + } +}; + +} // namespace std \ No newline at end of file From 4fe9406fae90fd01463d3199184fd53112a3f0e3 Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Sat, 24 Nov 2018 10:00:23 -0500 Subject: [PATCH 02/13] Add test cases to C++11 list --- Examples/test-suite/java/Makefile.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index acb0ef2e3..b5b1d65c7 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -46,8 +46,6 @@ CPP_TEST_CASES = \ li_std_list \ li_std_map \ li_std_set \ - li_std_unordered_map \ - li_std_unordered_set \ # li_boost_intrusive_ptr CPP11_TEST_CASES = \ @@ -56,6 +54,8 @@ CPP11_TEST_CASES = \ cpp11_shared_ptr_overload \ cpp11_shared_ptr_upcast \ cpp11_strongly_typed_enumerations_simple \ + li_std_unordered_map \ + li_std_unordered_set \ DOXYGEN_TEST_CASES := \ doxygen_parsing_enums_simple \ From b1697d7772e2b96367b0a73a756106ed96295190 Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Wed, 28 Nov 2018 16:51:53 -0500 Subject: [PATCH 03/13] Move unordered containers under cpp11_ prefix --- ...{li_std_unordered_map.i => cpp11_li_std_unordered_map.i} | 3 +-- ...{li_std_unordered_set.i => cpp11_li_std_unordered_set.i} | 3 +-- Examples/test-suite/java/Makefile.in | 4 ++-- ...map_runme.java => cpp11_li_std_unordered_map_runme.java} | 6 +++--- ...set_runme.java => cpp11_li_std_unordered_set_runme.java} | 6 +++--- 5 files changed, 10 insertions(+), 12 deletions(-) rename Examples/test-suite/{li_std_unordered_map.i => cpp11_li_std_unordered_map.i} (87%) rename Examples/test-suite/{li_std_unordered_set.i => cpp11_li_std_unordered_set.i} (82%) rename Examples/test-suite/java/{li_std_unordered_map_runme.java => cpp11_li_std_unordered_map_runme.java} (95%) rename Examples/test-suite/java/{li_std_unordered_set_runme.java => cpp11_li_std_unordered_set_runme.java} (91%) diff --git a/Examples/test-suite/li_std_unordered_map.i b/Examples/test-suite/cpp11_li_std_unordered_map.i similarity index 87% rename from Examples/test-suite/li_std_unordered_map.i rename to Examples/test-suite/cpp11_li_std_unordered_map.i index a6869cc0b..5f40e76fd 100644 --- a/Examples/test-suite/li_std_unordered_map.i +++ b/Examples/test-suite/cpp11_li_std_unordered_map.i @@ -2,11 +2,10 @@ * A test of unordered_map containers. */ -%module li_std_unordered_map +%module cpp11_li_std_unordered_map %include %include %template(IntIntUnorderedMap) std::unordered_map; %template(StringIntUnorderedMap) std::unordered_map; - diff --git a/Examples/test-suite/li_std_unordered_set.i b/Examples/test-suite/cpp11_li_std_unordered_set.i similarity index 82% rename from Examples/test-suite/li_std_unordered_set.i rename to Examples/test-suite/cpp11_li_std_unordered_set.i index a051e9389..e9711ce60 100644 --- a/Examples/test-suite/li_std_unordered_set.i +++ b/Examples/test-suite/cpp11_li_std_unordered_set.i @@ -2,10 +2,9 @@ * A test of unordered_set containers. */ -%module li_std_unordered_set +%module cpp11_li_std_unordered_set %include %include %template(StringUnorderedSet) std::unordered_set; - diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index b5b1d65c7..3323f8a9c 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -54,8 +54,8 @@ CPP11_TEST_CASES = \ cpp11_shared_ptr_overload \ cpp11_shared_ptr_upcast \ cpp11_strongly_typed_enumerations_simple \ - li_std_unordered_map \ - li_std_unordered_set \ + cpp11_li_std_unordered_map \ + cpp11_li_std_unordered_set \ DOXYGEN_TEST_CASES := \ doxygen_parsing_enums_simple \ diff --git a/Examples/test-suite/java/li_std_unordered_map_runme.java b/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java similarity index 95% rename from Examples/test-suite/java/li_std_unordered_map_runme.java rename to Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java index 8ab6f23e4..f216a0131 100644 --- a/Examples/test-suite/java/li_std_unordered_map_runme.java +++ b/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java @@ -1,10 +1,10 @@ -import li_std_unordered_map.*; +import cpp11_li_std_unordered_map.*; -public class li_std_unordered_map_runme { +public class cpp11_li_std_unordered_map_runme { static { try { - System.loadLibrary("li_std_unordered_map"); + System.loadLibrary("cpp11_li_std_unordered_map"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); System.exit(1); diff --git a/Examples/test-suite/java/li_std_unordered_set_runme.java b/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java similarity index 91% rename from Examples/test-suite/java/li_std_unordered_set_runme.java rename to Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java index ce94f8fe4..ce33780f0 100644 --- a/Examples/test-suite/java/li_std_unordered_set_runme.java +++ b/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java @@ -1,10 +1,10 @@ -import li_std_unordered_set.*; +import cpp11_li_std_unordered_set.*; -public class li_std_unordered_set_runme { +public class cpp11_li_std_unordered_set_runme { static { try { - System.loadLibrary("li_std_unordered_set"); + System.loadLibrary("cpp11_li_std_unordered_set"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); System.exit(1); From 6ccc940a143406588f29d896fd0eb2f8113e0195 Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Fri, 30 Nov 2018 18:21:02 -0500 Subject: [PATCH 04/13] Add set/unordered_set that extend AbstractSet --- .../cpp11_li_std_unordered_set_runme.java | 72 ++++++--- .../test-suite/java/li_std_set_runme.java | 72 ++++++--- Lib/java/std_set.i | 150 +++++++++++++++++- Lib/java/std_unordered_set.i | 150 +++++++++++++++++- 4 files changed, 400 insertions(+), 44 deletions(-) diff --git a/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java b/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java index ce33780f0..15f6eba0a 100644 --- a/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java +++ b/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java @@ -11,30 +11,66 @@ public class cpp11_li_std_unordered_set_runme { } } + public static void failTest(int testNum) throws Throwable { + throw new RuntimeException("Test failed: " + testNum); + } + + public static void checkThat(boolean mustBeTrue, int testNum) throws Throwable { + if (!mustBeTrue) failTest(testNum); + } + public static void main(String argv[]) throws Throwable { - StringUnorderedSet ss = new StringUnorderedSet(); + java.util.AbstractSet ss = new StringUnorderedSet(); - if (!ss.empty()) throw new RuntimeException("Test (1) failed"); - if (ss.size() != 0) throw new RuntimeException("Test (2) failed"); - if (ss.has("key")) throw new RuntimeException("Test (3) failed"); - if (ss.erase("key")) throw new RuntimeException("Test (4) failed"); + checkThat(ss.isEmpty(), 1); + checkThat(!ss.contains("key"), 2); + checkThat(!ss.remove("key"), 3); - if (!ss.insert("key")) throw new RuntimeException("Test (5) failed"); - if (ss.insert("key")) throw new RuntimeException("Test (6) failed"); - if (!ss.has("key")) throw new RuntimeException("Test (7) failed"); + checkThat(ss.add("key"), 4); + checkThat(!ss.add("key"), 5); + checkThat(ss.contains("key"), 6); + checkThat(ss.remove("key"), 7); + checkThat(ss.isEmpty(), 8); + checkThat(ss.size() == 0, 9); - if (!ss.erase("key")) throw new RuntimeException("Test (8) failed"); - if (!ss.empty()) throw new RuntimeException("Test (9) failed"); - if (ss.size() != 0) throw new RuntimeException("Test (10) failed"); - - if (!ss.insert("key1")) throw new RuntimeException("Test (11) failed"); - if (!ss.insert("key2")) throw new RuntimeException("Test (12) failed"); - if (!ss.insert("key3")) throw new RuntimeException("Test (13) failed"); - if (ss.size() != 3) throw new RuntimeException("Test (14) failed"); + checkThat(ss.add("key1"), 10); + checkThat(ss.add("key2"), 11); + checkThat(ss.add("key3"), 12); + checkThat(ss.size() == 3, 13); ss.clear(); - if (!ss.empty()) throw new RuntimeException("Test (15) failed"); - if (ss.size() != 0) throw new RuntimeException("Test (16) failed"); + checkThat(ss.isEmpty(), 14); + checkThat(ss.size() == 0, 15); + + checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")), 16); + checkThat(ss.size() == 3, 17); + checkThat(ss.contains("one"), 18); + checkThat(!ss.contains("four"), 19); + + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")), 20); + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")), 21); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")), 22); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four")), 23); + + checkThat(!ss.addAll(java.util.Arrays.asList("one", "two", "three")), 24); + + java.util.Set found = new java.util.HashSet(); + java.util.Iterator itr = ss.iterator(); + while (itr.hasNext()) { + found.add(itr.next()); + } + + checkThat(ss.containsAll(found), 25); + checkThat(found.containsAll(ss), 26); + + java.util.AbstractSet ss2 = new StringUnorderedSet(ss); + checkThat(ss2.containsAll(ss), 27); + checkThat(ss.containsAll(ss2), 28); + + checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")), 29); + checkThat(ss.removeAll(found), 30); + checkThat(ss.isEmpty(), 31); + checkThat(ss.size() == 0, 32); } } diff --git a/Examples/test-suite/java/li_std_set_runme.java b/Examples/test-suite/java/li_std_set_runme.java index 61edd93dc..7bbd3fced 100644 --- a/Examples/test-suite/java/li_std_set_runme.java +++ b/Examples/test-suite/java/li_std_set_runme.java @@ -11,30 +11,66 @@ public class li_std_set_runme { } } + public static void failTest(int testNum) throws Throwable { + throw new RuntimeException("Test failed: " + testNum); + } + + public static void checkThat(boolean mustBeTrue, int testNum) throws Throwable { + if (!mustBeTrue) failTest(testNum); + } + public static void main(String argv[]) throws Throwable { - StringSet ss = new StringSet(); + java.util.AbstractSet ss = new StringSet(); - if (!ss.empty()) throw new RuntimeException("Test (1) failed"); - if (ss.size() != 0) throw new RuntimeException("Test (2) failed"); - if (ss.has("key")) throw new RuntimeException("Test (3) failed"); - if (ss.erase("key")) throw new RuntimeException("Test (4) failed"); + checkThat(ss.isEmpty(), 1); + checkThat(!ss.contains("key"), 2); + checkThat(!ss.remove("key"), 3); - if (!ss.insert("key")) throw new RuntimeException("Test (5) failed"); - if (ss.insert("key")) throw new RuntimeException("Test (6) failed"); - if (!ss.has("key")) throw new RuntimeException("Test (7) failed"); + checkThat(ss.add("key"), 4); + checkThat(!ss.add("key"), 5); + checkThat(ss.contains("key"), 6); + checkThat(ss.remove("key"), 7); + checkThat(ss.isEmpty(), 8); + checkThat(ss.size() == 0, 9); - if (!ss.erase("key")) throw new RuntimeException("Test (8) failed"); - if (!ss.empty()) throw new RuntimeException("Test (9) failed"); - if (ss.size() != 0) throw new RuntimeException("Test (10) failed"); - - if (!ss.insert("key1")) throw new RuntimeException("Test (11) failed"); - if (!ss.insert("key2")) throw new RuntimeException("Test (12) failed"); - if (!ss.insert("key3")) throw new RuntimeException("Test (13) failed"); - if (ss.size() != 3) throw new RuntimeException("Test (14) failed"); + checkThat(ss.add("key1"), 10); + checkThat(ss.add("key2"), 11); + checkThat(ss.add("key3"), 12); + checkThat(ss.size() == 3, 13); ss.clear(); - if (!ss.empty()) throw new RuntimeException("Test (15) failed"); - if (ss.size() != 0) throw new RuntimeException("Test (16) failed"); + checkThat(ss.isEmpty(), 14); + checkThat(ss.size() == 0, 15); + + checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")), 16); + checkThat(ss.size() == 3, 17); + checkThat(ss.contains("one"), 18); + checkThat(!ss.contains("four"), 19); + + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")), 20); + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")), 21); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")), 22); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four")), 23); + + checkThat(!ss.addAll(java.util.Arrays.asList("one", "two", "three")), 24); + + java.util.Set found = new java.util.HashSet(); + java.util.Iterator itr = ss.iterator(); + while (itr.hasNext()) { + found.add(itr.next()); + } + + checkThat(ss.containsAll(found), 25); + checkThat(found.containsAll(ss), 26); + + java.util.AbstractSet ss2 = new StringSet(ss); + checkThat(ss2.containsAll(ss), 27); + checkThat(ss.containsAll(ss2), 28); + + checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")), 29); + checkThat(ss.removeAll(found), 30); + checkThat(ss.isEmpty(), 31); + checkThat(ss.size() == 0, 32); } } diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i index 8c4bb7f17..57613f322 100644 --- a/Lib/java/std_set.i +++ b/Lib/java/std_set.i @@ -2,6 +2,8 @@ * std_set.i * * SWIG typemaps for std::set + * The Java proxy class extends java.util.AbstractSet. The std::set + * container looks and feels much like a java.util.HashSet from Java. * ----------------------------------------------------------------------------- */ %include @@ -12,35 +14,175 @@ %{ #include +#include %} +%fragment("SWIG_SetSize", "header", fragment="SWIG_JavaIntFromSize_t") { + SWIGINTERN jint SWIG_SetSize(size_t size) { + jint sz = SWIG_JavaIntFromSize_t(size); + if (sz == -1) { + throw std::out_of_range("set size is too large to fit into a Java int"); + } + + return sz; + } +} + +%javamethodmodifiers std::set::sizeImpl "private"; +%javamethodmodifiers std::set::containsImpl "private"; +%javamethodmodifiers std::set::removeImpl "private"; +%javamethodmodifiers std::set::hasNextImpl "private"; +%javamethodmodifiers std::set::begin "private"; +%javamethodmodifiers std::set::end "private"; + +%rename(Iterator) std::set::iterator; +%nodefaultctor std::set::iterator; +%javamethodmodifiers std::set::iterator::incrementUnchecked "private"; +%javamethodmodifiers std::set::iterator::derefUnchecked "private"; +%javamethodmodifiers std::set::iterator::isNot "private"; + namespace std { template class set { + +%typemap(javabase) std::set "java.util.AbstractSet<$typemap(jboxtype, KeyType)>" +%proxycode %{ + public $javaclassname(java.util.Collection collection) { + this(); + addAll(collection); + } + + public int size() { + return sizeImpl(); + } + + public boolean addAll(java.util.Collection collection) { + boolean didAddElement = false; + for (Object object : collection) { + didAddElement |= add(($typemap(jboxtype, KeyType))object); + } + + return didAddElement; + } + + public java.util.Iterator<$typemap(jboxtype, KeyType)> iterator() { + return new java.util.Iterator<$typemap(jboxtype, KeyType)>() { + private Iterator curr; + private Iterator end; + + private java.util.Iterator<$typemap(jboxtype, KeyType)> init() { + curr = $javaclassname.this.begin(); + end = $javaclassname.this.end(); + return this; + } + + public $typemap(jboxtype, KeyType) next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + + // Save the current position, increment it, + // then return the value at the position before the increment. + final $typemap(jboxtype, KeyType) currValue = curr.derefUnchecked(); + curr.incrementUnchecked(); + return currValue; + } + + public boolean hasNext() { + return curr.isNot(end); + } + }.init(); + } + + public boolean containsAll(java.util.Collection collection) { + for (Object object : collection) { + if (!contains(object)) { + return false; + } + } + + return true; + } + + public boolean contains(Object object) { + if (!(object instanceof $typemap(jboxtype, KeyType))) { + return false; + } + + return containsImpl(($typemap(jboxtype, KeyType))object); + } + + public boolean removeAll(java.util.Collection collection) { + boolean didRemoveElement = false; + for (Object object : collection) { + didRemoveElement |= remove(object); + } + + return didRemoveElement; + } + + public boolean remove(Object object) { + if (!(object instanceof $typemap(jboxtype, KeyType))) { + return false; + } + + return removeImpl(($typemap(jboxtype, KeyType))object); + } +%} + public: + + struct iterator { + %extend { + void incrementUnchecked() { + ++(*$self); + } + + KeyType derefUnchecked() const { + return **$self; + } + + bool isNot(const iterator other) const { + return (*$self != other); + } + } + }; + set(); set(const set&); - unsigned int size() const; + %rename(isEmpty) empty; bool empty() const; void clear(); + iterator begin(); + iterator end(); %extend { + %fragment("SWIG_SetSize"); + // Returns whether item was inserted. - bool insert(const KeyType& key) { + bool add(const KeyType& key) { return self->insert(key).second; } // Returns whether set contains key. - bool has(const KeyType& key) { + bool containsImpl(const KeyType& key) { return (self->count(key) > 0); } // Returns whether the item was erased. - bool erase(const KeyType& key) { + bool removeImpl(const KeyType& key) { return (self->erase(key) > 0); } + + jint sizeImpl() const throw (std::out_of_range) { + return SWIG_SetSize(self->size()); + } + + bool hasNextImpl(const iterator& itr) const { + return (itr != $self->end()); + } } }; diff --git a/Lib/java/std_unordered_set.i b/Lib/java/std_unordered_set.i index a9fac0f82..4074d8045 100644 --- a/Lib/java/std_unordered_set.i +++ b/Lib/java/std_unordered_set.i @@ -2,6 +2,8 @@ * std_unordered_set.i * * SWIG typemaps for std::unordered_set + * The Java proxy class extends java.util.AbstractSet. The std::unordered_set + * container looks and feels much like a java.util.HashSet from Java. * ----------------------------------------------------------------------------- */ %include @@ -12,35 +14,175 @@ %{ #include +#include %} +%fragment("SWIG_UnorderedSetSize", "header", fragment="SWIG_JavaIntFromSize_t") { + SWIGINTERN jint SWIG_UnorderedSetSize(size_t size) { + jint sz = SWIG_JavaIntFromSize_t(size); + if (sz == -1) { + throw std::out_of_range("unordered_set size is too large to fit into a Java int"); + } + + return sz; + } +} + +%javamethodmodifiers std::unordered_set::sizeImpl "private"; +%javamethodmodifiers std::unordered_set::containsImpl "private"; +%javamethodmodifiers std::unordered_set::removeImpl "private"; +%javamethodmodifiers std::unordered_set::hasNextImpl "private"; +%javamethodmodifiers std::unordered_set::begin "private"; +%javamethodmodifiers std::unordered_set::end "private"; + +%rename(Iterator) std::unordered_set::iterator; +%nodefaultctor std::unordered_set::iterator; +%javamethodmodifiers std::unordered_set::iterator::incrementUnchecked "private"; +%javamethodmodifiers std::unordered_set::iterator::derefUnchecked "private"; +%javamethodmodifiers std::unordered_set::iterator::isNot "private"; + namespace std { template class unordered_set { + +%typemap(javabase) std::unordered_set "java.util.AbstractSet<$typemap(jboxtype, KeyType)>" +%proxycode %{ + public $javaclassname(java.util.Collection collection) { + this(); + addAll(collection); + } + + public int size() { + return sizeImpl(); + } + + public boolean addAll(java.util.Collection collection) { + boolean didAddElement = false; + for (Object object : collection) { + didAddElement |= add(($typemap(jboxtype, KeyType))object); + } + + return didAddElement; + } + + public java.util.Iterator<$typemap(jboxtype, KeyType)> iterator() { + return new java.util.Iterator<$typemap(jboxtype, KeyType)>() { + private Iterator curr; + private Iterator end; + + private java.util.Iterator<$typemap(jboxtype, KeyType)> init() { + curr = $javaclassname.this.begin(); + end = $javaclassname.this.end(); + return this; + } + + public $typemap(jboxtype, KeyType) next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + + // Save the current position, increment it, + // then return the value at the position before the increment. + final $typemap(jboxtype, KeyType) currValue = curr.derefUnchecked(); + curr.incrementUnchecked(); + return currValue; + } + + public boolean hasNext() { + return curr.isNot(end); + } + }.init(); + } + + public boolean containsAll(java.util.Collection collection) { + for (Object object : collection) { + if (!contains(object)) { + return false; + } + } + + return true; + } + + public boolean contains(Object object) { + if (!(object instanceof $typemap(jboxtype, KeyType))) { + return false; + } + + return containsImpl(($typemap(jboxtype, KeyType))object); + } + + public boolean removeAll(java.util.Collection collection) { + boolean didRemoveElement = false; + for (Object object : collection) { + didRemoveElement |= remove(object); + } + + return didRemoveElement; + } + + public boolean remove(Object object) { + if (!(object instanceof $typemap(jboxtype, KeyType))) { + return false; + } + + return removeImpl(($typemap(jboxtype, KeyType))object); + } +%} + public: + + struct iterator { + %extend { + void incrementUnchecked() { + ++(*$self); + } + + KeyType derefUnchecked() const { + return **$self; + } + + bool isNot(const iterator other) const { + return (*$self != other); + } + } + }; + unordered_set(); unordered_set(const unordered_set&); - unsigned int size() const; + %rename(isEmpty) empty; bool empty() const; void clear(); + iterator begin(); + iterator end(); %extend { + %fragment("SWIG_UnorderedSetSize"); + // Returns whether item was inserted. - bool insert(const KeyType& key) { + bool add(const KeyType& key) { return self->insert(key).second; } // Returns whether set contains key. - bool has(const KeyType& key) { + bool containsImpl(const KeyType& key) { return (self->count(key) > 0); } // Returns whether the item was erased. - bool erase(const KeyType& key) { + bool removeImpl(const KeyType& key) { return (self->erase(key) > 0); } + + jint sizeImpl() const throw (std::out_of_range) { + return SWIG_UnorderedSetSize(self->size()); + } + + bool hasNextImpl(const iterator& itr) const { + return (itr != $self->end()); + } } }; From e6d86755137f40a6ec8e262a2a9a470554a0aac8 Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Tue, 4 Dec 2018 01:08:47 -0500 Subject: [PATCH 05/13] Mostly working for map --- .../test-suite/java/li_std_map_runme.java | 131 +++++++------- Lib/java/std_map.i | 166 +++++++++++++----- 2 files changed, 187 insertions(+), 110 deletions(-) diff --git a/Examples/test-suite/java/li_std_map_runme.java b/Examples/test-suite/java/li_std_map_runme.java index 3cc794ced..ed5b43a31 100644 --- a/Examples/test-suite/java/li_std_map_runme.java +++ b/Examples/test-suite/java/li_std_map_runme.java @@ -11,91 +11,92 @@ public class li_std_map_runme { } } + public static void failTest(int testNum) throws Throwable { + throw new RuntimeException("Test failed: " + testNum); + } + + public static void checkThat(boolean mustBeTrue, int testNum) throws Throwable { + if (!mustBeTrue) failTest(testNum); + } + public static void main(String argv[]) throws Throwable { - StringIntMap sim = new StringIntMap(); - IntIntMap iim = new IntIntMap(); + java.util.AbstractMap sim = new StringIntMap(); + java.util.AbstractMap iim = new IntIntMap(); - if (!sim.empty()) throw new RuntimeException("Test (1) failed"); - if (!iim.empty()) throw new RuntimeException("Test (2) failed"); + checkThat(sim.isEmpty(), 1); + checkThat(iim.isEmpty(), 2); + checkThat(sim.size() == 0, 3); + checkThat(iim.size() == 0, 4); - if (sim.size() != 0) throw new RuntimeException("Test (3) failed"); - if (iim.size() != 0) throw new RuntimeException("Test (4) failed"); + checkThat(sim.get("key") == null, 5); + checkThat(iim.get(1) == null, 6); - try { - sim.get("key"); - throw new RuntimeException("Test (5) failed"); - } catch (IndexOutOfBoundsException e) { - } + checkThat(!sim.containsKey("key"), 7); + checkThat(!iim.containsKey(1), 8); - try { - iim.get(1); - throw new RuntimeException("Test (6) failed"); - } catch (IndexOutOfBoundsException e) { - } + checkThat(sim.put("key", 2) == null, 9); + checkThat(iim.put(1, 2) == null, 10); - sim.set("key", 1); - iim.set(1, 1); + // if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); + // if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); - if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); - if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); + // sim.remove("key"); + // iim.remove(1); - sim.del("key"); - iim.del(1); + // if (sim.containsKey("key")) throw new RuntimeException("Test (9) failed"); + // if (iim.containsKey(1)) throw new RuntimeException("Test (10) failed"); - if (sim.has_key("key")) throw new RuntimeException("Test (9) failed"); - if (iim.has_key(1)) throw new RuntimeException("Test (10) failed"); + // if (!sim.isEmpty()) throw new RuntimeException("Test (11) failed"); + // if (!iim.isEmpty()) throw new RuntimeException("Test (12) failed"); + // if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); + // if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); - if (!sim.empty()) throw new RuntimeException("Test (11) failed"); - if (!iim.empty()) throw new RuntimeException("Test (12) failed"); - if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); - if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); + // try { + // sim.remove("key"); + // throw new RuntimeException("Test (15) failed"); + // } catch (IndexOutOfBoundsException e) { + // } - try { - sim.del("key"); - throw new RuntimeException("Test (15) failed"); - } catch (IndexOutOfBoundsException e) { - } + // try { + // iim.remove(1); + // throw new RuntimeException("Test (16) failed"); + // } catch (IndexOutOfBoundsException e) { + // } - try { - iim.del(1); - throw new RuntimeException("Test (16) failed"); - } catch (IndexOutOfBoundsException e) { - } + // sim.put("key", 1); + // iim.put(1, 1); - sim.set("key", 1); - iim.set(1, 1); + // if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); + // if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); - if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); - if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); + // sim.clear(); + // iim.clear(); - sim.clear(); - iim.clear(); + // if (sim.containsKey("key")) throw new RuntimeException("Test (19) failed"); + // if (iim.containsKey(1)) throw new RuntimeException("Test (20) failed"); - if (sim.has_key("key")) throw new RuntimeException("Test (19) failed"); - if (iim.has_key(1)) throw new RuntimeException("Test (20) failed"); + // if (!sim.isEmpty()) throw new RuntimeException("Test (21) failed"); + // if (!iim.isEmpty()) throw new RuntimeException("Test (22) failed"); + // if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); + // if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); - if (!sim.empty()) throw new RuntimeException("Test (21) failed"); - if (!iim.empty()) throw new RuntimeException("Test (22) failed"); - if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); - if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); + // sim.put("key", 1); + // sim.put("key2", 2); + // iim.put(1, 1); + // iim.put(2, 2); - sim.set("key", 1); - sim.set("key2", 2); - iim.set(1, 1); - iim.set(2, 2); + // if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); + // if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); + // if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); + // if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); - if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); - if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); - if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); - if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); + // sim.put("key", 3); + // iim.put(1, 3); - sim.set("key", 3); - iim.set(1, 3); - - if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); - if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); - if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); - if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); + // if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); + // if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); + // if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); + // if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); } } diff --git a/Lib/java/std_map.i b/Lib/java/std_map.i index 75d523cec..4169a18b4 100644 --- a/Lib/java/std_map.i +++ b/Lib/java/std_map.i @@ -2,9 +2,12 @@ * std_map.i * * SWIG typemaps for std::map + * The Java proxy class extends java.util.AbstractMap. The std::map + * container looks and feels much like a java.util.HashMap from Java. * ----------------------------------------------------------------------------- */ %include +%include // ------------------------------------------------------------------------ // std::map @@ -12,60 +15,133 @@ %{ #include -#include #include %} +%fragment("SWIG_MapSize", "header", fragment="SWIG_JavaIntFromSize_t") { + SWIGINTERN jint SWIG_MapSize(size_t size) { + jint sz = SWIG_JavaIntFromSize_t(size); + if (sz == -1) { + throw std::out_of_range("map size is too large to fit into a Java int"); + } + + return sz; + } +} + +%javamethodmodifiers std::map::sizeImpl "private"; +%javamethodmodifiers std::map::containsImpl "private"; +%javamethodmodifiers std::map::getImpl "private"; +%javamethodmodifiers std::map::putImpl "private"; +%javamethodmodifiers std::map::removeImpl "private"; + namespace std { - template > class map { - public: - typedef size_t size_type; - typedef ptrdiff_t difference_type; - typedef K key_type; - typedef T mapped_type; - map(); - map(const map< K, T, C > &); +template > class map { - unsigned int size() const; - bool empty() const; - void clear(); - %extend { - const T& get(const K& key) throw (std::out_of_range) { - std::map< K, T, C >::iterator i = self->find(key); - if (i != self->end()) - return i->second; - else - throw std::out_of_range("key not found"); - } - void set(const K& key, const T& x) { - (*self)[key] = x; - } - void del(const K& key) throw (std::out_of_range) { - std::map< K, T, C >::iterator i = self->find(key); - if (i != self->end()) - self->erase(i); - else - throw std::out_of_range("key not found"); - } - bool has_key(const K& key) { - std::map< K, T, C >::iterator i = self->find(key); - return i != self->end(); +%typemap(javabase) std::map + "java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>" + +%proxycode %{ + + public int size() { + return sizeImpl(); + } + + public boolean containsKey(Object object) { + if (!(object instanceof $typemap(jboxtype, KeyType))) { + return false; + } + + return containsImpl(($typemap(jboxtype, KeyType))object); + } + + public $typemap(jboxtype, ValueType) get(Object object) { + if (!(object instanceof $typemap(jboxtype, KeyType))) { + return null; + } + + try { + getImpl(($typemap(jboxtype, KeyType)) object); + } catch (IndexOutOfBoundsException e) {} + + return null; + } + + public $typemap(jboxtype, ValueType) put($typemap(jboxtype, KeyType) key, + $typemap(jboxtype, ValueType) value) { + try { + $typemap(jboxtype, ValueType) oldValue = putImpl(key, value); + return oldValue; + } catch (IndexOutOfBoundsException e) {} + + return null; + } + + public $typemap(jboxtype, ValueType) remove($typemap(jboxtype, KeyType) key) { + try { + $typemap(jboxtype, ValueType) oldValue = removeImpl(key); + return oldValue; + } catch (IndexOutOfBoundsException e) {} + + return null; + } + + public java.util.Set> entrySet() { + throw new RuntimeException("Stub"); + } +%} + + public: + map(); + map(const map&); + + %rename(isEmpty) empty; + bool empty() const; + void clear(); + %extend { + %fragment("SWIG_MapSize"); + + jint sizeImpl() const throw (std::out_of_range) { + return SWIG_MapSize(self->size()); + } + + bool containsImpl(const KeyType& key) { + return (self->count(key) > 0); + } + + const ValueType& getImpl(const KeyType& key) throw (std::out_of_range) { + std::map::iterator itr = self->find(key); + if (itr != self->end()) { + return itr->second; + } else { + throw std::out_of_range("map::get() - key not found"); } } - }; -// Legacy macros (deprecated) -%define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) -#warning "specialize_std_map_on_key ignored - macro is deprecated and no longer necessary" -%enddef + ValueType putImpl(const KeyType& key, const ValueType& value) { + std::map::iterator itr = self->find(key); + if (itr != self->end()) { + ValueType oldValue = itr->second; + itr->second = value; + return oldValue; + } else { + (*self)[key] = value; + throw std::out_of_range("map::put() - no existing value for key"); + } + } -%define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) -#warning "specialize_std_map_on_value ignored - macro is deprecated and no longer necessary" -%enddef - -%define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) -#warning "specialize_std_map_on_both ignored - macro is deprecated and no longer necessary" -%enddef + ValueType removeImpl(const KeyType& key) throw (std::out_of_range) { + std::map::iterator itr = self->find(key); + if (itr != self->end()) { + ValueType oldValue = itr->second; + self->erase(itr); + return oldValue; + } else { + throw std::out_of_range("map::remove() - key not found"); + } + } + } +}; } From 573ddb061e86541326ef1ff3256af65ff70c7afe Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Thu, 27 Dec 2018 01:11:54 -0500 Subject: [PATCH 06/13] Maps both working as java.util impls --- .../cpp11_li_std_unordered_map_runme.java | 149 +++++++------ .../cpp11_li_std_unordered_set_runme.java | 75 ++++--- .../test-suite/java/li_std_map_runme.java | 144 +++++++------ .../test-suite/java/li_std_set_runme.java | 75 ++++--- Lib/java/std_map.i | 158 +++++++++----- Lib/java/std_unordered_map.i | 198 +++++++++++++++--- 6 files changed, 514 insertions(+), 285 deletions(-) diff --git a/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java b/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java index f216a0131..bb85eeadd 100644 --- a/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java +++ b/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java @@ -11,91 +11,112 @@ public class cpp11_li_std_unordered_map_runme { } } + public static void checkThat(boolean mustBeTrue) throws Throwable { + if (!mustBeTrue) { + // Index [2], since this function is one hop away from main, and [1] is the current method. + throw new RuntimeException("Test failed at line number " + Thread.currentThread().getStackTrace()[2].getLineNumber()); + } + } + public static void main(String argv[]) throws Throwable { - StringIntUnorderedMap sim = new StringIntUnorderedMap(); - IntIntUnorderedMap iim = new IntIntUnorderedMap(); + java.util.AbstractMap sim = new StringIntUnorderedMap(); + java.util.AbstractMap iim = new IntIntUnorderedMap(); - if (!sim.empty()) throw new RuntimeException("Test (1) failed"); - if (!iim.empty()) throw new RuntimeException("Test (2) failed"); + checkThat(sim.isEmpty()); + checkThat(iim.isEmpty()); + checkThat(sim.size() == 0); + checkThat(iim.size() == 0); - if (sim.size() != 0) throw new RuntimeException("Test (3) failed"); - if (iim.size() != 0) throw new RuntimeException("Test (4) failed"); + checkThat(sim.get("key") == null); + checkThat(iim.get(1) == null); - try { - sim.get("key"); - throw new RuntimeException("Test (5) failed"); - } catch (IndexOutOfBoundsException e) { - } + checkThat(!sim.containsKey("key")); + checkThat(!iim.containsKey(1)); - try { - iim.get(1); - throw new RuntimeException("Test (6) failed"); - } catch (IndexOutOfBoundsException e) { - } + checkThat(sim.put("key", 2) == null); + checkThat(iim.put(1, 2) == null); - sim.set("key", 1); - iim.set(1, 1); + checkThat(sim.size() == 1); + checkThat(iim.size() == 1); + checkThat(!sim.isEmpty()); + checkThat(!iim.isEmpty()); - if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); - if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); + checkThat(sim.get("key") == 2); + checkThat(iim.get(1) == 2); - sim.del("key"); - iim.del(1); + checkThat(sim.remove("key") == 2); + checkThat(iim.remove(1) == 2); - if (sim.has_key("key")) throw new RuntimeException("Test (9) failed"); - if (iim.has_key(1)) throw new RuntimeException("Test (10) failed"); + checkThat(sim.isEmpty()); + checkThat(iim.isEmpty()); + checkThat(sim.size() == 0); + checkThat(iim.size() == 0); - if (!sim.empty()) throw new RuntimeException("Test (11) failed"); - if (!iim.empty()) throw new RuntimeException("Test (12) failed"); - if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); - if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); + checkThat(sim.get("key") == null); + checkThat(iim.get(1) == null); - try { - sim.del("key"); - throw new RuntimeException("Test (15) failed"); - } catch (IndexOutOfBoundsException e) { - } + checkThat(sim.remove("key") == null); + checkThat(iim.remove(1) == null); - try { - iim.del(1); - throw new RuntimeException("Test (16) failed"); - } catch (IndexOutOfBoundsException e) { - } - - sim.set("key", 1); - iim.set(1, 1); - - if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); - if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); + checkThat(sim.put("key", 2) == null); + checkThat(iim.put(1, 2) == null); sim.clear(); iim.clear(); + checkThat(sim.isEmpty()); + checkThat(iim.isEmpty()); - if (sim.has_key("key")) throw new RuntimeException("Test (19) failed"); - if (iim.has_key(1)) throw new RuntimeException("Test (20) failed"); + checkThat(sim.put("key1", 1) == null); + checkThat(iim.put(1, 1) == null); + checkThat(sim.put("key2", 2) == null); + checkThat(iim.put(2, 2) == null); - if (!sim.empty()) throw new RuntimeException("Test (21) failed"); - if (!iim.empty()) throw new RuntimeException("Test (22) failed"); - if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); - if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); + checkThat(sim.size() == 2); + checkThat(iim.size() == 2); + checkThat(sim.get("key1") == 1); + checkThat(iim.get(1) == 1); + checkThat(sim.get("key2") == 2); + checkThat(iim.get(2) == 2); - sim.set("key", 1); - sim.set("key2", 2); - iim.set(1, 1); - iim.set(2, 2); + checkThat(sim.put("key1", 3) == 1); + checkThat(iim.put(1, 3) == 1); - if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); - if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); - if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); - if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); + checkThat(sim.size() == 2); + checkThat(iim.size() == 2); + checkThat(sim.get("key1") == 3); + checkThat(iim.get(1) == 3); - sim.set("key", 3); - iim.set(1, 3); + java.util.Set> sim_es = sim.entrySet(); + java.util.Map sim_default = new java.util.HashMap(); + sim_default.put("key1", 3); + sim_default.put("key2", 2); + java.util.Set> sim_es_default = sim_default.entrySet(); + checkThat(sim_es.size() == sim_es_default.size()); + for (java.util.Map.Entry entry : sim_es) { + checkThat(sim_es_default.contains(entry)); + checkThat(sim_default.containsKey(entry.getKey())); + checkThat(sim_default.containsValue(entry.getValue())); - if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); - if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); - if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); - if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); + Integer oldValue = entry.getValue(); + entry.setValue(oldValue + 1); + checkThat(sim.get(entry.getKey()) == (oldValue + 1)); + } + + java.util.Set> iim_es = iim.entrySet(); + java.util.Map iim_default = new java.util.HashMap(); + iim_default.put(1, 3); + iim_default.put(2, 2); + java.util.Set> iim_es_default = iim_default.entrySet(); + checkThat(iim_es.size() == iim_es_default.size()); + for (java.util.Map.Entry entry : iim_es) { + checkThat(iim_es_default.contains(entry)); + checkThat(iim_default.containsKey(entry.getKey())); + checkThat(iim_default.containsValue(entry.getValue())); + + Integer oldValue = entry.getValue(); + entry.setValue(oldValue + 1); + checkThat(iim.get(entry.getKey()) == (oldValue + 1)); + } } } diff --git a/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java b/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java index 15f6eba0a..dddf00307 100644 --- a/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java +++ b/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java @@ -11,49 +11,48 @@ public class cpp11_li_std_unordered_set_runme { } } - public static void failTest(int testNum) throws Throwable { - throw new RuntimeException("Test failed: " + testNum); - } - - public static void checkThat(boolean mustBeTrue, int testNum) throws Throwable { - if (!mustBeTrue) failTest(testNum); + public static void checkThat(boolean mustBeTrue) throws Throwable { + if (!mustBeTrue) { + // Index [2], since this function is one hop away from main, and [1] is the current method. + throw new RuntimeException("Test failed at line number " + Thread.currentThread().getStackTrace()[2].getLineNumber()); + } } public static void main(String argv[]) throws Throwable { java.util.AbstractSet ss = new StringUnorderedSet(); - checkThat(ss.isEmpty(), 1); - checkThat(!ss.contains("key"), 2); - checkThat(!ss.remove("key"), 3); + checkThat(ss.isEmpty()); + checkThat(!ss.contains("key")); + checkThat(!ss.remove("key")); - checkThat(ss.add("key"), 4); - checkThat(!ss.add("key"), 5); - checkThat(ss.contains("key"), 6); - checkThat(ss.remove("key"), 7); - checkThat(ss.isEmpty(), 8); - checkThat(ss.size() == 0, 9); + checkThat(ss.add("key")); + checkThat(!ss.add("key")); + checkThat(ss.contains("key")); + checkThat(ss.remove("key")); + checkThat(ss.isEmpty()); + checkThat(ss.size() == 0); - checkThat(ss.add("key1"), 10); - checkThat(ss.add("key2"), 11); - checkThat(ss.add("key3"), 12); - checkThat(ss.size() == 3, 13); + checkThat(ss.add("key1")); + checkThat(ss.add("key2")); + checkThat(ss.add("key3")); + checkThat(ss.size() == 3); ss.clear(); - checkThat(ss.isEmpty(), 14); - checkThat(ss.size() == 0, 15); + checkThat(ss.isEmpty()); + checkThat(ss.size() == 0); - checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")), 16); - checkThat(ss.size() == 3, 17); - checkThat(ss.contains("one"), 18); - checkThat(!ss.contains("four"), 19); + checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three"))); + checkThat(ss.size() == 3); + checkThat(ss.contains("one")); + checkThat(!ss.contains("four")); - checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")), 20); - checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")), 21); - checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")), 22); - checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four")), 23); + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three"))); + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two"))); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four"))); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four"))); - checkThat(!ss.addAll(java.util.Arrays.asList("one", "two", "three")), 24); + checkThat(!ss.addAll(java.util.Arrays.asList("one", "two", "three"))); java.util.Set found = new java.util.HashSet(); java.util.Iterator itr = ss.iterator(); @@ -61,16 +60,16 @@ public class cpp11_li_std_unordered_set_runme { found.add(itr.next()); } - checkThat(ss.containsAll(found), 25); - checkThat(found.containsAll(ss), 26); + checkThat(ss.containsAll(found)); + checkThat(found.containsAll(ss)); java.util.AbstractSet ss2 = new StringUnorderedSet(ss); - checkThat(ss2.containsAll(ss), 27); - checkThat(ss.containsAll(ss2), 28); + checkThat(ss2.containsAll(ss)); + checkThat(ss.containsAll(ss2)); - checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")), 29); - checkThat(ss.removeAll(found), 30); - checkThat(ss.isEmpty(), 31); - checkThat(ss.size() == 0, 32); + checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four"))); + checkThat(ss.removeAll(found)); + checkThat(ss.isEmpty()); + checkThat(ss.size() == 0); } } diff --git a/Examples/test-suite/java/li_std_map_runme.java b/Examples/test-suite/java/li_std_map_runme.java index ed5b43a31..7ad4370cc 100644 --- a/Examples/test-suite/java/li_std_map_runme.java +++ b/Examples/test-suite/java/li_std_map_runme.java @@ -11,12 +11,11 @@ public class li_std_map_runme { } } - public static void failTest(int testNum) throws Throwable { - throw new RuntimeException("Test failed: " + testNum); - } - - public static void checkThat(boolean mustBeTrue, int testNum) throws Throwable { - if (!mustBeTrue) failTest(testNum); + public static void checkThat(boolean mustBeTrue) throws Throwable { + if (!mustBeTrue) { + // Index [2], since this function is one hop away from main, and [1] is the current method. + throw new RuntimeException("Test failed at line number " + Thread.currentThread().getStackTrace()[2].getLineNumber()); + } } public static void main(String argv[]) throws Throwable @@ -24,79 +23,100 @@ public class li_std_map_runme { java.util.AbstractMap sim = new StringIntMap(); java.util.AbstractMap iim = new IntIntMap(); - checkThat(sim.isEmpty(), 1); - checkThat(iim.isEmpty(), 2); - checkThat(sim.size() == 0, 3); - checkThat(iim.size() == 0, 4); + checkThat(sim.isEmpty()); + checkThat(iim.isEmpty()); + checkThat(sim.size() == 0); + checkThat(iim.size() == 0); - checkThat(sim.get("key") == null, 5); - checkThat(iim.get(1) == null, 6); + checkThat(sim.get("key") == null); + checkThat(iim.get(1) == null); - checkThat(!sim.containsKey("key"), 7); - checkThat(!iim.containsKey(1), 8); + checkThat(!sim.containsKey("key")); + checkThat(!iim.containsKey(1)); - checkThat(sim.put("key", 2) == null, 9); - checkThat(iim.put(1, 2) == null, 10); + checkThat(sim.put("key", 2) == null); + checkThat(iim.put(1, 2) == null); - // if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); - // if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); + checkThat(sim.size() == 1); + checkThat(iim.size() == 1); + checkThat(!sim.isEmpty()); + checkThat(!iim.isEmpty()); - // sim.remove("key"); - // iim.remove(1); + checkThat(sim.get("key") == 2); + checkThat(iim.get(1) == 2); - // if (sim.containsKey("key")) throw new RuntimeException("Test (9) failed"); - // if (iim.containsKey(1)) throw new RuntimeException("Test (10) failed"); + checkThat(sim.remove("key") == 2); + checkThat(iim.remove(1) == 2); - // if (!sim.isEmpty()) throw new RuntimeException("Test (11) failed"); - // if (!iim.isEmpty()) throw new RuntimeException("Test (12) failed"); - // if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); - // if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); + checkThat(sim.isEmpty()); + checkThat(iim.isEmpty()); + checkThat(sim.size() == 0); + checkThat(iim.size() == 0); - // try { - // sim.remove("key"); - // throw new RuntimeException("Test (15) failed"); - // } catch (IndexOutOfBoundsException e) { - // } + checkThat(sim.get("key") == null); + checkThat(iim.get(1) == null); - // try { - // iim.remove(1); - // throw new RuntimeException("Test (16) failed"); - // } catch (IndexOutOfBoundsException e) { - // } + checkThat(sim.remove("key") == null); + checkThat(iim.remove(1) == null); - // sim.put("key", 1); - // iim.put(1, 1); + checkThat(sim.put("key", 2) == null); + checkThat(iim.put(1, 2) == null); - // if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); - // if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); + sim.clear(); + iim.clear(); + checkThat(sim.isEmpty()); + checkThat(iim.isEmpty()); - // sim.clear(); - // iim.clear(); + checkThat(sim.put("key1", 1) == null); + checkThat(iim.put(1, 1) == null); + checkThat(sim.put("key2", 2) == null); + checkThat(iim.put(2, 2) == null); - // if (sim.containsKey("key")) throw new RuntimeException("Test (19) failed"); - // if (iim.containsKey(1)) throw new RuntimeException("Test (20) failed"); + checkThat(sim.size() == 2); + checkThat(iim.size() == 2); + checkThat(sim.get("key1") == 1); + checkThat(iim.get(1) == 1); + checkThat(sim.get("key2") == 2); + checkThat(iim.get(2) == 2); - // if (!sim.isEmpty()) throw new RuntimeException("Test (21) failed"); - // if (!iim.isEmpty()) throw new RuntimeException("Test (22) failed"); - // if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); - // if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); + checkThat(sim.put("key1", 3) == 1); + checkThat(iim.put(1, 3) == 1); - // sim.put("key", 1); - // sim.put("key2", 2); - // iim.put(1, 1); - // iim.put(2, 2); + checkThat(sim.size() == 2); + checkThat(iim.size() == 2); + checkThat(sim.get("key1") == 3); + checkThat(iim.get(1) == 3); - // if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); - // if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); - // if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); - // if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); + java.util.Set> sim_es = sim.entrySet(); + java.util.Map sim_default = new java.util.HashMap(); + sim_default.put("key1", 3); + sim_default.put("key2", 2); + java.util.Set> sim_es_default = sim_default.entrySet(); + checkThat(sim_es.size() == sim_es_default.size()); + for (java.util.Map.Entry entry : sim_es) { + checkThat(sim_es_default.contains(entry)); + checkThat(sim_default.containsKey(entry.getKey())); + checkThat(sim_default.containsValue(entry.getValue())); - // sim.put("key", 3); - // iim.put(1, 3); + Integer oldValue = entry.getValue(); + entry.setValue(oldValue + 1); + checkThat(sim.get(entry.getKey()) == (oldValue + 1)); + } - // if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); - // if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); - // if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); - // if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); + java.util.Set> iim_es = iim.entrySet(); + java.util.Map iim_default = new java.util.HashMap(); + iim_default.put(1, 3); + iim_default.put(2, 2); + java.util.Set> iim_es_default = iim_default.entrySet(); + checkThat(iim_es.size() == iim_es_default.size()); + for (java.util.Map.Entry entry : iim_es) { + checkThat(iim_es_default.contains(entry)); + checkThat(iim_default.containsKey(entry.getKey())); + checkThat(iim_default.containsValue(entry.getValue())); + + Integer oldValue = entry.getValue(); + entry.setValue(oldValue + 1); + checkThat(iim.get(entry.getKey()) == (oldValue + 1)); + } } } diff --git a/Examples/test-suite/java/li_std_set_runme.java b/Examples/test-suite/java/li_std_set_runme.java index 7bbd3fced..9763484c2 100644 --- a/Examples/test-suite/java/li_std_set_runme.java +++ b/Examples/test-suite/java/li_std_set_runme.java @@ -11,49 +11,48 @@ public class li_std_set_runme { } } - public static void failTest(int testNum) throws Throwable { - throw new RuntimeException("Test failed: " + testNum); - } - - public static void checkThat(boolean mustBeTrue, int testNum) throws Throwable { - if (!mustBeTrue) failTest(testNum); + public static void checkThat(boolean mustBeTrue) throws Throwable { + if (!mustBeTrue) { + // Index [2], since this function is one hop away from main, and [1] is the current method. + throw new RuntimeException("Test failed at line number " + Thread.currentThread().getStackTrace()[2].getLineNumber()); + } } public static void main(String argv[]) throws Throwable { java.util.AbstractSet ss = new StringSet(); - checkThat(ss.isEmpty(), 1); - checkThat(!ss.contains("key"), 2); - checkThat(!ss.remove("key"), 3); + checkThat(ss.isEmpty()); + checkThat(!ss.contains("key")); + checkThat(!ss.remove("key")); - checkThat(ss.add("key"), 4); - checkThat(!ss.add("key"), 5); - checkThat(ss.contains("key"), 6); - checkThat(ss.remove("key"), 7); - checkThat(ss.isEmpty(), 8); - checkThat(ss.size() == 0, 9); + checkThat(ss.add("key")); + checkThat(!ss.add("key")); + checkThat(ss.contains("key")); + checkThat(ss.remove("key")); + checkThat(ss.isEmpty()); + checkThat(ss.size() == 0); - checkThat(ss.add("key1"), 10); - checkThat(ss.add("key2"), 11); - checkThat(ss.add("key3"), 12); - checkThat(ss.size() == 3, 13); + checkThat(ss.add("key1")); + checkThat(ss.add("key2")); + checkThat(ss.add("key3")); + checkThat(ss.size() == 3); ss.clear(); - checkThat(ss.isEmpty(), 14); - checkThat(ss.size() == 0, 15); + checkThat(ss.isEmpty()); + checkThat(ss.size() == 0); - checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")), 16); - checkThat(ss.size() == 3, 17); - checkThat(ss.contains("one"), 18); - checkThat(!ss.contains("four"), 19); + checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three"))); + checkThat(ss.size() == 3); + checkThat(ss.contains("one")); + checkThat(!ss.contains("four")); - checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")), 20); - checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")), 21); - checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")), 22); - checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four")), 23); + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three"))); + checkThat(ss.containsAll(java.util.Arrays.asList("one", "two"))); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four"))); + checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four"))); - checkThat(!ss.addAll(java.util.Arrays.asList("one", "two", "three")), 24); + checkThat(!ss.addAll(java.util.Arrays.asList("one", "two", "three"))); java.util.Set found = new java.util.HashSet(); java.util.Iterator itr = ss.iterator(); @@ -61,16 +60,16 @@ public class li_std_set_runme { found.add(itr.next()); } - checkThat(ss.containsAll(found), 25); - checkThat(found.containsAll(ss), 26); + checkThat(ss.containsAll(found)); + checkThat(found.containsAll(ss)); java.util.AbstractSet ss2 = new StringSet(ss); - checkThat(ss2.containsAll(ss), 27); - checkThat(ss.containsAll(ss2), 28); + checkThat(ss2.containsAll(ss)); + checkThat(ss.containsAll(ss2)); - checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")), 29); - checkThat(ss.removeAll(found), 30); - checkThat(ss.isEmpty(), 31); - checkThat(ss.size() == 0, 32); + checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four"))); + checkThat(ss.removeAll(found)); + checkThat(ss.isEmpty()); + checkThat(ss.size() == 0); } } diff --git a/Lib/java/std_map.i b/Lib/java/std_map.i index 4169a18b4..d59ceb8fc 100644 --- a/Lib/java/std_map.i +++ b/Lib/java/std_map.i @@ -7,7 +7,6 @@ * ----------------------------------------------------------------------------- */ %include -%include // ------------------------------------------------------------------------ // std::map @@ -31,9 +30,19 @@ %javamethodmodifiers std::map::sizeImpl "private"; %javamethodmodifiers std::map::containsImpl "private"; -%javamethodmodifiers std::map::getImpl "private"; -%javamethodmodifiers std::map::putImpl "private"; -%javamethodmodifiers std::map::removeImpl "private"; +%javamethodmodifiers std::map::putUnchecked "private"; +%javamethodmodifiers std::map::removeUnchecked "private"; +%javamethodmodifiers std::map::find "private"; +%javamethodmodifiers std::map::begin "private"; +%javamethodmodifiers std::map::end "private"; + +%rename(Iterator) std::map::iterator; +%nodefaultctor std::map::iterator; +%javamethodmodifiers std::map::iterator::getNextUnchecked "private"; +%javamethodmodifiers std::map::iterator::isNot "private"; +%javamethodmodifiers std::map::iterator::getKey "private"; +%javamethodmodifiers std::map::iterator::getValue "private"; +%javamethodmodifiers std::map::iterator::setValue "private"; namespace std { @@ -48,47 +57,88 @@ template > return sizeImpl(); } - public boolean containsKey(Object object) { - if (!(object instanceof $typemap(jboxtype, KeyType))) { + public boolean containsKey(Object key) { + if (!(key instanceof $typemap(jboxtype, KeyType))) { return false; } - return containsImpl(($typemap(jboxtype, KeyType))object); + return containsImpl(($typemap(jboxtype, KeyType))key); } - public $typemap(jboxtype, ValueType) get(Object object) { - if (!(object instanceof $typemap(jboxtype, KeyType))) { + public $typemap(jboxtype, ValueType) get(Object key) { + if (!(key instanceof $typemap(jboxtype, KeyType))) { return null; } - try { - getImpl(($typemap(jboxtype, KeyType)) object); - } catch (IndexOutOfBoundsException e) {} + Iterator itr = find(($typemap(jboxtype, KeyType)) key); + if (itr.isNot(end())) { + return itr.getValue(); + } return null; } public $typemap(jboxtype, ValueType) put($typemap(jboxtype, KeyType) key, $typemap(jboxtype, ValueType) value) { - try { - $typemap(jboxtype, ValueType) oldValue = putImpl(key, value); + Iterator itr = find(($typemap(jboxtype, KeyType)) key); + if (itr.isNot(end())) { + $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + itr.setValue(value); return oldValue; - } catch (IndexOutOfBoundsException e) {} - - return null; + } else { + putUnchecked(key, value); + return null; + } } - public $typemap(jboxtype, ValueType) remove($typemap(jboxtype, KeyType) key) { - try { - $typemap(jboxtype, ValueType) oldValue = removeImpl(key); - return oldValue; - } catch (IndexOutOfBoundsException e) {} + public $typemap(jboxtype, ValueType) remove(Object key) { + if (!(key instanceof $typemap(jboxtype, KeyType))) { + return null; + } - return null; + Iterator itr = find(($typemap(jboxtype, KeyType)) key); + if (itr.isNot(end())) { + $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + removeUnchecked(itr); + return oldValue; + } else { + return null; + } } public java.util.Set> entrySet() { - throw new RuntimeException("Stub"); + java.util.Set> setToReturn = + new java.util.HashSet>(); + + Iterator itr = begin(); + final Iterator end = end(); + while(itr.isNot(end)) { + setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>() { + private Iterator iterator; + + private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)> init(Iterator iterator) { + this.iterator = iterator; + return this; + } + + public $typemap(jboxtype, KeyType) getKey() { + return iterator.getKey(); + } + + public $typemap(jboxtype, ValueType) getValue() { + return iterator.getValue(); + } + + public $typemap(jboxtype, ValueType) setValue($typemap(jboxtype, ValueType) newValue) { + $typemap(jboxtype, ValueType) oldValue = iterator.getValue(); + iterator.setValue(newValue); + return oldValue; + } + }.init(itr)); + itr = itr.getNextUnchecked(); + } + + return setToReturn; } %} @@ -96,9 +146,37 @@ template > map(); map(const map&); + struct iterator { + %extend { + std::map::iterator getNextUnchecked() { + std::map::iterator copy = (*$self); + return ++copy; + } + + bool isNot(const iterator other) const { + return (*$self != other); + } + + KeyType getKey() const { + return (*$self)->first; + } + + ValueType getValue() const { + return (*$self)->second; + } + + void setValue(const ValueType& newValue) { + (*$self)->second = newValue; + } + } + }; + %rename(isEmpty) empty; bool empty() const; void clear(); + iterator find(const KeyType&); + iterator begin(); + iterator end(); %extend { %fragment("SWIG_MapSize"); @@ -110,36 +188,12 @@ template > return (self->count(key) > 0); } - const ValueType& getImpl(const KeyType& key) throw (std::out_of_range) { - std::map::iterator itr = self->find(key); - if (itr != self->end()) { - return itr->second; - } else { - throw std::out_of_range("map::get() - key not found"); - } + void putUnchecked(const KeyType& key, const ValueType& value) { + (*self)[key] = value; } - ValueType putImpl(const KeyType& key, const ValueType& value) { - std::map::iterator itr = self->find(key); - if (itr != self->end()) { - ValueType oldValue = itr->second; - itr->second = value; - return oldValue; - } else { - (*self)[key] = value; - throw std::out_of_range("map::put() - no existing value for key"); - } - } - - ValueType removeImpl(const KeyType& key) throw (std::out_of_range) { - std::map::iterator itr = self->find(key); - if (itr != self->end()) { - ValueType oldValue = itr->second; - self->erase(itr); - return oldValue; - } else { - throw std::out_of_range("map::remove() - key not found"); - } + void removeUnchecked(const std::map::iterator itr) { + self->erase(itr); } } }; diff --git a/Lib/java/std_unordered_map.i b/Lib/java/std_unordered_map.i index 434292b07..b0e8b3e40 100644 --- a/Lib/java/std_unordered_map.i +++ b/Lib/java/std_unordered_map.i @@ -2,6 +2,8 @@ * std_unordered_map.i * * SWIG typemaps for std::unordered_map + * The Java proxy class extends java.util.AbstractMap. The std::unordered_map + * container looks and feels much like a java.util.HashMap from Java. * ----------------------------------------------------------------------------- */ %include @@ -15,51 +17,185 @@ #include %} +%fragment("SWIG_MapSize", "header", fragment="SWIG_JavaIntFromSize_t") { + SWIGINTERN jint SWIG_MapSize(size_t size) { + jint sz = SWIG_JavaIntFromSize_t(size); + if (sz == -1) { + throw std::out_of_range("map size is too large to fit into a Java int"); + } + + return sz; + } +} + +%javamethodmodifiers std::unordered_map::sizeImpl "private"; +%javamethodmodifiers std::unordered_map::containsImpl "private"; +%javamethodmodifiers std::unordered_map::putUnchecked "private"; +%javamethodmodifiers std::unordered_map::removeUnchecked "private"; +%javamethodmodifiers std::unordered_map::find "private"; +%javamethodmodifiers std::unordered_map::begin "private"; +%javamethodmodifiers std::unordered_map::end "private"; + +%rename(Iterator) std::unordered_map::iterator; +%nodefaultctor std::unordered_map::iterator; +%javamethodmodifiers std::unordered_map::iterator::getNextUnchecked "private"; +%javamethodmodifiers std::unordered_map::iterator::isNot "private"; +%javamethodmodifiers std::unordered_map::iterator::getKey "private"; +%javamethodmodifiers std::unordered_map::iterator::getValue "private"; +%javamethodmodifiers std::unordered_map::iterator::setValue "private"; + namespace std { -template class unordered_map { - public: - typedef size_t size_type; - typedef ptrdiff_t difference_type; - typedef KeyType key_type; - typedef MappedType mapped_type; - unordered_map(); - unordered_map(const unordered_map&); +template class unordered_map { - unsigned int size() const; +%typemap(javabase) std::unordered_map + "java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>" + +%proxycode %{ + + public int size() { + return sizeImpl(); + } + + public boolean containsKey(Object key) { + if (!(key instanceof $typemap(jboxtype, KeyType))) { + return false; + } + + return containsImpl(($typemap(jboxtype, KeyType))key); + } + + public $typemap(jboxtype, ValueType) get(Object key) { + if (!(key instanceof $typemap(jboxtype, KeyType))) { + return null; + } + + Iterator itr = find(($typemap(jboxtype, KeyType)) key); + if (itr.isNot(end())) { + return itr.getValue(); + } + + return null; + } + + public $typemap(jboxtype, ValueType) put($typemap(jboxtype, KeyType) key, + $typemap(jboxtype, ValueType) value) { + Iterator itr = find(($typemap(jboxtype, KeyType)) key); + if (itr.isNot(end())) { + $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + itr.setValue(value); + return oldValue; + } else { + putUnchecked(key, value); + return null; + } + } + + public $typemap(jboxtype, ValueType) remove(Object key) { + if (!(key instanceof $typemap(jboxtype, KeyType))) { + return null; + } + + Iterator itr = find(($typemap(jboxtype, KeyType)) key); + if (itr.isNot(end())) { + $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + removeUnchecked(itr); + return oldValue; + } else { + return null; + } + } + + public java.util.Set> entrySet() { + java.util.Set> setToReturn = + new java.util.HashSet>(); + + Iterator itr = begin(); + final Iterator end = end(); + while(itr.isNot(end)) { + setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>() { + private Iterator iterator; + + private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)> init(Iterator iterator) { + this.iterator = iterator; + return this; + } + + public $typemap(jboxtype, KeyType) getKey() { + return iterator.getKey(); + } + + public $typemap(jboxtype, ValueType) getValue() { + return iterator.getValue(); + } + + public $typemap(jboxtype, ValueType) setValue($typemap(jboxtype, ValueType) newValue) { + $typemap(jboxtype, ValueType) oldValue = iterator.getValue(); + iterator.setValue(newValue); + return oldValue; + } + }.init(itr)); + itr = itr.getNextUnchecked(); + } + + return setToReturn; + } +%} + + public: + unordered_map(); + unordered_map(const unordered_map&); + + struct iterator { + %extend { + std::unordered_map::iterator getNextUnchecked() { + std::unordered_map::iterator copy = (*$self); + return ++copy; + } + + bool isNot(const iterator other) const { + return (*$self != other); + } + + KeyType getKey() const { + return (*$self)->first; + } + + ValueType getValue() const { + return (*$self)->second; + } + + void setValue(const ValueType& newValue) { + (*$self)->second = newValue; + } + } + }; + + %rename(isEmpty) empty; bool empty() const; void clear(); - + iterator find(const KeyType&); + iterator begin(); + iterator end(); %extend { - const MappedType& get(const KeyType& key) throw (std::out_of_range) { - std::unordered_map::iterator i = self->find(key); + %fragment("SWIG_MapSize"); - if (i != self->end()) { - return i->second; - } else { - throw std::out_of_range("key not found"); - } + jint sizeImpl() const throw (std::out_of_range) { + return SWIG_MapSize(self->size()); } - void set(const KeyType& key, const MappedType& x) { - (*self)[key] = x; + bool containsImpl(const KeyType& key) { + return (self->count(key) > 0); } - void del(const KeyType& key) throw (std::out_of_range) { - std::unordered_map::iterator i = self->find(key); - - if (i != self->end()) { - self->erase(i); - } else { - throw std::out_of_range("key not found"); - } + void putUnchecked(const KeyType& key, const ValueType& value) { + (*self)[key] = value; } - bool has_key(const KeyType& key) { - std::unordered_map::iterator i = self->find(key); - return i != self->end(); + void removeUnchecked(const std::unordered_map::iterator itr) { + self->erase(itr); } } }; -} // namespace std +} From 7e9f1a79724d6c0d480999958109f2a2f2e3494f Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Thu, 27 Dec 2018 01:15:18 -0500 Subject: [PATCH 07/13] Add to STL file as well --- Lib/java/std_set.i | 2 +- Lib/java/std_unordered_set.i | 2 +- Lib/java/stl.i | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i index 57613f322..de0c31fba 100644 --- a/Lib/java/std_set.i +++ b/Lib/java/std_set.i @@ -186,4 +186,4 @@ class set { } }; -} // namespace std \ No newline at end of file +} // namespace std diff --git a/Lib/java/std_unordered_set.i b/Lib/java/std_unordered_set.i index 4074d8045..ca9af0f19 100644 --- a/Lib/java/std_unordered_set.i +++ b/Lib/java/std_unordered_set.i @@ -186,4 +186,4 @@ class unordered_set { } }; -} // namespace std \ No newline at end of file +} // namespace std diff --git a/Lib/java/stl.i b/Lib/java/stl.i index 04f86014f..6f2a4342a 100644 --- a/Lib/java/stl.i +++ b/Lib/java/stl.i @@ -3,8 +3,11 @@ * ----------------------------------------------------------------------------- */ %include -%include -%include %include %include +%include +%include +%include +%include +%include From 945bd7c8088af6813195d9584e6b373e47341363 Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Thu, 27 Dec 2018 11:47:16 -0500 Subject: [PATCH 08/13] Remove c++11 from stl.i --- Lib/java/stl.i | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/java/stl.i b/Lib/java/stl.i index 6f2a4342a..a04adf7b6 100644 --- a/Lib/java/stl.i +++ b/Lib/java/stl.i @@ -7,7 +7,4 @@ %include %include %include -%include -%include %include - From d06ffe1087c714d163c09e57f6f3c20ccbcd4922 Mon Sep 17 00:00:00 2001 From: Brad Kotsopoulos Date: Mon, 4 Feb 2019 22:23:13 -0500 Subject: [PATCH 09/13] Legacy macros, protected iterator, typedefs --- Lib/java/std_map.i | 70 +++++++++++++++++++++++------------- Lib/java/std_set.i | 6 ++++ Lib/java/std_unordered_map.i | 54 +++++++++++++++------------- Lib/java/std_unordered_set.i | 6 ++++ 4 files changed, 87 insertions(+), 49 deletions(-) diff --git a/Lib/java/std_map.i b/Lib/java/std_map.i index d59ceb8fc..5b7517ad5 100644 --- a/Lib/java/std_map.i +++ b/Lib/java/std_map.i @@ -46,10 +46,10 @@ namespace std { -template > class map { +template > class map { -%typemap(javabase) std::map - "java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>" +%typemap(javabase) std::map + "java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>" %proxycode %{ @@ -65,7 +65,7 @@ template > return containsImpl(($typemap(jboxtype, KeyType))key); } - public $typemap(jboxtype, ValueType) get(Object key) { + public $typemap(jboxtype, MappedType) get(Object key) { if (!(key instanceof $typemap(jboxtype, KeyType))) { return null; } @@ -78,11 +78,11 @@ template > return null; } - public $typemap(jboxtype, ValueType) put($typemap(jboxtype, KeyType) key, - $typemap(jboxtype, ValueType) value) { + public $typemap(jboxtype, MappedType) put($typemap(jboxtype, KeyType) key, + $typemap(jboxtype, MappedType) value) { Iterator itr = find(($typemap(jboxtype, KeyType)) key); if (itr.isNot(end())) { - $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + $typemap(jboxtype, MappedType) oldValue = itr.getValue(); itr.setValue(value); return oldValue; } else { @@ -91,14 +91,14 @@ template > } } - public $typemap(jboxtype, ValueType) remove(Object key) { + public $typemap(jboxtype, MappedType) remove(Object key) { if (!(key instanceof $typemap(jboxtype, KeyType))) { return null; } Iterator itr = find(($typemap(jboxtype, KeyType)) key); if (itr.isNot(end())) { - $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + $typemap(jboxtype, MappedType) oldValue = itr.getValue(); removeUnchecked(itr); return oldValue; } else { @@ -106,17 +106,17 @@ template > } } - public java.util.Set> entrySet() { - java.util.Set> setToReturn = - new java.util.HashSet>(); + public java.util.Set> entrySet() { + java.util.Set> setToReturn = + new java.util.HashSet>(); Iterator itr = begin(); final Iterator end = end(); while(itr.isNot(end)) { - setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>() { + setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>() { private Iterator iterator; - private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)> init(Iterator iterator) { + private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)> init(Iterator iterator) { this.iterator = iterator; return this; } @@ -125,12 +125,12 @@ template > return iterator.getKey(); } - public $typemap(jboxtype, ValueType) getValue() { + public $typemap(jboxtype, MappedType) getValue() { return iterator.getValue(); } - public $typemap(jboxtype, ValueType) setValue($typemap(jboxtype, ValueType) newValue) { - $typemap(jboxtype, ValueType) oldValue = iterator.getValue(); + public $typemap(jboxtype, MappedType) setValue($typemap(jboxtype, MappedType) newValue) { + $typemap(jboxtype, MappedType) oldValue = iterator.getValue(); iterator.setValue(newValue); return oldValue; } @@ -143,17 +143,24 @@ template > %} public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef KeyType key_type; + typedef MappedType mapped_type; + typedef Compare key_compare; + map(); - map(const map&); + map(const map&); struct iterator { + %typemap(javaclassmodifiers) iterator "protected class" %extend { - std::map::iterator getNextUnchecked() { - std::map::iterator copy = (*$self); + std::map::iterator getNextUnchecked() { + std::map::iterator copy = (*$self); return ++copy; } - bool isNot(const iterator other) const { + bool isNot(iterator other) const { return (*$self != other); } @@ -161,11 +168,11 @@ template > return (*$self)->first; } - ValueType getValue() const { + MappedType getValue() const { return (*$self)->second; } - void setValue(const ValueType& newValue) { + void setValue(const MappedType& newValue) { (*$self)->second = newValue; } } @@ -188,14 +195,27 @@ template > return (self->count(key) > 0); } - void putUnchecked(const KeyType& key, const ValueType& value) { + void putUnchecked(const KeyType& key, const MappedType& value) { (*self)[key] = value; } - void removeUnchecked(const std::map::iterator itr) { + void removeUnchecked(const std::map::iterator itr) { self->erase(itr); } } }; +// Legacy macros (deprecated) +%define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) +#warning "specialize_std_map_on_key ignored - macro is deprecated and no longer necessary" +%enddef + +%define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) +#warning "specialize_std_map_on_value ignored - macro is deprecated and no longer necessary" +%enddef + +%define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) +#warning "specialize_std_map_on_both ignored - macro is deprecated and no longer necessary" +%enddef + } diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i index de0c31fba..6f557f627 100644 --- a/Lib/java/std_set.i +++ b/Lib/java/std_set.i @@ -134,6 +134,7 @@ class set { public: struct iterator { + %typemap(javaclassmodifiers) iterator "protected class" %extend { void incrementUnchecked() { ++(*$self); @@ -149,6 +150,11 @@ class set { } }; + typedef KeyType key_type; + typedef KeyType value_type; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + set(); set(const set&); diff --git a/Lib/java/std_unordered_map.i b/Lib/java/std_unordered_map.i index b0e8b3e40..405e0fba5 100644 --- a/Lib/java/std_unordered_map.i +++ b/Lib/java/std_unordered_map.i @@ -46,10 +46,10 @@ namespace std { -template class unordered_map { +template class unordered_map { -%typemap(javabase) std::unordered_map - "java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>" +%typemap(javabase) std::unordered_map + "java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>" %proxycode %{ @@ -65,7 +65,7 @@ template class unordered_map { return containsImpl(($typemap(jboxtype, KeyType))key); } - public $typemap(jboxtype, ValueType) get(Object key) { + public $typemap(jboxtype, MappedType) get(Object key) { if (!(key instanceof $typemap(jboxtype, KeyType))) { return null; } @@ -78,11 +78,11 @@ template class unordered_map { return null; } - public $typemap(jboxtype, ValueType) put($typemap(jboxtype, KeyType) key, - $typemap(jboxtype, ValueType) value) { + public $typemap(jboxtype, MappedType) put($typemap(jboxtype, KeyType) key, + $typemap(jboxtype, MappedType) value) { Iterator itr = find(($typemap(jboxtype, KeyType)) key); if (itr.isNot(end())) { - $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + $typemap(jboxtype, MappedType) oldValue = itr.getValue(); itr.setValue(value); return oldValue; } else { @@ -91,14 +91,14 @@ template class unordered_map { } } - public $typemap(jboxtype, ValueType) remove(Object key) { + public $typemap(jboxtype, MappedType) remove(Object key) { if (!(key instanceof $typemap(jboxtype, KeyType))) { return null; } Iterator itr = find(($typemap(jboxtype, KeyType)) key); if (itr.isNot(end())) { - $typemap(jboxtype, ValueType) oldValue = itr.getValue(); + $typemap(jboxtype, MappedType) oldValue = itr.getValue(); removeUnchecked(itr); return oldValue; } else { @@ -106,17 +106,17 @@ template class unordered_map { } } - public java.util.Set> entrySet() { - java.util.Set> setToReturn = - new java.util.HashSet>(); + public java.util.Set> entrySet() { + java.util.Set> setToReturn = + new java.util.HashSet>(); Iterator itr = begin(); final Iterator end = end(); while(itr.isNot(end)) { - setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>() { + setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>() { private Iterator iterator; - private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)> init(Iterator iterator) { + private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)> init(Iterator iterator) { this.iterator = iterator; return this; } @@ -125,12 +125,12 @@ template class unordered_map { return iterator.getKey(); } - public $typemap(jboxtype, ValueType) getValue() { + public $typemap(jboxtype, MappedType) getValue() { return iterator.getValue(); } - public $typemap(jboxtype, ValueType) setValue($typemap(jboxtype, ValueType) newValue) { - $typemap(jboxtype, ValueType) oldValue = iterator.getValue(); + public $typemap(jboxtype, MappedType) setValue($typemap(jboxtype, MappedType) newValue) { + $typemap(jboxtype, MappedType) oldValue = iterator.getValue(); iterator.setValue(newValue); return oldValue; } @@ -143,13 +143,19 @@ template class unordered_map { %} public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef KeyType key_type; + typedef MappedType mapped_type; + unordered_map(); - unordered_map(const unordered_map&); + unordered_map(const unordered_map&); struct iterator { + %typemap(javaclassmodifiers) iterator "protected class" %extend { - std::unordered_map::iterator getNextUnchecked() { - std::unordered_map::iterator copy = (*$self); + std::unordered_map::iterator getNextUnchecked() { + std::unordered_map::iterator copy = (*$self); return ++copy; } @@ -161,11 +167,11 @@ template class unordered_map { return (*$self)->first; } - ValueType getValue() const { + MappedType getValue() const { return (*$self)->second; } - void setValue(const ValueType& newValue) { + void setValue(const MappedType& newValue) { (*$self)->second = newValue; } } @@ -188,11 +194,11 @@ template class unordered_map { return (self->count(key) > 0); } - void putUnchecked(const KeyType& key, const ValueType& value) { + void putUnchecked(const KeyType& key, const MappedType& value) { (*self)[key] = value; } - void removeUnchecked(const std::unordered_map::iterator itr) { + void removeUnchecked(const std::unordered_map::iterator itr) { self->erase(itr); } } diff --git a/Lib/java/std_unordered_set.i b/Lib/java/std_unordered_set.i index ca9af0f19..b408c47de 100644 --- a/Lib/java/std_unordered_set.i +++ b/Lib/java/std_unordered_set.i @@ -134,6 +134,7 @@ class unordered_set { public: struct iterator { + %typemap(javaclassmodifiers) iterator "protected class" %extend { void incrementUnchecked() { ++(*$self); @@ -149,6 +150,11 @@ class unordered_set { } }; + typedef KeyType key_type; + typedef KeyType value_type; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + unordered_set(); unordered_set(const unordered_set&); From 437037a3e6ec0e64785ce084853ba406997466f3 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 11 Feb 2019 19:49:42 +0000 Subject: [PATCH 10/13] Replicate some cosmetic changes from std_map.i into std_set.i, std_unordered_map.i, std_unordered_set.i. --- Lib/java/std_map.i | 2 +- Lib/java/std_set.i | 46 ++++++++++++++++++------------------ Lib/java/std_unordered_map.i | 4 ++-- Lib/java/std_unordered_set.i | 46 ++++++++++++++++++------------------ 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/Lib/java/std_map.i b/Lib/java/std_map.i index 5b7517ad5..7df94b0b7 100644 --- a/Lib/java/std_map.i +++ b/Lib/java/std_map.i @@ -112,7 +112,7 @@ template Iterator itr = begin(); final Iterator end = end(); - while(itr.isNot(end)) { + while (itr.isNot(end)) { setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>() { private Iterator iterator; diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i index 6f557f627..020ab67f7 100644 --- a/Lib/java/std_set.i +++ b/Lib/java/std_set.i @@ -43,12 +43,12 @@ namespace std { -template +template class set { -%typemap(javabase) std::set "java.util.AbstractSet<$typemap(jboxtype, KeyType)>" +%typemap(javabase) std::set "java.util.AbstractSet<$typemap(jboxtype, T)>" %proxycode %{ - public $javaclassname(java.util.Collection collection) { + public $javaclassname(java.util.Collection collection) { this(); addAll(collection); } @@ -57,34 +57,34 @@ class set { return sizeImpl(); } - public boolean addAll(java.util.Collection collection) { + public boolean addAll(java.util.Collection collection) { boolean didAddElement = false; for (Object object : collection) { - didAddElement |= add(($typemap(jboxtype, KeyType))object); + didAddElement |= add(($typemap(jboxtype, T))object); } return didAddElement; } - public java.util.Iterator<$typemap(jboxtype, KeyType)> iterator() { - return new java.util.Iterator<$typemap(jboxtype, KeyType)>() { + public java.util.Iterator<$typemap(jboxtype, T)> iterator() { + return new java.util.Iterator<$typemap(jboxtype, T)>() { private Iterator curr; private Iterator end; - private java.util.Iterator<$typemap(jboxtype, KeyType)> init() { + private java.util.Iterator<$typemap(jboxtype, T)> init() { curr = $javaclassname.this.begin(); end = $javaclassname.this.end(); return this; } - public $typemap(jboxtype, KeyType) next() { + public $typemap(jboxtype, T) next() { if (!hasNext()) { throw new java.util.NoSuchElementException(); } // Save the current position, increment it, // then return the value at the position before the increment. - final $typemap(jboxtype, KeyType) currValue = curr.derefUnchecked(); + final $typemap(jboxtype, T) currValue = curr.derefUnchecked(); curr.incrementUnchecked(); return currValue; } @@ -106,11 +106,11 @@ class set { } public boolean contains(Object object) { - if (!(object instanceof $typemap(jboxtype, KeyType))) { + if (!(object instanceof $typemap(jboxtype, T))) { return false; } - return containsImpl(($typemap(jboxtype, KeyType))object); + return containsImpl(($typemap(jboxtype, T))object); } public boolean removeAll(java.util.Collection collection) { @@ -123,11 +123,11 @@ class set { } public boolean remove(Object object) { - if (!(object instanceof $typemap(jboxtype, KeyType))) { + if (!(object instanceof $typemap(jboxtype, T))) { return false; } - return removeImpl(($typemap(jboxtype, KeyType))object); + return removeImpl(($typemap(jboxtype, T))object); } %} @@ -140,23 +140,23 @@ class set { ++(*$self); } - KeyType derefUnchecked() const { + T derefUnchecked() const { return **$self; } - bool isNot(const iterator other) const { + bool isNot(iterator other) const { return (*$self != other); } } }; - typedef KeyType key_type; - typedef KeyType value_type; + typedef T key_type; + typedef T value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; set(); - set(const set&); + set(const set&); %rename(isEmpty) empty; bool empty() const; @@ -168,17 +168,17 @@ class set { %fragment("SWIG_SetSize"); // Returns whether item was inserted. - bool add(const KeyType& key) { + bool add(const T& key) { return self->insert(key).second; } // Returns whether set contains key. - bool containsImpl(const KeyType& key) { + bool containsImpl(const T& key) { return (self->count(key) > 0); } // Returns whether the item was erased. - bool removeImpl(const KeyType& key) { + bool removeImpl(const T& key) { return (self->erase(key) > 0); } @@ -192,4 +192,4 @@ class set { } }; -} // namespace std +} diff --git a/Lib/java/std_unordered_map.i b/Lib/java/std_unordered_map.i index 405e0fba5..3b926b0e6 100644 --- a/Lib/java/std_unordered_map.i +++ b/Lib/java/std_unordered_map.i @@ -112,7 +112,7 @@ template class unordered_map { Iterator itr = begin(); final Iterator end = end(); - while(itr.isNot(end)) { + while (itr.isNot(end)) { setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>() { private Iterator iterator; @@ -159,7 +159,7 @@ template class unordered_map { return ++copy; } - bool isNot(const iterator other) const { + bool isNot(iterator other) const { return (*$self != other); } diff --git a/Lib/java/std_unordered_set.i b/Lib/java/std_unordered_set.i index b408c47de..13d1fa4f9 100644 --- a/Lib/java/std_unordered_set.i +++ b/Lib/java/std_unordered_set.i @@ -43,12 +43,12 @@ namespace std { -template +template class unordered_set { -%typemap(javabase) std::unordered_set "java.util.AbstractSet<$typemap(jboxtype, KeyType)>" +%typemap(javabase) std::unordered_set "java.util.AbstractSet<$typemap(jboxtype, T)>" %proxycode %{ - public $javaclassname(java.util.Collection collection) { + public $javaclassname(java.util.Collection collection) { this(); addAll(collection); } @@ -57,34 +57,34 @@ class unordered_set { return sizeImpl(); } - public boolean addAll(java.util.Collection collection) { + public boolean addAll(java.util.Collection collection) { boolean didAddElement = false; for (Object object : collection) { - didAddElement |= add(($typemap(jboxtype, KeyType))object); + didAddElement |= add(($typemap(jboxtype, T))object); } return didAddElement; } - public java.util.Iterator<$typemap(jboxtype, KeyType)> iterator() { - return new java.util.Iterator<$typemap(jboxtype, KeyType)>() { + public java.util.Iterator<$typemap(jboxtype, T)> iterator() { + return new java.util.Iterator<$typemap(jboxtype, T)>() { private Iterator curr; private Iterator end; - private java.util.Iterator<$typemap(jboxtype, KeyType)> init() { + private java.util.Iterator<$typemap(jboxtype, T)> init() { curr = $javaclassname.this.begin(); end = $javaclassname.this.end(); return this; } - public $typemap(jboxtype, KeyType) next() { + public $typemap(jboxtype, T) next() { if (!hasNext()) { throw new java.util.NoSuchElementException(); } // Save the current position, increment it, // then return the value at the position before the increment. - final $typemap(jboxtype, KeyType) currValue = curr.derefUnchecked(); + final $typemap(jboxtype, T) currValue = curr.derefUnchecked(); curr.incrementUnchecked(); return currValue; } @@ -106,11 +106,11 @@ class unordered_set { } public boolean contains(Object object) { - if (!(object instanceof $typemap(jboxtype, KeyType))) { + if (!(object instanceof $typemap(jboxtype, T))) { return false; } - return containsImpl(($typemap(jboxtype, KeyType))object); + return containsImpl(($typemap(jboxtype, T))object); } public boolean removeAll(java.util.Collection collection) { @@ -123,11 +123,11 @@ class unordered_set { } public boolean remove(Object object) { - if (!(object instanceof $typemap(jboxtype, KeyType))) { + if (!(object instanceof $typemap(jboxtype, T))) { return false; } - return removeImpl(($typemap(jboxtype, KeyType))object); + return removeImpl(($typemap(jboxtype, T))object); } %} @@ -140,23 +140,23 @@ class unordered_set { ++(*$self); } - KeyType derefUnchecked() const { + T derefUnchecked() const { return **$self; } - bool isNot(const iterator other) const { + bool isNot(iterator other) const { return (*$self != other); } } }; - typedef KeyType key_type; - typedef KeyType value_type; + typedef T key_type; + typedef T value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; unordered_set(); - unordered_set(const unordered_set&); + unordered_set(const unordered_set&); %rename(isEmpty) empty; bool empty() const; @@ -168,17 +168,17 @@ class unordered_set { %fragment("SWIG_UnorderedSetSize"); // Returns whether item was inserted. - bool add(const KeyType& key) { + bool add(const T& key) { return self->insert(key).second; } // Returns whether set contains key. - bool containsImpl(const KeyType& key) { + bool containsImpl(const T& key) { return (self->count(key) > 0); } // Returns whether the item was erased. - bool removeImpl(const KeyType& key) { + bool removeImpl(const T& key) { return (self->erase(key) > 0); } @@ -192,4 +192,4 @@ class unordered_set { } }; -} // namespace std +} From 9849174d936b717b37e35b39011b6640b3d36901 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 11 Feb 2019 19:53:33 +0000 Subject: [PATCH 11/13] Nicer looking generated Java container code --- Lib/java/std_map.i | 3 +-- Lib/java/std_unordered_map.i | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/java/std_map.i b/Lib/java/std_map.i index 7df94b0b7..b177f9021 100644 --- a/Lib/java/std_map.i +++ b/Lib/java/std_map.i @@ -78,8 +78,7 @@ template return null; } - public $typemap(jboxtype, MappedType) put($typemap(jboxtype, KeyType) key, - $typemap(jboxtype, MappedType) value) { + public $typemap(jboxtype, MappedType) put($typemap(jboxtype, KeyType) key, $typemap(jboxtype, MappedType) value) { Iterator itr = find(($typemap(jboxtype, KeyType)) key); if (itr.isNot(end())) { $typemap(jboxtype, MappedType) oldValue = itr.getValue(); diff --git a/Lib/java/std_unordered_map.i b/Lib/java/std_unordered_map.i index 3b926b0e6..05b72f409 100644 --- a/Lib/java/std_unordered_map.i +++ b/Lib/java/std_unordered_map.i @@ -78,8 +78,7 @@ template class unordered_map { return null; } - public $typemap(jboxtype, MappedType) put($typemap(jboxtype, KeyType) key, - $typemap(jboxtype, MappedType) value) { + public $typemap(jboxtype, MappedType) put($typemap(jboxtype, KeyType) key, $typemap(jboxtype, MappedType) value) { Iterator itr = find(($typemap(jboxtype, KeyType)) key); if (itr.isNot(end())) { $typemap(jboxtype, MappedType) oldValue = itr.getValue(); From 82d163207140935919faaa73a7fffa0db1c5888e Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 12 Feb 2019 06:36:01 +0000 Subject: [PATCH 12/13] Combine duplicate unordered_set unordered_map testcases Merge cpp11_li_std_unordered_map into cpp11_std_unordered_map Merge cpp11_li_std_unordered_set into cpp11_std_unordered_set --- Examples/test-suite/cpp11_li_std_unordered_map.i | 11 ----------- Examples/test-suite/cpp11_li_std_unordered_set.i | 10 ---------- Examples/test-suite/cpp11_std_unordered_map.i | 2 ++ Examples/test-suite/cpp11_std_unordered_set.i | 2 ++ Examples/test-suite/java/Makefile.in | 4 ++-- ..._runme.java => cpp11_std_unordered_map_runme.java} | 10 +++++----- ..._runme.java => cpp11_std_unordered_set_runme.java} | 10 +++++----- 7 files changed, 16 insertions(+), 33 deletions(-) delete mode 100644 Examples/test-suite/cpp11_li_std_unordered_map.i delete mode 100644 Examples/test-suite/cpp11_li_std_unordered_set.i rename Examples/test-suite/java/{cpp11_li_std_unordered_map_runme.java => cpp11_std_unordered_map_runme.java} (92%) rename Examples/test-suite/java/{cpp11_li_std_unordered_set_runme.java => cpp11_std_unordered_set_runme.java} (89%) diff --git a/Examples/test-suite/cpp11_li_std_unordered_map.i b/Examples/test-suite/cpp11_li_std_unordered_map.i deleted file mode 100644 index 5f40e76fd..000000000 --- a/Examples/test-suite/cpp11_li_std_unordered_map.i +++ /dev/null @@ -1,11 +0,0 @@ -/* - * A test of unordered_map containers. - */ - -%module cpp11_li_std_unordered_map - -%include -%include - -%template(IntIntUnorderedMap) std::unordered_map; -%template(StringIntUnorderedMap) std::unordered_map; diff --git a/Examples/test-suite/cpp11_li_std_unordered_set.i b/Examples/test-suite/cpp11_li_std_unordered_set.i deleted file mode 100644 index e9711ce60..000000000 --- a/Examples/test-suite/cpp11_li_std_unordered_set.i +++ /dev/null @@ -1,10 +0,0 @@ -/* - * A test of unordered_set containers. - */ - -%module cpp11_li_std_unordered_set - -%include -%include - -%template(StringUnorderedSet) std::unordered_set; diff --git a/Examples/test-suite/cpp11_std_unordered_map.i b/Examples/test-suite/cpp11_std_unordered_map.i index 56c4a5248..b11d8f275 100644 --- a/Examples/test-suite/cpp11_std_unordered_map.i +++ b/Examples/test-suite/cpp11_std_unordered_map.i @@ -1,5 +1,7 @@ %module cpp11_std_unordered_map +%include %include %template(UnorderedMapIntInt) std::unordered_map; +%template(UnorderedMapStringInt) std::unordered_map; diff --git a/Examples/test-suite/cpp11_std_unordered_set.i b/Examples/test-suite/cpp11_std_unordered_set.i index f5652cb88..c2b8174bb 100644 --- a/Examples/test-suite/cpp11_std_unordered_set.i +++ b/Examples/test-suite/cpp11_std_unordered_set.i @@ -1,5 +1,7 @@ %module cpp11_std_unordered_set +%include %include %template(UnorderedSetInt) std::unordered_set; +%template(UnorderedSetString) std::unordered_set; diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index 3323f8a9c..2e788fa07 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -53,9 +53,9 @@ CPP11_TEST_CASES = \ cpp11_shared_ptr_nullptr_in_containers \ cpp11_shared_ptr_overload \ cpp11_shared_ptr_upcast \ + cpp11_std_unordered_map \ + cpp11_std_unordered_set \ cpp11_strongly_typed_enumerations_simple \ - cpp11_li_std_unordered_map \ - cpp11_li_std_unordered_set \ DOXYGEN_TEST_CASES := \ doxygen_parsing_enums_simple \ diff --git a/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java b/Examples/test-suite/java/cpp11_std_unordered_map_runme.java similarity index 92% rename from Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java rename to Examples/test-suite/java/cpp11_std_unordered_map_runme.java index bb85eeadd..79f683378 100644 --- a/Examples/test-suite/java/cpp11_li_std_unordered_map_runme.java +++ b/Examples/test-suite/java/cpp11_std_unordered_map_runme.java @@ -1,10 +1,10 @@ -import cpp11_li_std_unordered_map.*; +import cpp11_std_unordered_map.*; -public class cpp11_li_std_unordered_map_runme { +public class cpp11_std_unordered_map_runme { static { try { - System.loadLibrary("cpp11_li_std_unordered_map"); + System.loadLibrary("cpp11_std_unordered_map"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); System.exit(1); @@ -20,8 +20,8 @@ public class cpp11_li_std_unordered_map_runme { public static void main(String argv[]) throws Throwable { - java.util.AbstractMap sim = new StringIntUnorderedMap(); - java.util.AbstractMap iim = new IntIntUnorderedMap(); + java.util.AbstractMap sim = new UnorderedMapStringInt(); + java.util.AbstractMap iim = new UnorderedMapIntInt(); checkThat(sim.isEmpty()); checkThat(iim.isEmpty()); diff --git a/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java b/Examples/test-suite/java/cpp11_std_unordered_set_runme.java similarity index 89% rename from Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java rename to Examples/test-suite/java/cpp11_std_unordered_set_runme.java index dddf00307..9d64ab240 100644 --- a/Examples/test-suite/java/cpp11_li_std_unordered_set_runme.java +++ b/Examples/test-suite/java/cpp11_std_unordered_set_runme.java @@ -1,10 +1,10 @@ -import cpp11_li_std_unordered_set.*; +import cpp11_std_unordered_set.*; -public class cpp11_li_std_unordered_set_runme { +public class cpp11_std_unordered_set_runme { static { try { - System.loadLibrary("cpp11_li_std_unordered_set"); + System.loadLibrary("cpp11_std_unordered_set"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); System.exit(1); @@ -20,7 +20,7 @@ public class cpp11_li_std_unordered_set_runme { public static void main(String argv[]) throws Throwable { - java.util.AbstractSet ss = new StringUnorderedSet(); + java.util.AbstractSet ss = new UnorderedSetString(); checkThat(ss.isEmpty()); checkThat(!ss.contains("key")); @@ -63,7 +63,7 @@ public class cpp11_li_std_unordered_set_runme { checkThat(ss.containsAll(found)); checkThat(found.containsAll(ss)); - java.util.AbstractSet ss2 = new StringUnorderedSet(ss); + java.util.AbstractSet ss2 = new UnorderedSetString(ss); checkThat(ss2.containsAll(ss)); checkThat(ss.containsAll(ss2)); From e83e14a15e67205ba7d5b7bf32a0cababaf1569b Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 12 Feb 2019 07:15:51 +0000 Subject: [PATCH 13/13] Add missing typedefs to Java STL containers --- Lib/java/std_map.i | 5 +++++ Lib/java/std_set.i | 4 ++++ Lib/java/std_unordered_map.i | 5 +++++ Lib/java/std_unordered_set.i | 4 ++++ 4 files changed, 18 insertions(+) diff --git a/Lib/java/std_map.i b/Lib/java/std_map.i index b177f9021..e44a7e97a 100644 --- a/Lib/java/std_map.i +++ b/Lib/java/std_map.i @@ -146,7 +146,12 @@ template typedef ptrdiff_t difference_type; typedef KeyType key_type; typedef MappedType mapped_type; + typedef std::pair< const KeyType, MappedType > value_type; typedef Compare key_compare; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; map(); map(const map&); diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i index 020ab67f7..018d056cc 100644 --- a/Lib/java/std_set.i +++ b/Lib/java/std_set.i @@ -154,6 +154,10 @@ class set { typedef T value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; set(); set(const set&); diff --git a/Lib/java/std_unordered_map.i b/Lib/java/std_unordered_map.i index 05b72f409..86992479e 100644 --- a/Lib/java/std_unordered_map.i +++ b/Lib/java/std_unordered_map.i @@ -146,6 +146,11 @@ template class unordered_map { typedef ptrdiff_t difference_type; typedef KeyType key_type; typedef MappedType mapped_type; + typedef std::pair< const KeyType, MappedType > value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; unordered_map(); unordered_map(const unordered_map&); diff --git a/Lib/java/std_unordered_set.i b/Lib/java/std_unordered_set.i index 13d1fa4f9..3cbdca639 100644 --- a/Lib/java/std_unordered_set.i +++ b/Lib/java/std_unordered_set.i @@ -154,6 +154,10 @@ class unordered_set { typedef T value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; unordered_set(); unordered_set(const unordered_set&);