Maps both working as java.util impls

This commit is contained in:
Brad Kotsopoulos 2018-12-27 01:11:54 -05:00
commit 573ddb061e
6 changed files with 514 additions and 285 deletions

View file

@ -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 public static void main(String argv[]) throws Throwable
{ {
StringIntUnorderedMap sim = new StringIntUnorderedMap(); java.util.AbstractMap<String, Integer> sim = new StringIntUnorderedMap();
IntIntUnorderedMap iim = new IntIntUnorderedMap(); java.util.AbstractMap<Integer, Integer> iim = new IntIntUnorderedMap();
if (!sim.empty()) throw new RuntimeException("Test (1) failed"); checkThat(sim.isEmpty());
if (!iim.empty()) throw new RuntimeException("Test (2) failed"); checkThat(iim.isEmpty());
checkThat(sim.size() == 0);
checkThat(iim.size() == 0);
if (sim.size() != 0) throw new RuntimeException("Test (3) failed"); checkThat(sim.get("key") == null);
if (iim.size() != 0) throw new RuntimeException("Test (4) failed"); checkThat(iim.get(1) == null);
try { checkThat(!sim.containsKey("key"));
sim.get("key"); checkThat(!iim.containsKey(1));
throw new RuntimeException("Test (5) failed");
} catch (IndexOutOfBoundsException e) {
}
try { checkThat(sim.put("key", 2) == null);
iim.get(1); checkThat(iim.put(1, 2) == null);
throw new RuntimeException("Test (6) failed");
} catch (IndexOutOfBoundsException e) {
}
sim.set("key", 1); checkThat(sim.size() == 1);
iim.set(1, 1); checkThat(iim.size() == 1);
checkThat(!sim.isEmpty());
checkThat(!iim.isEmpty());
if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); checkThat(sim.get("key") == 2);
if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); checkThat(iim.get(1) == 2);
sim.del("key"); checkThat(sim.remove("key") == 2);
iim.del(1); checkThat(iim.remove(1) == 2);
if (sim.has_key("key")) throw new RuntimeException("Test (9) failed"); checkThat(sim.isEmpty());
if (iim.has_key(1)) throw new RuntimeException("Test (10) failed"); checkThat(iim.isEmpty());
checkThat(sim.size() == 0);
checkThat(iim.size() == 0);
if (!sim.empty()) throw new RuntimeException("Test (11) failed"); checkThat(sim.get("key") == null);
if (!iim.empty()) throw new RuntimeException("Test (12) failed"); checkThat(iim.get(1) == null);
if (sim.size() != 0) throw new RuntimeException("Test (13) failed");
if (iim.size() != 0) throw new RuntimeException("Test (14) failed");
try { checkThat(sim.remove("key") == null);
sim.del("key"); checkThat(iim.remove(1) == null);
throw new RuntimeException("Test (15) failed");
} catch (IndexOutOfBoundsException e) {
}
try { checkThat(sim.put("key", 2) == null);
iim.del(1); checkThat(iim.put(1, 2) == null);
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(); sim.clear();
iim.clear(); iim.clear();
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
if (sim.has_key("key")) throw new RuntimeException("Test (19) failed"); checkThat(sim.put("key1", 1) == null);
if (iim.has_key(1)) throw new RuntimeException("Test (20) failed"); 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"); checkThat(sim.size() == 2);
if (!iim.empty()) throw new RuntimeException("Test (22) failed"); checkThat(iim.size() == 2);
if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); checkThat(sim.get("key1") == 1);
if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); checkThat(iim.get(1) == 1);
checkThat(sim.get("key2") == 2);
checkThat(iim.get(2) == 2);
sim.set("key", 1); checkThat(sim.put("key1", 3) == 1);
sim.set("key2", 2); checkThat(iim.put(1, 3) == 1);
iim.set(1, 1);
iim.set(2, 2);
if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); checkThat(sim.size() == 2);
if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); checkThat(iim.size() == 2);
if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); checkThat(sim.get("key1") == 3);
if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); checkThat(iim.get(1) == 3);
sim.set("key", 3); java.util.Set<java.util.Map.Entry<String, Integer>> sim_es = sim.entrySet();
iim.set(1, 3); 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()));
if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); Integer oldValue = entry.getValue();
if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); entry.setValue(oldValue + 1);
if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); checkThat(sim.get(entry.getKey()) == (oldValue + 1));
if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); }
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

@ -11,49 +11,48 @@ public class cpp11_li_std_unordered_set_runme {
} }
} }
public static void failTest(int testNum) throws Throwable { public static void checkThat(boolean mustBeTrue) throws Throwable {
throw new RuntimeException("Test failed: " + testNum); 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 checkThat(boolean mustBeTrue, int testNum) throws Throwable { }
if (!mustBeTrue) failTest(testNum);
} }
public static void main(String argv[]) throws Throwable public static void main(String argv[]) throws Throwable
{ {
java.util.AbstractSet<String> ss = new StringUnorderedSet(); java.util.AbstractSet<String> ss = new StringUnorderedSet();
checkThat(ss.isEmpty(), 1); checkThat(ss.isEmpty());
checkThat(!ss.contains("key"), 2); checkThat(!ss.contains("key"));
checkThat(!ss.remove("key"), 3); checkThat(!ss.remove("key"));
checkThat(ss.add("key"), 4); checkThat(ss.add("key"));
checkThat(!ss.add("key"), 5); checkThat(!ss.add("key"));
checkThat(ss.contains("key"), 6); checkThat(ss.contains("key"));
checkThat(ss.remove("key"), 7); checkThat(ss.remove("key"));
checkThat(ss.isEmpty(), 8); checkThat(ss.isEmpty());
checkThat(ss.size() == 0, 9); checkThat(ss.size() == 0);
checkThat(ss.add("key1"), 10); checkThat(ss.add("key1"));
checkThat(ss.add("key2"), 11); checkThat(ss.add("key2"));
checkThat(ss.add("key3"), 12); checkThat(ss.add("key3"));
checkThat(ss.size() == 3, 13); checkThat(ss.size() == 3);
ss.clear(); ss.clear();
checkThat(ss.isEmpty(), 14); checkThat(ss.isEmpty());
checkThat(ss.size() == 0, 15); checkThat(ss.size() == 0);
checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")), 16); checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")));
checkThat(ss.size() == 3, 17); checkThat(ss.size() == 3);
checkThat(ss.contains("one"), 18); checkThat(ss.contains("one"));
checkThat(!ss.contains("four"), 19); checkThat(!ss.contains("four"));
checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")), 20); checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")));
checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")), 21); checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")));
checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")), 22); checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")));
checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four")), 23); 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<String> found = new java.util.HashSet<String>(); java.util.Set<String> found = new java.util.HashSet<String>();
java.util.Iterator<String> itr = ss.iterator(); java.util.Iterator<String> itr = ss.iterator();
@ -61,16 +60,16 @@ public class cpp11_li_std_unordered_set_runme {
found.add(itr.next()); found.add(itr.next());
} }
checkThat(ss.containsAll(found), 25); checkThat(ss.containsAll(found));
checkThat(found.containsAll(ss), 26); checkThat(found.containsAll(ss));
java.util.AbstractSet<String> ss2 = new StringUnorderedSet(ss); java.util.AbstractSet<String> ss2 = new StringUnorderedSet(ss);
checkThat(ss2.containsAll(ss), 27); checkThat(ss2.containsAll(ss));
checkThat(ss.containsAll(ss2), 28); checkThat(ss.containsAll(ss2));
checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")), 29); checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")));
checkThat(ss.removeAll(found), 30); checkThat(ss.removeAll(found));
checkThat(ss.isEmpty(), 31); checkThat(ss.isEmpty());
checkThat(ss.size() == 0, 32); checkThat(ss.size() == 0);
} }
} }

View file

@ -11,12 +11,11 @@ public class li_std_map_runme {
} }
} }
public static void failTest(int testNum) throws Throwable { public static void checkThat(boolean mustBeTrue) throws Throwable {
throw new RuntimeException("Test failed: " + testNum); 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 checkThat(boolean mustBeTrue, int testNum) throws Throwable { }
if (!mustBeTrue) failTest(testNum);
} }
public static void main(String argv[]) throws Throwable public static void main(String argv[]) throws Throwable
@ -24,79 +23,100 @@ public class li_std_map_runme {
java.util.AbstractMap<String, Integer> sim = new StringIntMap(); java.util.AbstractMap<String, Integer> sim = new StringIntMap();
java.util.AbstractMap<Integer, Integer> iim = new IntIntMap(); java.util.AbstractMap<Integer, Integer> iim = new IntIntMap();
checkThat(sim.isEmpty(), 1); checkThat(sim.isEmpty());
checkThat(iim.isEmpty(), 2); checkThat(iim.isEmpty());
checkThat(sim.size() == 0, 3); checkThat(sim.size() == 0);
checkThat(iim.size() == 0, 4); checkThat(iim.size() == 0);
checkThat(sim.get("key") == null, 5); checkThat(sim.get("key") == null);
checkThat(iim.get(1) == null, 6); checkThat(iim.get(1) == null);
checkThat(!sim.containsKey("key"), 7); checkThat(!sim.containsKey("key"));
checkThat(!iim.containsKey(1), 8); checkThat(!iim.containsKey(1));
checkThat(sim.put("key", 2) == null, 9); checkThat(sim.put("key", 2) == null);
checkThat(iim.put(1, 2) == null, 10); checkThat(iim.put(1, 2) == null);
// if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); checkThat(sim.size() == 1);
// if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); checkThat(iim.size() == 1);
checkThat(!sim.isEmpty());
checkThat(!iim.isEmpty());
// sim.remove("key"); checkThat(sim.get("key") == 2);
// iim.remove(1); checkThat(iim.get(1) == 2);
// if (sim.containsKey("key")) throw new RuntimeException("Test (9) failed"); checkThat(sim.remove("key") == 2);
// if (iim.containsKey(1)) throw new RuntimeException("Test (10) failed"); checkThat(iim.remove(1) == 2);
// if (!sim.isEmpty()) throw new RuntimeException("Test (11) failed"); checkThat(sim.isEmpty());
// if (!iim.isEmpty()) throw new RuntimeException("Test (12) failed"); checkThat(iim.isEmpty());
// if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); checkThat(sim.size() == 0);
// if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); checkThat(iim.size() == 0);
// try { checkThat(sim.get("key") == null);
// sim.remove("key"); checkThat(iim.get(1) == null);
// throw new RuntimeException("Test (15) failed");
// } catch (IndexOutOfBoundsException e) {
// }
// try { checkThat(sim.remove("key") == null);
// iim.remove(1); checkThat(iim.remove(1) == null);
// throw new RuntimeException("Test (16) failed");
// } catch (IndexOutOfBoundsException e) {
// }
// sim.put("key", 1); checkThat(sim.put("key", 2) == null);
// iim.put(1, 1); checkThat(iim.put(1, 2) == null);
// if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); sim.clear();
// if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); iim.clear();
checkThat(sim.isEmpty());
checkThat(iim.isEmpty());
// sim.clear(); checkThat(sim.put("key1", 1) == null);
// iim.clear(); 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"); checkThat(sim.size() == 2);
// if (iim.containsKey(1)) throw new RuntimeException("Test (20) failed"); 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"); checkThat(sim.put("key1", 3) == 1);
// if (!iim.isEmpty()) throw new RuntimeException("Test (22) failed"); checkThat(iim.put(1, 3) == 1);
// if (sim.size() != 0) throw new RuntimeException("Test (23) failed");
// if (iim.size() != 0) throw new RuntimeException("Test (24) failed");
// sim.put("key", 1); checkThat(sim.size() == 2);
// sim.put("key2", 2); checkThat(iim.size() == 2);
// iim.put(1, 1); checkThat(sim.get("key1") == 3);
// iim.put(2, 2); checkThat(iim.get(1) == 3);
// if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); java.util.Set<java.util.Map.Entry<String, Integer>> sim_es = sim.entrySet();
// if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); java.util.Map<String, Integer> sim_default = new java.util.HashMap<String, Integer>();
// if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); sim_default.put("key1", 3);
// if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); 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()));
// sim.put("key", 3); Integer oldValue = entry.getValue();
// iim.put(1, 3); entry.setValue(oldValue + 1);
checkThat(sim.get(entry.getKey()) == (oldValue + 1));
}
// if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); java.util.Set<java.util.Map.Entry<Integer, Integer>> iim_es = iim.entrySet();
// if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); java.util.Map<Integer, Integer> iim_default = new java.util.HashMap<Integer, Integer>();
// if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); iim_default.put(1, 3);
// if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); 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

@ -11,49 +11,48 @@ public class li_std_set_runme {
} }
} }
public static void failTest(int testNum) throws Throwable { public static void checkThat(boolean mustBeTrue) throws Throwable {
throw new RuntimeException("Test failed: " + testNum); 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 checkThat(boolean mustBeTrue, int testNum) throws Throwable { }
if (!mustBeTrue) failTest(testNum);
} }
public static void main(String argv[]) throws Throwable public static void main(String argv[]) throws Throwable
{ {
java.util.AbstractSet<String> ss = new StringSet(); java.util.AbstractSet<String> ss = new StringSet();
checkThat(ss.isEmpty(), 1); checkThat(ss.isEmpty());
checkThat(!ss.contains("key"), 2); checkThat(!ss.contains("key"));
checkThat(!ss.remove("key"), 3); checkThat(!ss.remove("key"));
checkThat(ss.add("key"), 4); checkThat(ss.add("key"));
checkThat(!ss.add("key"), 5); checkThat(!ss.add("key"));
checkThat(ss.contains("key"), 6); checkThat(ss.contains("key"));
checkThat(ss.remove("key"), 7); checkThat(ss.remove("key"));
checkThat(ss.isEmpty(), 8); checkThat(ss.isEmpty());
checkThat(ss.size() == 0, 9); checkThat(ss.size() == 0);
checkThat(ss.add("key1"), 10); checkThat(ss.add("key1"));
checkThat(ss.add("key2"), 11); checkThat(ss.add("key2"));
checkThat(ss.add("key3"), 12); checkThat(ss.add("key3"));
checkThat(ss.size() == 3, 13); checkThat(ss.size() == 3);
ss.clear(); ss.clear();
checkThat(ss.isEmpty(), 14); checkThat(ss.isEmpty());
checkThat(ss.size() == 0, 15); checkThat(ss.size() == 0);
checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")), 16); checkThat(ss.addAll(java.util.Arrays.asList("one", "two", "three")));
checkThat(ss.size() == 3, 17); checkThat(ss.size() == 3);
checkThat(ss.contains("one"), 18); checkThat(ss.contains("one"));
checkThat(!ss.contains("four"), 19); checkThat(!ss.contains("four"));
checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")), 20); checkThat(ss.containsAll(java.util.Arrays.asList("one", "two", "three")));
checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")), 21); checkThat(ss.containsAll(java.util.Arrays.asList("one", "two")));
checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")), 22); checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "four")));
checkThat(!ss.containsAll(java.util.Arrays.asList("one", "two", "three", "four")), 23); 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<String> found = new java.util.HashSet<String>(); java.util.Set<String> found = new java.util.HashSet<String>();
java.util.Iterator<String> itr = ss.iterator(); java.util.Iterator<String> itr = ss.iterator();
@ -61,16 +60,16 @@ public class li_std_set_runme {
found.add(itr.next()); found.add(itr.next());
} }
checkThat(ss.containsAll(found), 25); checkThat(ss.containsAll(found));
checkThat(found.containsAll(ss), 26); checkThat(found.containsAll(ss));
java.util.AbstractSet<String> ss2 = new StringSet(ss); java.util.AbstractSet<String> ss2 = new StringSet(ss);
checkThat(ss2.containsAll(ss), 27); checkThat(ss2.containsAll(ss));
checkThat(ss.containsAll(ss2), 28); checkThat(ss.containsAll(ss2));
checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")), 29); checkThat(!ss.removeAll(java.util.Arrays.asList("five", "four")));
checkThat(ss.removeAll(found), 30); checkThat(ss.removeAll(found));
checkThat(ss.isEmpty(), 31); checkThat(ss.isEmpty());
checkThat(ss.size() == 0, 32); checkThat(ss.size() == 0);
} }
} }

View file

@ -7,7 +7,6 @@
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <std_common.i> %include <std_common.i>
%include <std_set.i>
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// std::map // std::map
@ -31,9 +30,19 @@
%javamethodmodifiers std::map::sizeImpl "private"; %javamethodmodifiers std::map::sizeImpl "private";
%javamethodmodifiers std::map::containsImpl "private"; %javamethodmodifiers std::map::containsImpl "private";
%javamethodmodifiers std::map::getImpl "private"; %javamethodmodifiers std::map::putUnchecked "private";
%javamethodmodifiers std::map::putImpl "private"; %javamethodmodifiers std::map::removeUnchecked "private";
%javamethodmodifiers std::map::removeImpl "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 {
@ -48,47 +57,88 @@ template<class KeyType, class ValueType, class Comparator = std::less<KeyType> >
return sizeImpl(); return sizeImpl();
} }
public boolean containsKey(Object object) { public boolean containsKey(Object key) {
if (!(object instanceof $typemap(jboxtype, KeyType))) { if (!(key instanceof $typemap(jboxtype, KeyType))) {
return false; return false;
} }
return containsImpl(($typemap(jboxtype, KeyType))object); return containsImpl(($typemap(jboxtype, KeyType))key);
} }
public $typemap(jboxtype, ValueType) get(Object object) { public $typemap(jboxtype, ValueType) get(Object key) {
if (!(object instanceof $typemap(jboxtype, KeyType))) { if (!(key instanceof $typemap(jboxtype, KeyType))) {
return null; return null;
} }
try { Iterator itr = find(($typemap(jboxtype, KeyType)) key);
getImpl(($typemap(jboxtype, KeyType)) object); if (itr.isNot(end())) {
} catch (IndexOutOfBoundsException e) {} return itr.getValue();
}
return null; return null;
} }
public $typemap(jboxtype, ValueType) put($typemap(jboxtype, KeyType) key, public $typemap(jboxtype, ValueType) put($typemap(jboxtype, KeyType) key,
$typemap(jboxtype, ValueType) value) { $typemap(jboxtype, ValueType) value) {
try { Iterator itr = find(($typemap(jboxtype, KeyType)) key);
$typemap(jboxtype, ValueType) oldValue = putImpl(key, value); if (itr.isNot(end())) {
$typemap(jboxtype, ValueType) oldValue = itr.getValue();
itr.setValue(value);
return oldValue; return oldValue;
} catch (IndexOutOfBoundsException e) {} } else {
putUnchecked(key, value);
return null; return null;
}
} }
public $typemap(jboxtype, ValueType) remove($typemap(jboxtype, KeyType) key) { public $typemap(jboxtype, ValueType) remove(Object key) {
try { if (!(key instanceof $typemap(jboxtype, KeyType))) {
$typemap(jboxtype, ValueType) oldValue = removeImpl(key); return null;
return oldValue; }
} catch (IndexOutOfBoundsException e) {}
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<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>> entrySet() { public java.util.Set<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>> entrySet() {
throw new RuntimeException("Stub"); java.util.Set<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>> setToReturn =
new java.util.HashSet<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>>();
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<class KeyType, class ValueType, class Comparator = std::less<KeyType> >
map(); map();
map(const map<KeyType, ValueType, Comparator >&); map(const map<KeyType, ValueType, Comparator >&);
struct iterator {
%extend {
std::map<KeyType, ValueType, Comparator >::iterator getNextUnchecked() {
std::map<KeyType, ValueType, Comparator >::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; %rename(isEmpty) empty;
bool empty() const; bool empty() const;
void clear(); void clear();
iterator find(const KeyType&);
iterator begin();
iterator end();
%extend { %extend {
%fragment("SWIG_MapSize"); %fragment("SWIG_MapSize");
@ -110,36 +188,12 @@ template<class KeyType, class ValueType, class Comparator = std::less<KeyType> >
return (self->count(key) > 0); return (self->count(key) > 0);
} }
const ValueType& getImpl(const KeyType& key) throw (std::out_of_range) { void putUnchecked(const KeyType& key, const ValueType& value) {
std::map<KeyType, ValueType, Comparator >::iterator itr = self->find(key); (*self)[key] = value;
if (itr != self->end()) {
return itr->second;
} else {
throw std::out_of_range("map::get() - key not found");
}
} }
ValueType putImpl(const KeyType& key, const ValueType& value) { void removeUnchecked(const std::map<KeyType, ValueType, Comparator >::iterator itr) {
std::map<KeyType, ValueType, Comparator >::iterator itr = self->find(key); self->erase(itr);
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<KeyType, ValueType, Comparator >::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");
}
} }
} }
}; };

View file

@ -2,6 +2,8 @@
* std_unordered_map.i * std_unordered_map.i
* *
* SWIG typemaps for std::unordered_map * 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> %include <std_common.i>
@ -15,51 +17,185 @@
#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::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 { namespace std {
template<class KeyType, class MappedType> class unordered_map { template<class KeyType, class ValueType > 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<KeyType, MappedType>&);
unsigned int size() const; %typemap(javabase) std::unordered_map<KeyType, ValueType>
"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<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>> entrySet() {
java.util.Set<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>> setToReturn =
new java.util.HashSet<Entry<$typemap(jboxtype, KeyType), $typemap(jboxtype, ValueType)>>();
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<KeyType, ValueType >&);
struct iterator {
%extend {
std::unordered_map<KeyType, ValueType >::iterator getNextUnchecked() {
std::unordered_map<KeyType, ValueType >::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; bool empty() const;
void clear(); void clear();
iterator find(const KeyType&);
iterator begin();
iterator end();
%extend { %extend {
const MappedType& get(const KeyType& key) throw (std::out_of_range) { %fragment("SWIG_MapSize");
std::unordered_map<KeyType, MappedType>::iterator i = self->find(key);
if (i != self->end()) { jint sizeImpl() const throw (std::out_of_range) {
return i->second; return SWIG_MapSize(self->size());
} else {
throw std::out_of_range("key not found");
}
} }
void set(const KeyType& key, const MappedType& x) { bool containsImpl(const KeyType& key) {
(*self)[key] = x; return (self->count(key) > 0);
} }
void del(const KeyType& key) throw (std::out_of_range) { void putUnchecked(const KeyType& key, const ValueType& value) {
std::unordered_map<KeyType, MappedType>::iterator i = self->find(key); (*self)[key] = value;
if (i != self->end()) {
self->erase(i);
} else {
throw std::out_of_range("key not found");
}
} }
bool has_key(const KeyType& key) { void removeUnchecked(const std::unordered_map<KeyType, ValueType >::iterator itr) {
std::unordered_map<KeyType, MappedType>::iterator i = self->find(key); self->erase(itr);
return i != self->end();
} }
} }
}; };
} // namespace std }