~knarkles/pyeigen/trunk

« back to all changes in this revision

Viewing changes to source/templates/iterator.h

  • Committer: Jussi Lepistö
  • Date: 2010-05-15 20:09:03 UTC
  • Revision ID: jussi.lepisto@iki.fi-20100515200903-104h5fzi0v4fnee3
Completely restructure the code, moving header files to the include directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2010 Jussi Lepisto
2
 
 
3
 
#ifndef TEMPLATES_ITERATOR_H
4
 
#define TEMPLATES_ITERATOR_H
5
 
 
6
 
#include <Python.h>
7
 
#include <Eigen/Core>
8
 
 
9
 
#include "config.h"
10
 
#include "macros.h"
11
 
#include "templates.h"
12
 
#include "types.h"
13
 
 
14
 
// Iterators
15
 
template<typename Scalar, int Rows, int Cols>
16
 
PyObject* MatrixIterator_iter(MATRIXITERATOR_TYPE* self);
17
 
template<typename Scalar, int Rows, int Cols>
18
 
PyObject* MatrixIterator_iternext(MATRIXITERATOR_TYPE* self);
19
 
 
20
 
//////////////////////////////////////////////////////////////////////////////
21
 
// IMPLEMENTATION
22
 
//////////////////////////////////////////////////////////////////////////////
23
 
 
24
 
// Helpers
25
 
template<typename Scalar, int Rows, int Cols>
26
 
MATRIXITERATOR_TYPE* MatrixIterator_Create()
27
 
{
28
 
        PyTypeObject* t = MatrixIterator_GetType<MATRIX_TEMPLATE>();
29
 
        return (MATRIXITERATOR_TYPE*)PyObject_New(MATRIXITERATOR_TYPE, t);
30
 
}
31
 
 
32
 
// Iterators
33
 
template<typename Scalar, int Rows, int Cols>
34
 
PyObject* MatrixIterator_iter(MATRIXITERATOR_TYPE* self)
35
 
{
36
 
        Py_INCREF(self);
37
 
        return (PyObject*)self;
38
 
}
39
 
 
40
 
template<typename Scalar, int Rows, int Cols>
41
 
PyObject* MatrixIterator_iternext(MATRIXITERATOR_TYPE* self)
42
 
{
43
 
        if(self->pObject == NULL || self->pMatrix == NULL)
44
 
        {
45
 
                PyErr_SetString(PyExc_RuntimeError, "MatrixIterator not initialized");
46
 
                return NULL;
47
 
        }
48
 
 
49
 
        // Stop iteration
50
 
        if(self->row == self->pMatrix->rows())
51
 
        {
52
 
                Py_DECREF(self->pObject);
53
 
                return NULL;
54
 
        }
55
 
 
56
 
        PyObject* value = PyFloat_FromDouble(
57
 
                (*self->pMatrix)(self->row, self->col));
58
 
 
59
 
        ++self->col;
60
 
        if(self->col == self->pMatrix->cols())
61
 
        {
62
 
                ++self->row;
63
 
                self->col = 0;
64
 
        }
65
 
 
66
 
        return value;
67
 
}
68
 
 
69
 
#endif