Java std::vector std::list enhancements
- Add missing vector copy constructor - Add constructor to initialize the containers. Note that Java's equivalent constructor for ArrayList just sets the capacity, whereas the wrappers behave like the C++ constructor and set the size. I've done this mainly because there has been a vector(size_type) constructor in the Java wrappers for many years, so best to keep this unchanged.
This commit is contained in:
parent
b40b9aee83
commit
ea55c5bba0
5 changed files with 114 additions and 12 deletions
|
|
@ -53,6 +53,12 @@ public class li_std_list_runme {
|
||||||
if (v1.size() != 0) throw new RuntimeException("v1 test (16) failed");
|
if (v1.size() != 0) throw new RuntimeException("v1 test (16) failed");
|
||||||
if (v1.remove(new Integer(456))) throw new RuntimeException("v1 test (17) failed");
|
if (v1.remove(new Integer(456))) throw new RuntimeException("v1 test (17) failed");
|
||||||
|
|
||||||
|
if (new IntList(3).size() != 3) throw new RuntimeException("constructor initial size test failed");
|
||||||
|
for (int n : new IntList(10, 999))
|
||||||
|
if (n != 999) throw new RuntimeException("constructor initialization with value failed");
|
||||||
|
for (int n : new IntList(new IntList(10, 999)))
|
||||||
|
if (n != 999) throw new RuntimeException("copy constructor initialization with value failed");
|
||||||
|
|
||||||
StructList v4 = new StructList();
|
StructList v4 = new StructList();
|
||||||
StructPtrList v5 = new StructPtrList();
|
StructPtrList v5 = new StructPtrList();
|
||||||
StructConstPtrList v6 = new StructConstPtrList();
|
StructConstPtrList v6 = new StructConstPtrList();
|
||||||
|
|
@ -61,9 +67,68 @@ public class li_std_list_runme {
|
||||||
v5.add(new Struct(34));
|
v5.add(new Struct(34));
|
||||||
v6.add(new Struct(56));
|
v6.add(new Struct(56));
|
||||||
|
|
||||||
Struct s = null;
|
|
||||||
if (v4.get(0).getNum() != 12) throw new RuntimeException("v4 test failed");
|
if (v4.get(0).getNum() != 12) throw new RuntimeException("v4 test failed");
|
||||||
if (v5.get(0).getNum() != 34) throw new RuntimeException("v5 test failed");
|
if (v5.get(0).getNum() != 34) throw new RuntimeException("v5 test failed");
|
||||||
if (v6.get(0).getNum() != 56) throw new RuntimeException("v6 test failed");
|
if (v6.get(0).getNum() != 56) throw new RuntimeException("v6 test failed");
|
||||||
|
|
||||||
|
for (Struct s : v4) {
|
||||||
|
if (s.getNum() != 12) throw new RuntimeException("v4 loop test failed");
|
||||||
|
}
|
||||||
|
for (Struct s : v5) {
|
||||||
|
if (s.getNum() != 34) throw new RuntimeException("v5 loop test failed");
|
||||||
|
}
|
||||||
|
for (Struct s : v6) {
|
||||||
|
if (s.getNum() != 56) throw new RuntimeException("v6 loop test failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
StructList v7 = li_std_list.CopyContainerStruct(new StructList());
|
||||||
|
v7.add(new Struct(1));
|
||||||
|
v7.add(new Struct(23));
|
||||||
|
v7.add(new Struct(456));
|
||||||
|
v7.add(new Struct(7890));
|
||||||
|
if (v7.size() != 4) throw new RuntimeException("v7 test (1) failed");
|
||||||
|
{
|
||||||
|
double[] a7 = {1, 23, 456, 7890};
|
||||||
|
int i7 = 0;
|
||||||
|
for (Struct s7 : v7) {
|
||||||
|
if (s7.getNum() != a7[i7]) throw new RuntimeException("v7 test (2) failed");
|
||||||
|
i7++;
|
||||||
|
}
|
||||||
|
if (i7 != a7.length) throw new RuntimeException("v7 test (3) failed");
|
||||||
|
}
|
||||||
|
if (v7.remove(2).getNum() != 456) throw new RuntimeException("v7 test (4) failed");
|
||||||
|
{
|
||||||
|
double[] a7 = {1, 23, 7890};
|
||||||
|
int i7 = 0;
|
||||||
|
for (Struct s7 : v7) {
|
||||||
|
if (s7.getNum() != a7[i7]) throw new RuntimeException("v7 test (5) failed");
|
||||||
|
i7++;
|
||||||
|
}
|
||||||
|
if (i7 != a7.length) throw new RuntimeException("v7 test (6) failed");
|
||||||
|
}
|
||||||
|
v7.add(1, new Struct(123));
|
||||||
|
{
|
||||||
|
double[] a7 = {1, 123, 23, 7890};
|
||||||
|
int i7 = 0;
|
||||||
|
for (Struct s7 : v7) {
|
||||||
|
if (s7.getNum() != a7[i7]) throw new RuntimeException("v7 test (7) failed");
|
||||||
|
i7++;
|
||||||
|
}
|
||||||
|
if (i7 != a7.length) throw new RuntimeException("v7 test (8) failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
BoolList v8 = new BoolList();
|
||||||
|
if (!v8.add(true)) throw new RuntimeException("v8 test (1) failed");;
|
||||||
|
if (v8.get(0) != true) throw new RuntimeException("v8 test (2) failed");;
|
||||||
|
if (v8.set(0, false) != true) throw new RuntimeException("v8 test (3) failed");;
|
||||||
|
if (v8.set(0, false) != false) throw new RuntimeException("v8 test (4) failed");;
|
||||||
|
if (v8.size() != 1) throw new RuntimeException("v8 test (5) failed");;
|
||||||
|
|
||||||
|
java.util.ArrayList<Boolean> bl = new java.util.ArrayList<Boolean>(java.util.Arrays.asList(true, false, true, false));
|
||||||
|
BoolList bv = new BoolList(java.util.Arrays.asList(true, false, true, false));
|
||||||
|
BoolList bv2 = new BoolList(bl);
|
||||||
|
java.util.ArrayList<Boolean> bl2 = new java.util.ArrayList<Boolean>(bv);
|
||||||
|
boolean bbb1 = bv.get(0);
|
||||||
|
Boolean bbb2 = bv.get(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,12 @@ public class li_std_vector_runme {
|
||||||
if (v1.size() != 0) throw new RuntimeException("v1 test (16) failed");
|
if (v1.size() != 0) throw new RuntimeException("v1 test (16) failed");
|
||||||
if (v1.remove(new Integer(456))) throw new RuntimeException("v1 test (17) failed");
|
if (v1.remove(new Integer(456))) throw new RuntimeException("v1 test (17) failed");
|
||||||
|
|
||||||
|
if (new IntVector(3).size() != 3) throw new RuntimeException("constructor initial size test failed");
|
||||||
|
for (int n : new IntVector(10, 999))
|
||||||
|
if (n != 999) throw new RuntimeException("constructor initialization with value failed");
|
||||||
|
for (int n : new IntVector(new IntVector(10, 999)))
|
||||||
|
if (n != 999) throw new RuntimeException("copy constructor initialization with value failed");
|
||||||
|
|
||||||
StructVector v4 = li_std_vector.vecstruct(new StructVector());
|
StructVector v4 = li_std_vector.vecstruct(new StructVector());
|
||||||
StructPtrVector v5 = li_std_vector.vecstructptr(new StructPtrVector());
|
StructPtrVector v5 = li_std_vector.vecstructptr(new StructPtrVector());
|
||||||
StructConstPtrVector v6 = li_std_vector.vecstructconstptr(new StructConstPtrVector());
|
StructConstPtrVector v6 = li_std_vector.vecstructconstptr(new StructConstPtrVector());
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
%module li_std_list
|
%module li_std_list
|
||||||
|
|
||||||
%include "std_list.i"
|
%include "std_list.i"
|
||||||
|
%include "std_string.i"
|
||||||
|
|
||||||
%{
|
%{
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
@ -8,15 +9,18 @@
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
%}
|
%}
|
||||||
|
|
||||||
namespace std {
|
%template(BoolList) std::list<bool>;
|
||||||
%template(IntList) list<int>;
|
%template(CharList) std::list<char>;
|
||||||
}
|
%template(ShortList) std::list<short>;
|
||||||
|
%template(IntList) std::list<int>;
|
||||||
|
%template(LongList) std::list<long>;
|
||||||
|
%template(UCharList) std::list<unsigned char>;
|
||||||
|
%template(UIntList) std::list<unsigned int>;
|
||||||
|
%template(UShortList) std::list<unsigned short>;
|
||||||
|
%template(ULongList) std::list<unsigned long>;
|
||||||
|
%template(FloatList) std::list<float>;
|
||||||
%template(DoubleList) std::list<double>;
|
%template(DoubleList) std::list<double>;
|
||||||
|
%template(StringList) std::list<std::string>;
|
||||||
namespace std {
|
|
||||||
%template(RealList) list<float>;
|
|
||||||
}
|
|
||||||
|
|
||||||
%inline %{
|
%inline %{
|
||||||
|
|
||||||
|
|
@ -37,6 +41,10 @@ struct Struct {
|
||||||
// bool operator==(const Struct &other) { return (num == other.num); }
|
// bool operator==(const Struct &other) { return (num == other.num); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const std::list<Struct> & CopyContainerStruct(const std::list<Struct> & container) { return container; }
|
||||||
|
const std::list<Struct *> & CopyContainerStructPtr(const std::list<Struct *> & container) { return container; }
|
||||||
|
const std::list<const Struct *> & CopyContainerStructConstPtr(const std::list<const Struct *> & container) { return container; }
|
||||||
|
|
||||||
enum Fruit {
|
enum Fruit {
|
||||||
APPLE,
|
APPLE,
|
||||||
BANANNA,
|
BANANNA,
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ SWIGINTERN jint SWIG_ListSize(size_t size) {
|
||||||
namespace std {
|
namespace std {
|
||||||
template <typename T> class list {
|
template <typename T> class list {
|
||||||
|
|
||||||
%typemap(javabase) std::list< T > "java.util.AbstractSequentialList<$typemap(jboxtype, T)>"
|
%typemap(javabase) std::list<T> "java.util.AbstractSequentialList<$typemap(jboxtype, T)>"
|
||||||
%proxycode %{
|
%proxycode %{
|
||||||
public $javaclassname(java.util.Collection c) {
|
public $javaclassname(java.util.Collection c) {
|
||||||
this();
|
this();
|
||||||
|
|
@ -158,7 +158,6 @@ namespace std {
|
||||||
};
|
};
|
||||||
|
|
||||||
list();
|
list();
|
||||||
list(size_type n, const T &value = T());
|
|
||||||
list(const list &other);
|
list(const list &other);
|
||||||
~list();
|
~list();
|
||||||
void assign(size_type n, const T &value);
|
void assign(size_type n, const T &value);
|
||||||
|
|
@ -178,6 +177,18 @@ namespace std {
|
||||||
|
|
||||||
%extend {
|
%extend {
|
||||||
%fragment("SWIG_ListSize");
|
%fragment("SWIG_ListSize");
|
||||||
|
list(jint count) {
|
||||||
|
if (count < 0)
|
||||||
|
throw std::out_of_range("list count must be positive");
|
||||||
|
return new std::list<T>(static_cast<std::list<T>::size_type>(count));
|
||||||
|
}
|
||||||
|
|
||||||
|
list(jint count, const T &value) {
|
||||||
|
if (count < 0)
|
||||||
|
throw std::out_of_range("list count must be positive");
|
||||||
|
return new std::list<T>(static_cast<std::list<T>::size_type>(count), value);
|
||||||
|
}
|
||||||
|
|
||||||
jint doSize() const throw (std::out_of_range) {
|
jint doSize() const throw (std::out_of_range) {
|
||||||
return SWIG_ListSize(self->size());
|
return SWIG_ListSize(self->size());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ SWIGINTERN jint SWIG_VectorSize(size_t size) {
|
||||||
typedef CTYPE value_type;
|
typedef CTYPE value_type;
|
||||||
typedef CREF_TYPE const_reference;
|
typedef CREF_TYPE const_reference;
|
||||||
vector();
|
vector();
|
||||||
vector(size_type n);
|
vector(const vector &other);
|
||||||
size_type capacity() const;
|
size_type capacity() const;
|
||||||
void reserve(size_type n);
|
void reserve(size_type n);
|
||||||
%rename(isEmpty) empty;
|
%rename(isEmpty) empty;
|
||||||
|
|
@ -83,6 +83,18 @@ SWIGINTERN jint SWIG_VectorSize(size_t size) {
|
||||||
void clear();
|
void clear();
|
||||||
%extend {
|
%extend {
|
||||||
%fragment("SWIG_VectorSize");
|
%fragment("SWIG_VectorSize");
|
||||||
|
vector(jint count) {
|
||||||
|
if (count < 0)
|
||||||
|
throw std::out_of_range("vector count must be positive");
|
||||||
|
return new std::vector< CTYPE >(static_cast<std::vector< CTYPE >::size_type>(count));
|
||||||
|
}
|
||||||
|
|
||||||
|
vector(jint count, const CTYPE &value) {
|
||||||
|
if (count < 0)
|
||||||
|
throw std::out_of_range("vector count must be positive");
|
||||||
|
return new std::vector< CTYPE >(static_cast<std::vector< CTYPE >::size_type>(count), value);
|
||||||
|
}
|
||||||
|
|
||||||
jint doSize() const throw (std::out_of_range) {
|
jint doSize() const throw (std::out_of_range) {
|
||||||
return SWIG_VectorSize(self->size());
|
return SWIG_VectorSize(self->size());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue