Skip to content

Commit

Permalink
#12 Fix class sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Sep 22, 2024
1 parent dfa7a16 commit 7cfce4b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
22 changes: 21 additions & 1 deletion cppwg/input/class_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,29 @@ def __init__(self, name: str, class_config: Optional[Dict[str, Any]] = None):
self.py_names: List[str] = None
self.base_decls: Optional[List["declaration_t"]] = None # noqa: F821

def is_child_of(self, other: "ClassInfo") -> bool: # noqa: F821
"""
Check if the class is a child of the specified class.
Parameters
----------
other : ClassInfo
The other class to check
Returns
-------
bool
True if the class is a child of the specified class, False otherwise
"""
if not self.base_decls:
return False
if not other.decls:
return False
return any(base in other.decls for base in self.base_decls)

def update_from_ns(self, ns: "namespace_t") -> None: # noqa: F821
"""
Update the class information from the source namespace.
Update class with information from the source namespace.
Adds the class declarations and base class declarations.
Expand Down
51 changes: 30 additions & 21 deletions cppwg/input/module_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module information structure."""

import os
from functools import cmp_to_key
from typing import Any, Dict, List, Optional

from cppwg.input.base_info import BaseInfo
Expand Down Expand Up @@ -79,34 +78,44 @@ def is_decl_in_source_path(self, decl: "declaration_t") -> bool: # noqa: F821

def sort_classes(self) -> None:
"""Sort the class info collection in inheritance order."""
self.class_info_collection.sort(key=lambda x: x.name)

def compare(class_info_0: "ClassInfo", class_info_1: "ClassInfo"): # noqa: F821
# Sort classes with no declarations to the bottom
if class_info_0.decls == class_info_1.decls:
return 0
if class_info_0.decls is None:
return 1
if class_info_1.decls is None:
return -1
order_changed = True
while order_changed:
order_changed = False

# 1 if class_0 is a child of class_1
child_0 = int(
any(base in class_info_1.decls for base in class_info_0.base_decls)
)
i = 0
n = len(self.class_info_collection)
while i < n - 1:
cls_i = self.class_info_collection[i]
ii = i # destination of cls_i
child_pos = [] # positions of cls_i's children

# 1 if class_1 is a child of class 0
child_1 = int(
any(base in class_info_0.decls for base in class_info_1.base_decls)
)
for j in range(i + 1, n):
cls_j = self.class_info_collection[j]
if cls_i.is_child_of(cls_j):
ii = j
elif cls_j.is_child_of(cls_i):
child_pos.append(j)

return child_0 - child_1
if ii <= i:
i += 1
continue # no change in cls_i's position

self.class_info_collection.sort(key=lambda x: x.name)
self.class_info_collection.sort(key=cmp_to_key(compare))
cls_i = self.class_info_collection.pop(i)
self.class_info_collection.insert(ii, cls_i)

for j, idx in enumerate(child_pos):
if j > ii:
break # children already positioned after cls_i
cls_j = self.class_info_collection.pop(j - 1 - idx)
self.class_info_collection.insert(ii + idx, cls_j)

order_changed = True

def update_from_ns(self, ns: "namespace_t") -> None: # noqa: F821
"""
Update class info objects with information from the source namespace.
Update module with information from the source namespace.
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions examples/shapes/wrapper/primitives/primitives.main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include "wrapper_header_collection.hpp"
#include "Shape2.cppwg.hpp"
#include "Shape3.cppwg.hpp"
#include "Cuboid.cppwg.hpp"
#include "Rectangle.cppwg.hpp"
#include "Cuboid.cppwg.hpp"

namespace py = pybind11;

PYBIND11_MODULE(_pyshapes_primitives, m)
{
register_Shape2_class(m);
register_Shape3_class(m);
register_Cuboid_class(m);
register_Rectangle_class(m);
register_Cuboid_class(m);
}

0 comments on commit 7cfce4b

Please sign in to comment.