Merge branch 'bkotzz-add_set_map'

* bkotzz-add_set_map:
  Add missing typedefs to Java STL containers
  Combine duplicate unordered_set unordered_map testcases
  Nicer looking generated Java container code
  Replicate some cosmetic changes from std_map.i
  Legacy macros, protected iterator, typedefs
  Remove c++11 from stl.i
  Add to STL file as well
  Maps both working as java.util impls
  Mostly working for map
  Add set/unordered_set that extend AbstractSet
  Move unordered containers under cpp11_ prefix
  Add test cases to C++11 list
  Add unordered_{set|map} and set to Java
This commit is contained in:
William S Fulton 2019-02-12 18:09:38 +00:00
commit 136e6cfe2b
13 changed files with 1211 additions and 45 deletions

View file

@ -1,5 +1,7 @@
%module cpp11_std_unordered_map %module cpp11_std_unordered_map
%include <std_string.i>
%include <std_unordered_map.i> %include <std_unordered_map.i>
%template(UnorderedMapIntInt) std::unordered_map<int, int>; %template(UnorderedMapIntInt) std::unordered_map<int, int>;
%template(UnorderedMapStringInt) std::unordered_map<std::string, int>;

View file

@ -1,5 +1,7 @@
%module cpp11_std_unordered_set %module cpp11_std_unordered_set
%include <std_string.i>
%include <std_unordered_set.i> %include <std_unordered_set.i>
%template(UnorderedSetInt) std::unordered_set<int>; %template(UnorderedSetInt) std::unordered_set<int>;
%template(UnorderedSetString) std::unordered_set<std::string>;

View file

@ -44,6 +44,8 @@ CPP_TEST_CASES = \
java_typemaps_proxy \ java_typemaps_proxy \
java_typemaps_typewrapper \ java_typemaps_typewrapper \
li_std_list \ li_std_list \
li_std_map \
li_std_set \
# li_boost_intrusive_ptr # li_boost_intrusive_ptr
CPP11_TEST_CASES = \ CPP11_TEST_CASES = \
@ -51,6 +53,8 @@ CPP11_TEST_CASES = \
cpp11_shared_ptr_nullptr_in_containers \ cpp11_shared_ptr_nullptr_in_containers \
cpp11_shared_ptr_overload \ cpp11_shared_ptr_overload \
cpp11_shared_ptr_upcast \ cpp11_shared_ptr_upcast \
cpp11_std_unordered_map \
cpp11_std_unordered_set \
cpp11_strongly_typed_enumerations_simple \ cpp11_strongly_typed_enumerations_simple \
DOXYGEN_TEST_CASES := \ DOXYGEN_TEST_CASES := \

View file

@ -0,0 +1,122 @@
import cpp11_std_unordered_map.*;
public class cpp11_std_unordered_map_runme {
static {
try {
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);
}
}
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.AbstractMap<String, Integer> sim = new UnorderedMapStringInt();
java.util.AbstractMap<Integer, Integer> iim = new UnorderedMapIntInt();
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
checkThat(sim.size() == 0);
checkThat(iim.size() == 0);
checkThat(sim.get("key") == null);
checkThat(iim.get(1) == null);
checkThat(!sim.containsKey("key"));
checkThat(!iim.containsKey(1));
checkThat(sim.put("key", 2) == null);
checkThat(iim.put(1, 2) == null);
checkThat(sim.size() == 1);
checkThat(iim.size() == 1);
checkThat(!sim.isEmpty());
checkThat(!iim.isEmpty());
checkThat(sim.get("key") == 2);
checkThat(iim.get(1) == 2);
checkThat(sim.remove("key") == 2);
checkThat(iim.remove(1) == 2);
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
checkThat(sim.size() == 0);
checkThat(iim.size() == 0);
checkThat(sim.get("key") == null);
checkThat(iim.get(1) == null);
checkThat(sim.remove("key") == null);
checkThat(iim.remove(1) == null);
checkThat(sim.put("key", 2) == null);
checkThat(iim.put(1, 2) == null);
sim.clear();
iim.clear();
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
checkThat(sim.put("key1", 1) == null);
checkThat(iim.put(1, 1) == null);
checkThat(sim.put("key2", 2) == null);
checkThat(iim.put(2, 2) == null);
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);
checkThat(sim.put("key1", 3) == 1);
checkThat(iim.put(1, 3) == 1);
checkThat(sim.size() == 2);
checkThat(iim.size() == 2);
checkThat(sim.get("key1") == 3);
checkThat(iim.get(1) == 3);
java.util.Set<java.util.Map.Entry<String, Integer>> sim_es = sim.entrySet();
java.util.Map<String, Integer> sim_default = new java.util.HashMap<String, Integer>();
sim_default.put("key1", 3);
sim_default.put("key2", 2);
java.util.Set<java.util.Map.Entry<String, Integer>> sim_es_default = sim_default.entrySet();
checkThat(sim_es.size() == sim_es_default.size());
for (java.util.Map.Entry<String, Integer> entry : sim_es) {
checkThat(sim_es_default.contains(entry));
checkThat(sim_default.containsKey(entry.getKey()));
checkThat(sim_default.containsValue(entry.getValue()));
Integer oldValue = entry.getValue();
entry.setValue(oldValue + 1);
checkThat(sim.get(entry.getKey()) == (oldValue + 1));
}
java.util.Set<java.util.Map.Entry<Integer, Integer>> iim_es = iim.entrySet();
java.util.Map<Integer, Integer> iim_default = new java.util.HashMap<Integer, Integer>();
iim_default.put(1, 3);
iim_default.put(2, 2);
java.util.Set<java.util.Map.Entry<Integer, Integer>> iim_es_default = iim_default.entrySet();
checkThat(iim_es.size() == iim_es_default.size());
for (java.util.Map.Entry<Integer, Integer> 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));
}
}
}

View file

@ -0,0 +1,75 @@
import cpp11_std_unordered_set.*;
public class cpp11_std_unordered_set_runme {
static {
try {
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);
}
}
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<String> ss = new UnorderedSetString();
checkThat(ss.isEmpty());
checkThat(!ss.contains("key"));
checkThat(!ss.remove("key"));
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"));
checkThat(ss.add("key2"));
checkThat(ss.add("key3"));
checkThat(ss.size() == 3);
ss.clear();
checkThat(ss.isEmpty());
checkThat(ss.size() == 0);
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")));
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")));
java.util.Set<String> found = new java.util.HashSet<String>();
java.util.Iterator<String> itr = ss.iterator();
while (itr.hasNext()) {
found.add(itr.next());
}
checkThat(ss.containsAll(found));
checkThat(found.containsAll(ss));
java.util.AbstractSet<String> ss2 = new UnorderedSetString(ss);
checkThat(ss2.containsAll(ss));
checkThat(ss.containsAll(ss2));
checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")));
checkThat(ss.removeAll(found));
checkThat(ss.isEmpty());
checkThat(ss.size() == 0);
}
}

View file

@ -0,0 +1,122 @@
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 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.AbstractMap<String, Integer> sim = new StringIntMap();
java.util.AbstractMap<Integer, Integer> iim = new IntIntMap();
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
checkThat(sim.size() == 0);
checkThat(iim.size() == 0);
checkThat(sim.get("key") == null);
checkThat(iim.get(1) == null);
checkThat(!sim.containsKey("key"));
checkThat(!iim.containsKey(1));
checkThat(sim.put("key", 2) == null);
checkThat(iim.put(1, 2) == null);
checkThat(sim.size() == 1);
checkThat(iim.size() == 1);
checkThat(!sim.isEmpty());
checkThat(!iim.isEmpty());
checkThat(sim.get("key") == 2);
checkThat(iim.get(1) == 2);
checkThat(sim.remove("key") == 2);
checkThat(iim.remove(1) == 2);
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
checkThat(sim.size() == 0);
checkThat(iim.size() == 0);
checkThat(sim.get("key") == null);
checkThat(iim.get(1) == null);
checkThat(sim.remove("key") == null);
checkThat(iim.remove(1) == null);
checkThat(sim.put("key", 2) == null);
checkThat(iim.put(1, 2) == null);
sim.clear();
iim.clear();
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
checkThat(sim.put("key1", 1) == null);
checkThat(iim.put(1, 1) == null);
checkThat(sim.put("key2", 2) == null);
checkThat(iim.put(2, 2) == null);
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);
checkThat(sim.put("key1", 3) == 1);
checkThat(iim.put(1, 3) == 1);
checkThat(sim.size() == 2);
checkThat(iim.size() == 2);
checkThat(sim.get("key1") == 3);
checkThat(iim.get(1) == 3);
java.util.Set<java.util.Map.Entry<String, Integer>> sim_es = sim.entrySet();
java.util.Map<String, Integer> sim_default = new java.util.HashMap<String, Integer>();
sim_default.put("key1", 3);
sim_default.put("key2", 2);
java.util.Set<java.util.Map.Entry<String, Integer>> sim_es_default = sim_default.entrySet();
checkThat(sim_es.size() == sim_es_default.size());
for (java.util.Map.Entry<String, Integer> entry : sim_es) {
checkThat(sim_es_default.contains(entry));
checkThat(sim_default.containsKey(entry.getKey()));
checkThat(sim_default.containsValue(entry.getValue()));
Integer oldValue = entry.getValue();
entry.setValue(oldValue + 1);
checkThat(sim.get(entry.getKey()) == (oldValue + 1));
}
java.util.Set<java.util.Map.Entry<Integer, Integer>> iim_es = iim.entrySet();
java.util.Map<Integer, Integer> iim_default = new java.util.HashMap<Integer, Integer>();
iim_default.put(1, 3);
iim_default.put(2, 2);
java.util.Set<java.util.Map.Entry<Integer, Integer>> iim_es_default = iim_default.entrySet();
checkThat(iim_es.size() == iim_es_default.size());
for (java.util.Map.Entry<Integer, Integer> 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));
}
}
}

View file

@ -0,0 +1,75 @@
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 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<String> ss = new StringSet();
checkThat(ss.isEmpty());
checkThat(!ss.contains("key"));
checkThat(!ss.remove("key"));
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"));
checkThat(ss.add("key2"));
checkThat(ss.add("key3"));
checkThat(ss.size() == 3);
ss.clear();
checkThat(ss.isEmpty());
checkThat(ss.size() == 0);
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")));
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")));
java.util.Set<String> found = new java.util.HashSet<String>();
java.util.Iterator<String> itr = ss.iterator();
while (itr.hasNext()) {
found.add(itr.next());
}
checkThat(ss.containsAll(found));
checkThat(found.containsAll(ss));
java.util.AbstractSet<String> ss2 = new StringSet(ss);
checkThat(ss2.containsAll(ss));
checkThat(ss.containsAll(ss2));
checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")));
checkThat(ss.removeAll(found));
checkThat(ss.isEmpty());
checkThat(ss.size() == 0);
}
}

View file

@ -1,5 +1,5 @@
/* /*
* a test of set containers. * a test of set containers.
* Languages should define swig::LANGUAGE_OBJ to be * Languages should define swig::LANGUAGE_OBJ to be
* an entity of their native pointer type which can be * an entity of their native pointer type which can be
* included in a STL container. * included in a STL container.
@ -13,22 +13,23 @@
%include <std_string.i> %include <std_string.i>
%include <std_set.i> %include <std_set.i>
%include <std_multiset.i>
%include <std_vector.i> %include <std_vector.i>
%template(set_string) std::set<std::string>; // Use language macros since Java doesn't have multiset support (yet)
%template(set_int) std::multiset<int>; // and uses different naming conventions.
#if defined(SWIGRUBY) || defined(SWIGPYTHON)
%include <std_multiset.i>
%template(v_int) std::vector<int>; %template(set_int) std::multiset<int>;
%template(v_int) std::vector<int>;
%template(set_string) std::set<std::string>;
#elif defined(SWIGJAVA)
%template(StringSet) std::set<std::string>;
#endif
#if defined(SWIGRUBY) #if defined(SWIGRUBY)
%template(LanguageSet) std::set<swig::LANGUAGE_OBJ>; %template(LanguageSet) std::set<swig::LANGUAGE_OBJ>;
#endif #endif
#if defined(SWIGPYTHON) #if defined(SWIGPYTHON)
%template(pyset) std::set<swig::SwigPtr_PyObject>; %template(pyset) std::set<swig::SwigPtr_PyObject>;
#endif #endif

View file

@ -2,6 +2,8 @@
* std_map.i * std_map.i
* *
* SWIG typemaps for std::map * 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 <std_common.i> %include <std_common.i>
@ -12,48 +14,200 @@
%{ %{
#include <map> #include <map>
#include <algorithm>
#include <stdexcept> #include <stdexcept>
%} %}
%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::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 { namespace std {
template<class K, class T, class C = std::less<K> > class map { template<class KeyType, class MappedType, class Comparator = std::less<KeyType> > 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 > &);
unsigned int size() const; %typemap(javabase) std::map<KeyType, MappedType, Comparator>
bool empty() const; "java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>"
void clear();
%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, MappedType) 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, 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();
itr.setValue(value);
return oldValue;
} else {
putUnchecked(key, value);
return null;
}
}
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, MappedType) oldValue = itr.getValue();
removeUnchecked(itr);
return oldValue;
} else {
return null;
}
}
public java.util.Set<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>> entrySet() {
java.util.Set<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>> setToReturn =
new java.util.HashSet<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>>();
Iterator itr = begin();
final Iterator end = end();
while (itr.isNot(end)) {
setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>() {
private Iterator iterator;
private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)> init(Iterator iterator) {
this.iterator = iterator;
return this;
}
public $typemap(jboxtype, KeyType) getKey() {
return iterator.getKey();
}
public $typemap(jboxtype, MappedType) getValue() {
return iterator.getValue();
}
public $typemap(jboxtype, MappedType) setValue($typemap(jboxtype, MappedType) newValue) {
$typemap(jboxtype, MappedType) oldValue = iterator.getValue();
iterator.setValue(newValue);
return oldValue;
}
}.init(itr));
itr = itr.getNextUnchecked();
}
return setToReturn;
}
%}
public:
typedef size_t size_type;
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<KeyType, MappedType, Comparator >&);
struct iterator {
%typemap(javaclassmodifiers) iterator "protected class"
%extend { %extend {
const T& get(const K& key) throw (std::out_of_range) { std::map<KeyType, MappedType, Comparator >::iterator getNextUnchecked() {
std::map< K, T, C >::iterator i = self->find(key); std::map<KeyType, MappedType, Comparator >::iterator copy = (*$self);
if (i != self->end()) return ++copy;
return i->second;
else
throw std::out_of_range("key not found");
} }
void set(const K& key, const T& x) {
(*self)[key] = x; bool isNot(iterator other) const {
return (*$self != other);
} }
void del(const K& key) throw (std::out_of_range) {
std::map< K, T, C >::iterator i = self->find(key); KeyType getKey() const {
if (i != self->end()) return (*$self)->first;
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); MappedType getValue() const {
return i != self->end(); return (*$self)->second;
}
void setValue(const MappedType& newValue) {
(*$self)->second = newValue;
} }
} }
}; };
%rename(isEmpty) empty;
bool empty() const;
void clear();
iterator find(const KeyType&);
iterator begin();
iterator end();
%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);
}
void putUnchecked(const KeyType& key, const MappedType& value) {
(*self)[key] = value;
}
void removeUnchecked(const std::map<KeyType, MappedType, Comparator >::iterator itr) {
self->erase(itr);
}
}
};
// Legacy macros (deprecated) // Legacy macros (deprecated)
%define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)

199
Lib/java/std_set.i Normal file
View file

@ -0,0 +1,199 @@
/* -----------------------------------------------------------------------------
* 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 <std_common.i>
// ------------------------------------------------------------------------
// std::set
// ------------------------------------------------------------------------
%{
#include <set>
#include <stdexcept>
%}
%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 T>
class set {
%typemap(javabase) std::set<T> "java.util.AbstractSet<$typemap(jboxtype, T)>"
%proxycode %{
public $javaclassname(java.util.Collection<? extends $typemap(jboxtype, T)> collection) {
this();
addAll(collection);
}
public int size() {
return sizeImpl();
}
public boolean addAll(java.util.Collection<? extends $typemap(jboxtype, T)> collection) {
boolean didAddElement = false;
for (Object object : collection) {
didAddElement |= add(($typemap(jboxtype, T))object);
}
return didAddElement;
}
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, T)> init() {
curr = $javaclassname.this.begin();
end = $javaclassname.this.end();
return this;
}
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, T) 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, T))) {
return false;
}
return containsImpl(($typemap(jboxtype, T))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, T))) {
return false;
}
return removeImpl(($typemap(jboxtype, T))object);
}
%}
public:
struct iterator {
%typemap(javaclassmodifiers) iterator "protected class"
%extend {
void incrementUnchecked() {
++(*$self);
}
T derefUnchecked() const {
return **$self;
}
bool isNot(iterator other) const {
return (*$self != other);
}
}
};
typedef T key_type;
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<T>&);
%rename(isEmpty) empty;
bool empty() const;
void clear();
iterator begin();
iterator end();
%extend {
%fragment("SWIG_SetSize");
// Returns whether item was inserted.
bool add(const T& key) {
return self->insert(key).second;
}
// Returns whether set contains key.
bool containsImpl(const T& key) {
return (self->count(key) > 0);
}
// Returns whether the item was erased.
bool removeImpl(const T& 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());
}
}
};
}

View file

@ -0,0 +1,211 @@
/* -----------------------------------------------------------------------------
* 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 <std_common.i>
// ------------------------------------------------------------------------
// std::unordered_map
// ------------------------------------------------------------------------
%{
#include <unordered_map>
#include <stdexcept>
%}
%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 KeyType, class MappedType > class unordered_map {
%typemap(javabase) std::unordered_map<KeyType, MappedType>
"java.util.AbstractMap<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>"
%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, MappedType) 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, 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();
itr.setValue(value);
return oldValue;
} else {
putUnchecked(key, value);
return null;
}
}
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, MappedType) oldValue = itr.getValue();
removeUnchecked(itr);
return oldValue;
} else {
return null;
}
}
public java.util.Set<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>> entrySet() {
java.util.Set<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>> setToReturn =
new java.util.HashSet<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>>();
Iterator itr = begin();
final Iterator end = end();
while (itr.isNot(end)) {
setToReturn.add(new Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)>() {
private Iterator iterator;
private Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, MappedType)> init(Iterator iterator) {
this.iterator = iterator;
return this;
}
public $typemap(jboxtype, KeyType) getKey() {
return iterator.getKey();
}
public $typemap(jboxtype, MappedType) getValue() {
return iterator.getValue();
}
public $typemap(jboxtype, MappedType) setValue($typemap(jboxtype, MappedType) newValue) {
$typemap(jboxtype, MappedType) oldValue = iterator.getValue();
iterator.setValue(newValue);
return oldValue;
}
}.init(itr));
itr = itr.getNextUnchecked();
}
return setToReturn;
}
%}
public:
typedef size_t size_type;
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<KeyType, MappedType >&);
struct iterator {
%typemap(javaclassmodifiers) iterator "protected class"
%extend {
std::unordered_map<KeyType, MappedType >::iterator getNextUnchecked() {
std::unordered_map<KeyType, MappedType >::iterator copy = (*$self);
return ++copy;
}
bool isNot(iterator other) const {
return (*$self != other);
}
KeyType getKey() const {
return (*$self)->first;
}
MappedType getValue() const {
return (*$self)->second;
}
void setValue(const MappedType& newValue) {
(*$self)->second = newValue;
}
}
};
%rename(isEmpty) empty;
bool empty() const;
void clear();
iterator find(const KeyType&);
iterator begin();
iterator end();
%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);
}
void putUnchecked(const KeyType& key, const MappedType& value) {
(*self)[key] = value;
}
void removeUnchecked(const std::unordered_map<KeyType, MappedType >::iterator itr) {
self->erase(itr);
}
}
};
}

View file

@ -0,0 +1,199 @@
/* -----------------------------------------------------------------------------
* 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 <std_common.i>
// ------------------------------------------------------------------------
// std::unordered_set
// ------------------------------------------------------------------------
%{
#include <unordered_set>
#include <stdexcept>
%}
%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 T>
class unordered_set {
%typemap(javabase) std::unordered_set<T> "java.util.AbstractSet<$typemap(jboxtype, T)>"
%proxycode %{
public $javaclassname(java.util.Collection<? extends $typemap(jboxtype, T)> collection) {
this();
addAll(collection);
}
public int size() {
return sizeImpl();
}
public boolean addAll(java.util.Collection<? extends $typemap(jboxtype, T)> collection) {
boolean didAddElement = false;
for (Object object : collection) {
didAddElement |= add(($typemap(jboxtype, T))object);
}
return didAddElement;
}
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, T)> init() {
curr = $javaclassname.this.begin();
end = $javaclassname.this.end();
return this;
}
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, T) 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, T))) {
return false;
}
return containsImpl(($typemap(jboxtype, T))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, T))) {
return false;
}
return removeImpl(($typemap(jboxtype, T))object);
}
%}
public:
struct iterator {
%typemap(javaclassmodifiers) iterator "protected class"
%extend {
void incrementUnchecked() {
++(*$self);
}
T derefUnchecked() const {
return **$self;
}
bool isNot(iterator other) const {
return (*$self != other);
}
}
};
typedef T key_type;
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<T>&);
%rename(isEmpty) empty;
bool empty() const;
void clear();
iterator begin();
iterator end();
%extend {
%fragment("SWIG_UnorderedSetSize");
// Returns whether item was inserted.
bool add(const T& key) {
return self->insert(key).second;
}
// Returns whether set contains key.
bool containsImpl(const T& key) {
return (self->count(key) > 0);
}
// Returns whether the item was erased.
bool removeImpl(const T& 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());
}
}
};
}

View file

@ -3,8 +3,8 @@
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <std_common.i> %include <std_common.i>
%include <std_string.i>
%include <std_vector.i>
%include <std_map.i> %include <std_map.i>
%include <std_pair.i> %include <std_pair.i>
%include <std_set.i>
%include <std_string.i>
%include <std_vector.i>