-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added support for 'set' #251
Open
FadyEssam
wants to merge
2
commits into
boostorg:develop
Choose a base branch
from
FadyEssam:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright Fady Essam 2019. Distributed under the Boost | ||
// Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
#ifndef SET_BOOST_PYTHON_HH | ||
#define SET_BOOST_PYTHON_HH | ||
|
||
#include <boost/python/ssize_t.hpp> | ||
#include <boost/python.hpp> | ||
|
||
|
||
namespace boost { | ||
namespace python { | ||
|
||
namespace detail | ||
{ | ||
struct BOOST_PYTHON_DECL set_base : object | ||
{ | ||
void add(object_cref); // add object to set | ||
|
||
object pop(); // remove and return item at top | ||
|
||
void discard(object_cref x); // discard value from set | ||
|
||
long __len__(); // length of set | ||
|
||
void clear(); // empties set | ||
|
||
protected: | ||
set_base(); | ||
explicit set_base(object_cref sequence); // new set initialized from sequence's items | ||
|
||
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(set_base, object) | ||
private: | ||
static detail::new_non_null_reference call(object const&); | ||
}; | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
class set : public detail::set_base | ||
{ | ||
typedef detail::set_base base; | ||
public: | ||
set() {} | ||
|
||
template <class T> | ||
explicit set(T const& sequence) | ||
: base(object(sequence)) | ||
{ | ||
} | ||
|
||
template <class T> | ||
void add(T const& x) | ||
{ | ||
base::add(object(x)); | ||
} | ||
|
||
object pop() { return base::pop(); } | ||
|
||
template <class T> | ||
void discard(T const& value) | ||
{ | ||
base::discard(object(value)); | ||
} | ||
|
||
void clear() { base::clear(); } | ||
|
||
long __len__() { return base::__len__(); } | ||
|
||
|
||
|
||
public: | ||
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(set, base) | ||
}; | ||
|
||
|
||
namespace converter | ||
{ | ||
template <> | ||
struct object_manager_traits<set> | ||
: pytype_object_manager_traits<&PySet_Type, set> | ||
{ | ||
}; | ||
} | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
#endif // !SET_BOOST_PYTHON_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright Fady Essam 2019. Distributed under the Boost | ||
// Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
#include <boost/python/set.hpp> | ||
|
||
|
||
namespace boost { | ||
namespace python { | ||
namespace detail { | ||
|
||
|
||
detail::new_non_null_reference set_base::call(object_cref arg_) | ||
{ | ||
return (detail::new_non_null_reference) | ||
(expect_non_null)( | ||
PySet_New(arg_.ptr()) | ||
); | ||
} | ||
|
||
set_base::set_base() | ||
: object(detail::new_reference(PySet_New(NULL))) | ||
{} | ||
|
||
set_base::set_base(object_cref sequence) | ||
: object(set_base::call(sequence)) | ||
{} | ||
|
||
void set_base::add(object_cref x) | ||
{ | ||
if (PyAnySet_CheckExact(this->ptr())) | ||
{ | ||
if (PySet_Add(this->ptr(), x.ptr()) == -1) | ||
throw_error_already_set(); | ||
} | ||
else | ||
{ | ||
this->attr("add")(x); | ||
} | ||
} | ||
|
||
|
||
void set_base::discard(object_cref x) | ||
{ | ||
if (PyAnySet_CheckExact(this->ptr())) | ||
{ | ||
if (PySet_Discard(this->ptr(), x.ptr()) == -1) | ||
throw_error_already_set(); | ||
} | ||
else | ||
{ | ||
this->attr("discrad")(x); | ||
} | ||
} | ||
|
||
object set_base::pop() | ||
{ | ||
return this->attr("pop")(); | ||
} | ||
|
||
void set_base::clear() | ||
{ | ||
this->attr("clear")(); | ||
} | ||
|
||
long set_base::__len__() | ||
{ | ||
return extract<long>(object(PySet_Size(this->ptr())))(); | ||
} | ||
|
||
|
||
static struct register_set_pytype_ptr | ||
{ | ||
register_set_pytype_ptr() | ||
{ | ||
const_cast<converter::registration &>( | ||
converter::registry::lookup(boost::python::type_id<boost::python::set>()) | ||
).m_class_object = &PySet_Type; | ||
} | ||
}register_set_pytype_ptr_; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright Fady Essam 2019. Distributed under the Boost | ||
// Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
#include <boost/python/module.hpp> | ||
#define BOOST_ENABLE_ASSERT_HANDLER | ||
#include <boost/assert.hpp> | ||
|
||
#include <boost/python/def.hpp> | ||
#include <boost/python/class.hpp> | ||
#include <boost/python/set.hpp> | ||
#include <exception> | ||
#include <string> | ||
#include <iostream> | ||
|
||
using namespace boost::python; | ||
|
||
object new_set() | ||
{ | ||
return set(); | ||
} | ||
|
||
object data_set() | ||
{ | ||
set tmp1; | ||
tmp1.add("value1"); | ||
|
||
tmp1.add(2); | ||
return tmp1; | ||
} | ||
|
||
object set_from_sequence(object sequence) | ||
{ | ||
return set(sequence); | ||
} | ||
|
||
|
||
void work_with_set(set data1) | ||
{ | ||
if (!data1.contains("k1")) { | ||
std::cout << "data1 doesn't have k1" << std::endl; | ||
} | ||
data1.add("k1"); | ||
|
||
if (data1.contains("k1")) { | ||
std::cout << "data1 now has k1" << std::endl; | ||
} | ||
|
||
data1.discard("k1"); | ||
if (!data1.contains("k1")) { | ||
std::cout << "data1 doesn't have k1 again" << std::endl; | ||
} | ||
|
||
} | ||
|
||
void test_templates(object print) | ||
{ | ||
std::string key = "key"; | ||
|
||
set tmp; | ||
tmp.add("a test string"); | ||
print(tmp); | ||
tmp.add(13); | ||
print(tmp.contains(1.5)); | ||
print(tmp.contains(13)); | ||
print(tmp); | ||
|
||
BOOST_ASSERT(tmp.__len__() == 2); | ||
} | ||
|
||
BOOST_PYTHON_MODULE(set_ext) | ||
{ | ||
def("new_set", new_set); | ||
def("data_set", data_set); | ||
def("set_from_sequence", set_from_sequence); | ||
def("work_with_set", work_with_set); | ||
def("test_templates", test_templates); | ||
} | ||
|
||
#include "module_tail.cpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright Fady Essam 2019. Distributed under the Boost | ||
# Software License, Version 1.0. (See accompanying | ||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
from __future__ import print_function | ||
""" | ||
>>> import set_ext | ||
>>> set_ext.new_set() | ||
set() | ||
>>> set_ext.data_set() | ||
{2, 'value1'} | ||
>>> set_ext.set_from_sequence([1,2,3,3]) | ||
{1, 2, 3} | ||
>>> s = set_ext.new_set() | ||
>>> set_ext.test_templates(print) | ||
{'a test string'} | ||
False | ||
True | ||
{'a test string', 13} | ||
""" | ||
|
||
def run(args = None): | ||
import sys | ||
import doctest | ||
|
||
if args is not None: | ||
sys.argv = args | ||
return doctest.testmod(sys.modules.get(__name__)) | ||
|
||
if __name__ == '__main__': | ||
print("running...") | ||
import sys | ||
status = run()[0] | ||
if (status == 0): print("Done.") | ||
sys.exit(status) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a typo here