/usr/share/pythoncad/PythonCAD/Generic/tree.py is in pythoncad 0.1.37.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | #
# Copyright (c) 2002, 2003 Art Haas
#
# This file is part of PythonCAD.
#
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# a class for maintaining a sorted list
#
# each list holds only a single type of object, and
# the list is organized from smallest to largest
#
from PythonCAD.Generic import baseobject
class TreeItem(baseobject.ModObject):
"""An object stored in a Tree.
A TreeItem object is meant to be a base class for classes
that will have instances stored in a Tree. TreeItem objects
are derived from ModObjects, so they share the same attributes
and methods as those objects.
"""
def __init__(self):
"""Initialize a TreeItem object.
There are no arguments to this method.
"""
baseobject.ModObject.__init__(self)
self.__tree = None # private to the TreeItem
def _setTree(self, tree):
"""Store a reference to the Tree this TreeItem is stored within.
_setTree(tree)
A TreeItem object can only be kept in one Tree at a time.
This routine is private to the TreeItem implementation.
"""
if tree is None:
self.__tree = None
else:
if not isinstance(tree, Tree):
raise TypeError, "Invalid Tree for storage: " + str(tree)
if self.__tree is not None:
raise ValueError, "TreeItem already claimed by a tree."
self.__tree = tree
def modified(self):
"""Set the modified flag value to True.
modified()
This method extends the ModObject::modified() method
"""
baseobject.ModObject.modified(self)
if self.__tree is not None:
self.__tree.markModified(self)
class Tree(list):
"""A class for Trees.
The Tree class is similar to a list, but it stores the
objects in a sorted order. A Tree object can be iterated
over, and there is a several method for scanning objects
held in the tree.
An object stored in the tree must be derived from the TreeItem
class.
A Tree has several methods:
clear(): Remove all objects from the tree.
store(obj): Store some object in the tree.
remove(obj): Remove an object from the tree
binscan(obj): Search the tree for the object using a binary search
scan(obj): Search the tree for the object using a linear search.
isModified(): An object stored in the Tree has its modified flag set to True
getModified(): Return the objects in the Tree that are modified
clean(): Sort the objects stored in the Tree, and remove any duplicates.
Trees also have a modified __add__ method that allows
you to add trees together, and a __contains__ method
for doing "obj in tree" or "obj not in tree" operations.
Also, a Tree supports indexed retrival similar to a list,
but not allow indexed assignment or indexed deletion.
"""
def __init__(self, t):
"""Initialize a Tree.
Tree(t)
A Tree takes a single parameter t which is the
type of object stored within the tree. Any Tree
will only hold one type of object, and that object
type cannot be None.
"""
if not issubclass(t, TreeItem):
raise ValueError, "Invalid object for Tree storage: " + `t`
list.__init__(self)
self.__type = t
self.__iter_index = 0
self.__modified = []
def __iter__(self):
"""Make a Tree iterable.
"""
if len(self.__modified):
raise ValueError, "Tree in modified state."
self.__iter_index = 0
return self
def next(self):
"""This method is used for iteration.
"""
_index = self.__iter_index
_next = _index + 1
if _next > len(self):
raise StopIteration
self.__iter_index = _next
return self[_index]
def __setitem__(self, index, value):
raise StandardError, "Tree::__setitem__() is not supported."
def __delitem__(self, index):
raise StandardError, "Tree::__delitem__() is not supported."
def __setslice__(self, i, j, vals):
raise StandardError, "Tree::__setslice__() is not supported."
def __delslice__(self, i, j):
raise StandardError, "Tree::__delslice__() is not supported."
def append(self, obj):
raise StandardError, "Tree::append() is not supported."
def insert(self, index, obj):
raise StandardError, "Tree::insert() is not supported."
def extend(self, objlist):
raise StandardError, "Tree::extend() is not supported."
def pop(self, idx=-1):
raise StandardError, "Tree::pop() is not supported."
def reverse(self):
raise StandardError, "Tree::reverse() is not supported."
def __contains__(self, obj):
"""Test if an object is in a Tree.
This method uses a binary search to find if an object
is in a Tree. It _should_ always be faster than a simple
linear search from first object to the last.
"""
if not isinstance(obj, self.__type):
raise TypeError, "Invalid object for inclusion test: " + `obj`
if len(self.__modified):
raise ValueError, "Tree in modified state."
_lo = 0
_hi = len(self)
_scanlist = []
_seen = False
while _lo < _hi:
_mid = (_hi+_lo)//2
if _mid in _scanlist:
break
_scanlist.append(_mid)
_res = cmp(self[_mid], obj)
if _res == -1:
_lo = _mid + 1
elif _res == 1:
_hi = _mid
else:
_seen = True
break
return _seen
def clear(self):
"""Empty the Tree.
clear()
"""
del self.__modified[:]
list.__delslice__(self, 0, len(self))
def markModified(self, treeitem):
"""Store a reference to a modified TreeItem object.
markModified(treeitem)
This routine is private to the Tree implementation.
"""
self.__modified.append(treeitem)
def store(self, obj):
"""Store an object in the Tree.
store(obj)
The object must be the same as was given when the tree
was instantiated. A TypeError is raised if another
object type is stored.
The objects in the Tree are searched by using the cmp()
function. Any class that will be stored in a Tree should
provide a suitable __cmp__ method.
"""
if not isinstance(obj, self.__type):
raise TypeError, "Invalid object for storage: " + `obj`
obj._setTree(self)
if len(self.__modified):
raise ValueError, "Tree in modified state."
_lo = 0
_hi = len(self)
_scanlist = []
while _lo < _hi:
_mid = (_hi+_lo)//2
if _mid in _scanlist:
break
_scanlist.append(_mid)
_res = cmp(self[_mid], obj)
if _res == -1:
_lo = _mid + 1
elif _res == 1:
_hi = _mid
else:
raise ValueError, "Equivalent object already in tree: " + `obj`
list.insert(self, _lo, obj)
assert _is_sorted(self), "Tree is not sorted: " + `self`
def remove(self, obj):
"""Remove an object from a Tree.
remove(obj)
Delete the object from the Tree.
"""
if not isinstance(obj, self.__type):
raise TypeError, "Invalid object for removal: " + `obj`
if len(self.__modified):
raise ValueError, "Tree in modified state."
obj._setTree(None)
_lo = 0
_hi = len(self)
_scanlist = []
while _lo < _hi:
_mid = (_hi+_lo)//2
if _mid in _scanlist:
break
_scanlist.append(_mid)
_res = cmp(self[_mid], obj)
if _res == -1:
_lo = _mid + 1
elif _res == 1:
_hi = _mid
else:
list.__delitem__(self, _mid)
break
def binscan(self, obj, tol=0):
"""Scan the Tree for the an object using a binary search.
binscan(obj)
This method looks for an object in a Tree utilizing a simple
binary search. If the object is found within the tree, the
index of the object is returned. Otherwise, the function returns
None.
"""
if not isinstance(obj, self.__type):
raise TypeError, "Invalid object for binscan: " + `obj`
if len(self.__modified):
raise ValueError, "Tree in modified state."
_lo = 0
_hi = len(self)
_scanlist = []
_ro = None
while _lo < _hi:
_mid = (_hi+_lo)//2
if _mid in _scanlist:
break
_scanlist.append(_mid)
_res = cmp(self[_mid], obj)
if _res == -1:
_lo = _mid + 1
elif _res == 1:
_hi = _mid
else:
_ro = self[_mid]
break
return _ro
def scan(self, obj, tol=0):
"""Scan the Tree for the an object from first to last.
scan(obj)
This method looks for an object in a Tree from the first object
to the last. It stops searching at the first instance which is
greater than the object, or the first instance where one object
is equal to the other.
The function returns None if no object in the Tree is found
equal to the object being compared against.
"""
if not isinstance(obj, self.__type):
raise TypeError, "Invalid object for scan: " + `obj`
if len(self.__modified):
raise ValueError, "Tree in modified state."
_ro = None
for _sobj in self:
_res = cmp(_sobj, obj)
if _res == 1:
break
if _res == 0:
_ro = _sobj
break
return _ro
def isModified(self):
"""Returns True if an object in the tree has the modified flag to True.
isModified()
"""
return len(self.__modified) != 0
def getModified(self):
"""Return any objects in the tree that have the modified flag to True.
getModified()
"""
return self.__modified[:]
def clean(self):
"""Sort the objects in the tree, and remove duplicated entities.
clean()
This function returns any duplicated entities found in the Tree.
It is up to the caller to deal with any returned objects.
"""
_dups = []
if len(self.__modified):
self.sort()
_pobj = self[0]
_dlist = []
for _i in range(1, len(self)):
_obj = self[_i]
if _obj == _pobj:
_dlist.append(_i)
else:
_pobj = _obj
if len(_dlist):
_dlist.reverse()
for _i in _dlist:
_dups.append(self[_i])
list.__delitem__(self, _i)
for _obj in self.__modified:
if _obj not in _dups:
_obj.reset()
del self.__modified[:]
return _dups
#
# this is a function used for assertion testing
#
def _is_sorted(tree):
_res = True
for _i in range(1, len(tree)):
_tp = tree[_i-1]
_ti = tree[_i]
if _tp >= _ti:
_res = False
break
return _res
|