File: li_std_list.i

package info (click to toggle)
swig 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 45,980 kB
  • sloc: cpp: 54,284; ansic: 29,022; java: 17,595; python: 12,734; cs: 10,421; ruby: 7,263; yacc: 6,501; makefile: 5,860; javascript: 5,538; sh: 5,422; perl: 4,246; php: 3,733; ml: 2,198; tcl: 2,015; lisp: 1,448; xml: 115
file content (115 lines) | stat: -rw-r--r-- 3,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
%module li_std_list

%include "std_list.i"
%include "std_string.i"

%{
#include <algorithm>
#include <functional>
#include <numeric>
%}

#if 1
#if defined(SWIGJAVA) && JAVA_VERSION_MAJOR < 21
// Test these methods which were removed in swig-4.0 as JDK added the same methods
// in the java.util.AbstractSequentialList base but unfortunately two of them use
// a different return type.
%extend std::list {
  void removeLast() { $self->pop_back(); }
  void removeFirst() { $self->pop_front(); }
  void addLast(const T &value) { $self->push_back(value); }
  void addFirst(const T &value) { $self->push_front(value); }
}
#endif
#else
// Alternative implementation suggested in https://github.com/swig/swig/issues/3156
%extend std::list {
%proxycode %{
  public $typemap(jboxtype, T) removeLast() {
    if (this.isEmpty()) {
      throw new java.util.NoSuchElementException();
    } else {
      return this.remove(this.size() - 1);
    }
  }

  public $typemap(jboxtype, T) removeFirst() {
    if (this.isEmpty()) {
      throw new java.util.NoSuchElementException();
    } else {
      return this.remove(0);
    }
  }

  public void addLast($typemap(jboxtype, T) value) {
    this.add(value);
  }

  public void addFirst($typemap(jboxtype, T) value) {
    this.add(0, value);
  }
%}
}
#endif

%template(BoolList) std::list<bool>;
%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(StringList) std::list<std::string>;

%inline %{

double average(std::list<int> v) {
    return std::accumulate(v.begin(),v.end(),0.0)/v.size();
}


void halve_in_place(std::list<double>& v) {
    for (std::list<double>::iterator it = v.begin(); it != v.end(); ++it)
        *it /= 2.0;
}

struct Struct {
  double num;
  Struct() : num(0.0) {}
  Struct(double d) : num(d) {}
};

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; }

const std::list<float> & listreal(const std::list<float> & list) { return list; }
           
const std::list<int> & listint(const std::list<int> & list) { return list; }
const std::list<int *> & listintptr(const std::list<int *> & list) { return list; }
const std::list<const int *> & listintconstptr(const std::list<const int *> & list) { return list; }
           
const std::list<Struct> & liststruct(const std::list<Struct> & list) { return list; }
const std::list<Struct *> & liststructptr(const std::list<Struct *> & list) { return list; }
const std::list<const Struct *> & liststructconstptr(const std::list<const Struct *> & list) { return list; }

enum Fruit {
  APPLE,
  BANANNA,
  PEAR,
  KIWI,
};
%}

#if !defined(SWIGR)
%template(IntPtrList) std::list<int *>;
%template(IntConstPtrList) std::list<const int *>;
#endif
%template(StructList) std::list<Struct>;
%template(StructPtrList) std::list<Struct *>;
%template(StructConstPtrList) std::list<const Struct *>;
%template(FruitList) std::list<enum Fruit>;