Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.so
*.egg-info/
21 changes: 21 additions & 0 deletions fraglets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,27 @@ void fraglets::drawGraphViz(){
gvFreeContext(graphContext);
}

std::vector<std::string> fraglets::getSorted(){
std::vector<std::string> result;
auto extract = [&result](keyMultisetMap &km){
for(auto &kv : km){
moleculeMultiset *mset = kv.second;
for(auto mol : mset->multiset){
if(!mol->vector.empty() && *(mol->vector[0]) == "sorted"){
for(size_t i=1;i<mol->vector.size();++i){
result.push_back(*(mol->vector[i]));
}
return true;
}
}
}
return false;
};
if(extract(this->active.keyMap)) return result;
extract(this->passive.keyMap);
return result;
}




Expand Down
1 change: 1 addition & 0 deletions fraglets.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class fraglets {
void interpret(std::string filename);
void trace();
void drawGraphViz();
std::vector<std::string> getSorted();
int iter = 0;


Expand Down
3 changes: 3 additions & 0 deletions fraglets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def parse(self, line):
def drawGraphViz(self):
cFraglets.drawGraphViz(self.cfraglets)

def get_sorted(self):
return cFraglets.getSorted(self.cfraglets)


def __delete__(self):
cFraglets.delete_object(self.cfraglets)
Expand Down
27 changes: 27 additions & 0 deletions fragletsToPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ PyObject* getUnimolTags(PyObject* self, PyObject* args)
return tags;
}

PyObject* getSorted(PyObject* self, PyObject* args)
{
PyObject* fragletsCapsule_;

PyArg_ParseTuple(args, "O",
&fragletsCapsule_);

fraglets* frag = (fraglets*)PyCapsule_GetPointer(fragletsCapsule_, "fragletsPtr");

std::vector<std::string> vec = frag->getSorted();
PyObject* list = PyList_New(vec.size());
for(size_t i=0;i<vec.size();++i){
const std::string &sym = vec[i];
PyObject* item;
try{
int v = std::stoi(sym);
item = PyLong_FromLong(v);
}catch(...){
item = Py_BuildValue("s", sym.c_str());
}
PyList_SetItem(list, i, item);
}
return list;
}


PyObject* delete_object(PyObject* self, PyObject* args)
{
Expand Down Expand Up @@ -145,6 +170,8 @@ static PyMethodDef fragletsFunctions[] =
"gets the current number of iterations"},
{"drawGraphViz",drawGraphViz,METH_VARARGS,
"draws graph"},
{"getSorted",getSorted,METH_VARARGS,
"returns sorted list"},
{"delete_object", // C++/Py Destructor
delete_object, METH_VARARGS,
"Delete `fraglets` object"},
Expand Down
24 changes: 24 additions & 0 deletions test_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
import unittest
import fraglets

class SortFraTest(unittest.TestCase):
def test_sort_fra_runs(self):
f = fraglets.fraglets()
sort_path = os.path.join(os.path.dirname(__file__), 'sort.fra')
nums = []
with open(sort_path) as fh:
for line in fh:
line = line.strip()
if line and not line.startswith('#'):
f.parse(line)
if line.startswith('[sort'):
parts = line.strip('[]').split()
nums = list(map(int, parts[1:]))
f.run(10000, 10000, True)
result = f.get_sorted()
self.assertEqual(len(result), len(nums))
self.assertTrue(all(result[i] <= result[i+1] for i in range(len(result)-1)))

if __name__ == '__main__':
unittest.main()