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()); + } } };