Skip to content

Commit

Permalink
Merge pull request #15 from LucienShui/feature/using_swig
Browse files Browse the repository at this point in the history
Add swig support
  • Loading branch information
LucienShui authored Oct 22, 2020
2 parents 0e4d0a4 + da24625 commit eb41b9b
Show file tree
Hide file tree
Showing 28 changed files with 377 additions and 482 deletions.
48 changes: 42 additions & 6 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
types: [published]

jobs:
deploy:
macos-build:

strategy:
matrix:
Expand All @@ -20,24 +20,54 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Set up Conda
uses: s-weigand/setup-conda@v1
- name: Install dependencies
run: |
conda install swig -y
python -m pip install --upgrade pip
pip install setuptools wheel twine
python -m pip install setuptools wheel twine
- name: Build and publish
shell: bash
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
swig -c++ -python flow_network/core/core.i
python setup.py sdist bdist_wheel
twine upload dist/*
windows-build:

strategy:
matrix:
os: [windows-latest]

name: Build with ${{ matrix.os }}

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- name: Set up Conda
uses: s-weigand/setup-conda@v1
- name: Install dependencies
run: |
conda install swig -y
python -m pip install --upgrade pip
python -m pip install setuptools wheel twine
- name: Build and publish
shell: bash
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
swig -c++ -python flow_network/core/core.i
python setup.py sdist bdist_wheel
find dist -name "*.tar.gz" | xargs rm -rf
twine upload dist/*
linux-build:
name: Build with ubuntu-latest
runs-on: ubuntu-latest
Expand All @@ -46,5 +76,11 @@ jobs:
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
steps:
- uses: actions/checkout@v2
- name: Set up Conda
uses: s-weigand/setup-conda@v1
- name: SWIG generation
run: |
conda install swig -y
swig -c++ -python flow_network/core/core.i
- name: build and upload manylinux wheels
uses: Niraj-Kamdar/manylinux-wheel-builder@master
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,10 @@ dmypy.json
# macOS
.DS_Store

# .outputs
# SWIG
flow_network/core/core_wrap.cxx
flow_network/core/core.py

# test
graph.txt
main.cpp
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.17)
project(flow_network)
project(flow-network)

set(CMAKE_CXX_STANDARD 14)

add_library(network_flows SHARED flow_network/core/flow-network.cpp flow_network/core/flow-network.h flow_network/core/py-api.h flow_network/core/py-api.cpp)
add_executable(flow-network flow_network/core/minimum_cost_flow.cpp flow_network/core/minimum_cost_flow.h main.cpp flow_network/core/graph.cpp flow_network/core/graph.h flow_network/core/maximum_flow.cpp flow_network/core/maximum_flow.h flow_network/core/common.h flow_network/core/base_network.h flow_network/core/base_network.cpp)
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include flow_network/core *.cpp *.h *.cxx
2 changes: 1 addition & 1 deletion flow_network/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, print_function

from flow_network.network import FlowNetwork, MinimumCostFlow
from flow_network.flow_network import FlowNetwork, MinimumCostFlow

from .__version__ import __title__, __description__, __url__
from .__version__ import __version__, __build__, __author__
Expand Down
5 changes: 3 additions & 2 deletions flow_network/__version__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
__title__ = 'flow-network'
__description__ = 'Flow Network C++ Implementation'
__url__ = 'https://github.com/LucienShui/flow-network'
__version__ = '0.1.5'
__build__ = eval('0x{}'.format(''.join([f'{int(each):02d}' for each in __version__.split('.')])))
__version_info__ = (0, 1, 6)
__version__ = '.'.join(map(str, __version_info__))
__build__ = eval(f"0x{''.join(map(lambda x: f'{int(x):02d}', __version_info__))}")
__author__ = 'Lucien Shui'
__author_email__ = 'lucien@lucien.ink'
__license__ = 'Apache 2.0'
Expand Down
6 changes: 0 additions & 6 deletions flow_network/core/Makefile

This file was deleted.

17 changes: 16 additions & 1 deletion flow_network/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
from .clib import Clib
try:
from .core import MaximumFlow as CMaximumFlow
from .core import MinimumCostFlow as CMinimumCostFlow
from .core import BaseNetwork as CBaseNetwork
except ModuleNotFoundError as e:
import sys
"""
在调用 setup.py 的时候需要读取 flow_network.__version__
于是 flow_network.__init__.py 会被同时调用,然后一直执行到这里
但在调用 setup.py 的时候 core.i 还没被编译成 _core
所以在这里 mock 一些 _core 里面的对象,否则会报错
"""
if sys.argv[0] == 'setup.py':
CMaximumFlow = CMinimumCostFlow = CBaseNetwork = None
else:
raise e
6 changes: 6 additions & 0 deletions flow_network/core/base_network.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "base_network.h"

namespace flow_network {
BaseNetwork::BaseNetwork(int n) : dist(new int[n]), n(n), graph(n) {}
}

16 changes: 16 additions & 0 deletions flow_network/core/base_network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef FLOW_NETWORK_BASE_NETWORK_H
#define FLOW_NETWORK_BASE_NETWORK_H

#include "graph.h"

namespace flow_network {

struct BaseNetwork {
int *dist, n;
Graph graph;

explicit BaseNetwork(int n);
};
}

#endif //FLOW_NETWORK_BASE_NETWORK_H
16 changes: 0 additions & 16 deletions flow_network/core/clib.py

This file was deleted.

9 changes: 9 additions & 0 deletions flow_network/core/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef FLOW_NETWORK_COMMON_H
#define FLOW_NETWORK_COMMON_H

namespace flow_network {

static int INF = 0x3f3f3f3f;
}

#endif //FLOW_NETWORK_COMMON_H
24 changes: 24 additions & 0 deletions flow_network/core/core.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%module core

%include "std_vector.i"
%include "std_pair.i"

%{

#include "common.h"
#include "graph.h"
#include "base_network.h"
#include "maximum_flow.h"
#include "minimum_cost_flow.h"

%}

%template(EdgeVector) std::vector<flow_network::Edge>;
%template(IntVector) std::vector<int>;
%template(IntIntPair) std::pair<int, int>;

%include "common.h"
%include "graph.h"
%include "base_network.h"
%include "maximum_flow.h"
%include "minimum_cost_flow.h"
95 changes: 0 additions & 95 deletions flow_network/core/flow-network.cpp

This file was deleted.

Loading

0 comments on commit eb41b9b

Please sign in to comment.