Skip to content

Commit

Permalink
feat Merge sort and Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sanoguzhan committed Nov 8, 2020
1 parent 2373a25 commit 15c618f
Show file tree
Hide file tree
Showing 14 changed files with 3,760 additions and 2,831 deletions.
49 changes: 48 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# STAlib
==============
STALib
==============
Standard Template and algorithms library of C++ for Python with C-Python API


.. image:: https://readthedocs.org/projects/more-itertools/badge/?version=latest
:target: https://more-itertools.readthedocs.io/en/stable/

This Librariy is complementary algotrithms and templates to Python's ``built-in``.
The algorithms implemented in C++ and extended to Python and compitable with Python's ``list`` objects.

## Algorithms:
+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Sort Algorithms | `Bubble Sort`_, |
| | `Merge Sort`_, |
+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

## Templates:
+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Sequence | `Vector`_, |
| | ``_, |
+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+


Getting started
===============

To get started, install the library with `pip <https://pip.pypa.io/en/stable/>`_:

.. code-block:: shell
pip install stalib
### Example

Import the algorithms or templates:

.. code-block:: python
>>> from stalib.algorithms import merge_sort
>>> iterable = [1,9,2,4]
>>> list(merge_sort(iterable))
[0, 1, 2, 3]
For the full listing of functions, see the [Algorithms](#algorithms) and [Tempaltes](#templates)_.

4 changes: 4 additions & 0 deletions docs/versions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ Version History
.. automodule:: stalib
:noindex:

0.1.0
-----

* Initial release, with ``Merge Sort``and ``Bubble Sort`` Algorithms
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test=pytest


[pydocstyle]
ignore=D107,D104,D103,D100,D102,D105,D203,D101,D212
ignore=D107,D104,D103,D100,D102,D105,D203,D101,D212,D400,D415

[coverage:report]
skip_empty = true
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def get_long_description():
ext_modules = cythonize(ext_modules,
language_level=3,
),
url="https://github.com/sanoguzhan/STAlib",
long_description=get_long_description(),
author='Oguzhan San',
author_email='sanoguzhan@hotmail.com',
Expand Down
2 changes: 1 addition & 1 deletion stalib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.0.16'
__version__ = '0.1.0'

8 changes: 6 additions & 2 deletions stalib/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from typing import List, Any, Dict, Tuple
import sys;print(sys.path)
from engine import _sort_bubble
from engine import Typed

def buble_sort(ll: List[Any]):
return _sort_bubble(ll)
return Typed(ll).BubbleSort()


def merge_sort(ll: List[Any]):
return Typed(ll).MergeSort()
27 changes: 27 additions & 0 deletions stalib/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
test_data = {
"integer": {"input":[1,0,90,5,4,2],
"expected": [0,1,2,4,5,90]
},
"float":{"input":[1.0,0.9,90.2,5.4,4.2,2.1,2.3,],
"expected": [0.9, 1.0, 2.1, 2.3, 4.2, 5.4, 90.2]
},
"string": {"input":[ "ac", "foo","g", "a", "ab"],
"expected": ["a", "ab", "ac", "foo","g"]

}
}
@pytest.fixture
def Integer() -> dict:
"""Test integer list for sort Algortihms"""
return test_data.get("integer")

@pytest.fixture
def Float() -> dict:
"""Test float list values for sort Algorithms"""
return test_data.get("float")

@pytest.fixture
def String() -> dict:
"""Test string list values for sort Algorithms"""
return test_data.get("string")
2 changes: 1 addition & 1 deletion stalib/core/_algorithms.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ from libcpp.vector cimport vector

cdef extern from "algorithms/sort/sort.hpp" namespace "sta":
void bubble_sort[T](vector[T] &vec) nogil except +

void merge_sort[T](vector[T] &vec) nogil except +
52 changes: 52 additions & 0 deletions stalib/core/algorithms/sort/sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,58 @@ void bubble_sort(std::vector<T> &vec){
}


template<typename T>
void merge(std::vector<T> &left,
std::vector<T> &right,
std::vector<T> &vec)
{

size_t nLeft = left.size();
size_t nRight = right.size();

size_t i=0, j=0, k=0;

while (j < nLeft && k < nRight)
{
if(left.at(j) < right.at(k)){
vec[i] = left.at(j);
j++;
}else{
vec[i] = right.at(k);
k++;
}
i++;
}

while( j < nLeft){
vec[i] = left.at(j);
j++;
i++;
}
while(k < nRight){
vec[i] = right.at(k);
k++;
i++;
}

}

template<typename T>
void merge_sort(std::vector<T> &vec){
if (vec.size() <= 1) return;
std::vector<T> left;
std::vector<T> right;
size_t mid = vec.size() /2;
for (size_t j = 0; j < mid;j++)
left.push_back(vec[j]);
for (size_t j = 0; j < (vec.size()) - mid; j++)
right.push_back(vec[mid + j]);

merge_sort(left);
merge_sort(right);
merge(left,right,vec);

}


}
Expand Down
Loading

0 comments on commit 15c618f

Please sign in to comment.