File: list_indexing_suite.hpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (178 lines) | stat: -rw-r--r-- 5,719 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//  (C) Copyright Greg Landrum 2003-2006.
//  Derived from vector_indexing_suite.hpp,
//  which is (C) Copyright Joel de Guzman 2003.
//  Permission to copy, use, modify, sell and distribute this software
//  is granted provided this copyright notice appears in all copies. This
//  software is provided "as is" without express or implied warranty, and
//  with no claim as to its suitability for any purpose.

#ifndef LIST_INDEXING_SUITE_GL20042_HPP
#define LIST_INDEXING_SUITE_GL20042_HPP

#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/indexing_suite.hpp>
#include <boost/python/suite/indexing/container_utils.hpp>
#include <boost/python/iterator.hpp>

#include "pyint_api.h"

namespace boost {
namespace python {

// Forward declaration
template <class Container, bool NoProxy, class DerivedPolicies>
class list_indexing_suite;

namespace detail {
template <class Container, bool NoProxy>
class final_list_derived_policies
    : public list_indexing_suite<
          Container, NoProxy, final_list_derived_policies<Container, NoProxy>> {
};
}  // namespace detail

// The vector_indexing_suite class is a predefined indexing_suite derived
// class for wrapping std::vector (and std::vector like) classes. It provides
// all the policies required by the indexing_suite (see indexing_suite).
// Example usage:
//
//  class X {...};
//
//  ...
//
//      class_<std::vector<X> >("XVec")
//          .def(vector_indexing_suite<std::vector<X> >())
//      ;
//
// By default indexed elements are returned by proxy. This can be
// disabled by supplying *true* in the NoProxy template parameter.
//
template <class Container, bool NoProxy = false,
          class DerivedPolicies =
              detail::final_list_derived_policies<Container, NoProxy>>
class list_indexing_suite
    : public indexing_suite<Container, DerivedPolicies, NoProxy> {
 public:
  typedef typename Container::value_type data_type;
  typedef typename Container::value_type key_type;
  typedef typename Container::size_type index_type;
  typedef typename Container::size_type size_type;
  typedef typename Container::iterator iterator_type;

  static typename mpl::if_<is_class<data_type>, data_type&, data_type>::type
  get_item(Container& container, index_type i) {
    iterator_type pos = moveToPos(container, i);
    return *pos;
  }
  static object get_slice(Container& container, index_type from,
                          index_type to) {
    Container res;
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    std::copy(beg, end, res.begin());
    return object(res);
  }
  static void set_item(Container& container, index_type i, data_type const& v) {
    iterator_type pos = moveToPos(container, i);
    *pos = v;
  }

  static void set_slice(Container& container, index_type from, index_type to,
                        data_type const& v) {
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    container.erase(beg, end);
    // FIX: didn't we just invalidate this iterator?
    container.insert(beg, v);
  }
  template <class Iter>
  static void set_slice(Container& container, index_type from, index_type to,
                        Iter first, Iter last) {
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    container.erase(beg, end);
    // FIX: didn't we just invalidate this iterator?
    container.insert(beg, first, last);
  }

  static void delete_item(Container& container, index_type i) {
    iterator_type pos = moveToPos(container, i);
    container.erase(pos);
  }

  static void delete_slice(Container& container, index_type from,
                           index_type to) {
    iterator_type beg = moveToPos(container, from);
    iterator_type end = moveToPos(container, to);
    container.erase(beg, end);
  }

  static size_t size(Container& container) { return container.size(); }

  static bool contains(Container& container, key_type const& key) {
    return std::find(container.begin(), container.end(), key) !=
           container.end();
  }

  static index_type get_min_index(Container& container) {
    (void)container;
    return 0;
  }

  static index_type get_max_index(Container& container) {
    return container.size();
  }

  static bool compare_index(Container& container, index_type a, index_type b) {
    (void)container;
    return a < b;
  }

  static void append(Container& container, data_type const& v) {
    container.push_back(v);
  }
  template <class Iter>
  static void extend(Container& container, Iter first, Iter last) {
    container.insert(container.end(), first, last);
  }

  static index_type convert_index(Container& container, PyObject* i_) {
    extract<long> i(i_);
    if (i.check()) {
      long index = i();
      if (index < 0) {
        index += DerivedPolicies::size(container);
      }
      if (index >= long(container.size()) || index < 0) {
        PyErr_SetString(PyExc_IndexError, "Index out of range");
        throw_error_already_set();
      }
      return index;
    }

    PyErr_SetString(PyExc_TypeError, "Invalid index type");
    throw_error_already_set();
    return index_type();
  }

 private:
  static iterator_type moveToPos(Container& container, index_type i) {
    iterator_type pos;
    index_type idx = 0;
    pos = container.begin();
    while (idx < i && pos != container.end()) {
      pos++;
      idx++;
    }
    if (pos == container.end()) {
      PyErr_SetObject(PyExc_IndexError, PyInt_FromLong(i));
      python::throw_error_already_set();
    }
    return pos;
  }
};

}  // namespace python
}  // namespace boost

#endif