Skip to content

Commit

Permalink
Merge pull request #1712 from borglab/update-wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal authored Jan 17, 2024
2 parents f191388 + 6cc16a3 commit 2dfd15e
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 152 deletions.
1 change: 0 additions & 1 deletion gtsam/3rdparty/cephes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ project(

set(CEPHES_HEADER_FILES
cephes.h
cephes/cephes_names.h
cephes/dd_idefs.h
cephes/dd_real.h
cephes/dd_real_idefs.h
Expand Down
1 change: 0 additions & 1 deletion gtsam/3rdparty/cephes/cephes.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CEPHES_H
#define CEPHES_H

#include "cephes/cephes_names.h"
#include "dllexport.h"

#ifdef __cplusplus
Expand Down
114 changes: 0 additions & 114 deletions gtsam/3rdparty/cephes/cephes/cephes_names.h

This file was deleted.

1 change: 0 additions & 1 deletion gtsam/3rdparty/cephes/cephes/mconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
#include <math.h>
#include <stdlib.h>

#include "cephes_names.h"
#include "cephes.h"
#include "polevl.h"
#include "sf_error.h"
Expand Down
15 changes: 15 additions & 0 deletions gtsam/gtsam.i
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class KeyList {
void remove(size_t key);

void serialize() const;

// Special dunder methods for Python wrapping
__len__();
__contains__(size_t key);
__iter__();
};

// Actually a FastSet<Key>
Expand All @@ -64,6 +69,11 @@ class KeySet {
bool count(size_t key) const; // returns true if value exists

void serialize() const;

// Special dunder methods for Python wrapping
__len__();
__contains__(size_t key);
__iter__();
};

// Actually a vector<Key>, needed for Matlab
Expand All @@ -85,6 +95,11 @@ class KeyVector {
void push_back(size_t key) const;

void serialize() const;

// Special dunder methods for Python wrapping
__len__();
__contains__(size_t key);
__iter__();
};

// Actually a FastMap<Key,int>
Expand Down
36 changes: 35 additions & 1 deletion python/gtsam/tests/test_Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import unittest

import numpy as np
from gtsam.utils.test_case import GtsamTestCase

import gtsam
from gtsam.utils.test_case import GtsamTestCase


class TestUtilites(GtsamTestCase):
"""Test various GTSAM utilities."""

def test_createKeyList(self):
"""Test createKeyList."""
I = [0, 1, 2]
Expand All @@ -28,6 +29,17 @@ def test_createKeyList(self):
kl = gtsam.utilities.createKeyList("s", I)
self.assertEqual(kl.size(), 3)

def test_KeyList_iteration(self):
"""Tests for KeyList iteration"""
I = [0, 1, 2]
kl = gtsam.utilities.createKeyList(I)

self.assertEqual(len(kl), len(I))

for i, key in enumerate(kl):
self.assertTrue(key in kl)
self.assertEqual(I[i], key)

def test_createKeyVector(self):
"""Test createKeyVector."""
I = [0, 1, 2]
Expand All @@ -37,6 +49,17 @@ def test_createKeyVector(self):
kl = gtsam.utilities.createKeyVector("s", I)
self.assertEqual(len(kl), 3)

def test_KeyVector_iteration(self):
"""Tests for KeyVector iteration"""
I = [0, 1, 2]
kv = gtsam.utilities.createKeyVector(I)

self.assertEqual(len(kv), len(I))

for i, key in enumerate(kv):
self.assertTrue(key in kv)
self.assertEqual(I[i], key)

def test_createKeySet(self):
"""Test createKeySet."""
I = [0, 1, 2]
Expand All @@ -46,6 +69,17 @@ def test_createKeySet(self):
kl = gtsam.utilities.createKeySet("s", I)
self.assertEqual(kl.size(), 3)

def test_KeySet_iteration(self):
"""Tests for KeySet iteration"""
I = [0, 1, 2]
ks = gtsam.utilities.createKeySet(I)

self.assertEqual(len(ks), len(I))

for i, key in enumerate(ks):
self.assertTrue(key in ks)
self.assertEqual(I[i], key)

def test_extractPoint2(self):
"""Test extractPoint2."""
initial = gtsam.Values()
Expand Down
48 changes: 39 additions & 9 deletions wrap/gtwrap/interface_parser/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

from typing import Any, Iterable, List, Union

from pyparsing import Literal, Optional, ZeroOrMore # type: ignore
from pyparsing import ZeroOrMore # type: ignore
from pyparsing import Literal, Optional, Word, alphas

from .enum import Enum
from .function import ArgumentList, ReturnType
from .template import Template
from .tokens import (CLASS, COLON, CONST, IDENT, LBRACE, LPAREN, OPERATOR,
RBRACE, RPAREN, SEMI_COLON, STATIC, VIRTUAL)
from .tokens import (CLASS, COLON, CONST, DUNDER, IDENT, LBRACE, LPAREN,
OPERATOR, RBRACE, RPAREN, SEMI_COLON, STATIC, VIRTUAL)
from .type import TemplatedType, Typename
from .utils import collect_namespaces
from .variable import Variable
Expand Down Expand Up @@ -212,6 +213,26 @@ def __repr__(self) -> str:
)


class DunderMethod:
"""Special Python double-underscore (dunder) methods, e.g. __iter__, __contains__"""
rule = (
DUNDER #
+ (Word(alphas))("name") #
+ DUNDER #
+ LPAREN #
+ ArgumentList.rule("args_list") #
+ RPAREN #
+ SEMI_COLON # BR
).setParseAction(lambda t: DunderMethod(t.name, t.args_list))

def __init__(self, name: str, args: ArgumentList):
self.name = name
self.args = args

def __repr__(self) -> str:
return f"DunderMethod: __{self.name}__({self.args})"


class Class:
"""
Rule to parse a class defined in the interface file.
Expand All @@ -223,23 +244,26 @@ class Hello {
};
```
"""

class Members:
"""
Rule for all the members within a class.
"""
rule = ZeroOrMore(Constructor.rule #
rule = ZeroOrMore(DunderMethod.rule #
^ Constructor.rule #
^ Method.rule #
^ StaticMethod.rule #
^ Variable.rule #
^ Operator.rule #
^ Enum.rule #
).setParseAction(lambda t: Class.Members(t.asList()))

def __init__(self,
members: List[Union[Constructor, Method, StaticMethod,
Variable, Operator]]):
def __init__(self, members: List[Union[Constructor, Method,
StaticMethod, Variable,
Operator, Enum, DunderMethod]]):
self.ctors = []
self.methods = []
self.dunder_methods = []
self.static_methods = []
self.properties = []
self.operators = []
Expand All @@ -251,6 +275,8 @@ def __init__(self,
self.methods.append(m)
elif isinstance(m, StaticMethod):
self.static_methods.append(m)
elif isinstance(m, DunderMethod):
self.dunder_methods.append(m)
elif isinstance(m, Variable):
self.properties.append(m)
elif isinstance(m, Operator):
Expand All @@ -271,8 +297,8 @@ def __init__(self,
+ SEMI_COLON # BR
).setParseAction(lambda t: Class(
t.template, t.is_virtual, t.name, t.parent_class, t.members.ctors, t.
members.methods, t.members.static_methods, t.members.properties, t.
members.operators, t.members.enums))
members.methods, t.members.static_methods, t.members.dunder_methods, t.
members.properties, t.members.operators, t.members.enums))

def __init__(
self,
Expand All @@ -283,6 +309,7 @@ def __init__(
ctors: List[Constructor],
methods: List[Method],
static_methods: List[StaticMethod],
dunder_methods: List[DunderMethod],
properties: List[Variable],
operators: List[Operator],
enums: List[Enum],
Expand All @@ -308,6 +335,7 @@ def __init__(
self.ctors = ctors
self.methods = methods
self.static_methods = static_methods
self.dunder_methods = dunder_methods
self.properties = properties
self.operators = operators
self.enums = enums
Expand All @@ -326,6 +354,8 @@ def __init__(
method.parent = self
for static_method in self.static_methods:
static_method.parent = self
for dunder_method in self.dunder_methods:
dunder_method.parent = self
for _property in self.properties:
_property.parent = self

Expand Down
2 changes: 1 addition & 1 deletion wrap/gtwrap/interface_parser/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def from_parse_result(parse_result: ParseResults):
return ArgumentList([])

def __repr__(self) -> str:
return repr(tuple(self.args_list))
return ",".join([repr(x) for x in self.args_list])

def __len__(self) -> int:
return len(self.args_list)
Expand Down
1 change: 1 addition & 0 deletions wrap/gtwrap/interface_parser/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

LPAREN, RPAREN, LBRACE, RBRACE, COLON, SEMI_COLON = map(Suppress, "(){}:;")
LOPBRACK, ROPBRACK, COMMA, EQUAL = map(Suppress, "<>,=")
DUNDER = Suppress(Literal("__"))

# Default argument passed to functions/methods.
# Allow anything up to ',' or ';' except when they
Expand Down
Loading

0 comments on commit 2dfd15e

Please sign in to comment.