gwift-book/chapters/trees.tex

36 lines
1.2 KiB
TeX
Raw Normal View History

2022-05-11 15:43:09 +02:00
\chapter{Arborescences et graphs}
2022-04-27 19:33:47 +02:00
2022-04-30 19:05:06 +02:00
On a un exemple de remplissage/vidage d'une closure table, mais il faudrait en fait présenter les listes adjacentes et les autres structures de données.
Comme ça on pourra introduire les graphs juste après.
\begin{minted}{python}
# <app>/management/commands/rebuild.py
"""This command manages Closure Tables implementation
It adds new levels and cleans links between entities.
This way, it's relatively easy to fetch an entire tree with just one tiny
request.
"""
from django.core.management.base import BaseCommand
from structure.models import Entity, EntityTreePath
class Command(BaseCommand):
def handle(self, *args, **options):
entities = Entity.objects.all()
for entity in entities:
breadcrumb = [node for node in entity.breadcrumb()]
tree = set(EntityTreePath.objects.filter(descendant=entity))
2022-05-11 15:43:09 +02:00
2022-04-30 19:05:06 +02:00
for idx, node in enumerate(breadcrumb):
tree_path, _ = EntityTreePath.objects.get_or_create(
ancestor=node, descendant=entity, weight=idx + 1
)
2022-05-11 15:43:09 +02:00
2022-04-30 19:05:06 +02:00
if tree_path in tree:
tree.remove(tree_path)
for tree_path in tree:
tree_path.delete()
\end{minted}