home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / exml.lha / exml / examples / ebook / gobo_ext / ds_linked_tree_cursor.e < prev    next >
Text File  |  1999-04-13  |  7KB  |  325 lines

  1. indexing
  2.     description:"Cursor class for flexible iteration through%
  3.                     %DS_LINKED_TREE objects"
  4.     status:        "See notice at end of class."
  5.     author:        "Andreas Leitner"
  6.  
  7. class
  8.     DS_LINKED_TREE_CURSOR [G]
  9.  
  10. inherit
  11.     DS_CURSOR [G]
  12.  
  13. creation
  14.     make
  15.  
  16. feature -- Initialization
  17. feature {NONE} -- Initialization
  18.  
  19.     make (tree: like container) is
  20.             -- Create a new cursor for `tree'.
  21.         require
  22.             list_not_void: tree /= void
  23.         do
  24.             container := tree
  25.             before_root := True
  26.         ensure
  27.             container_set: container = tree;
  28.             before_root: before_root
  29.         end;
  30.  
  31. feature -- Access
  32.  
  33.     container: DS_LINKED_TREE [G]
  34.  
  35. feature -- Status report
  36.  
  37.     is_root: BOOLEAN is
  38.             -- Is cursor on root item?
  39.         require
  40.             not_off: not off
  41.         do
  42.             Result := (not off) and then (container.root_cell = current_cell)
  43.         end
  44.  
  45.     depth: INTEGER is
  46.         require
  47.             not_off: not off
  48.         do
  49.             Result := current_cell.depth
  50.         end
  51.  
  52.  
  53.     is_leaf: BOOLEAN is
  54.         require
  55.             not_off: not off
  56.         do
  57.             Result := current_cell.is_leaf
  58.         end
  59.  
  60.     is_valid: BOOLEAN is
  61.             -- Is cursor valid?
  62.             -- (A cursor might become invalid if `container'
  63.             -- has been modified during traversal.)
  64.             -- (from DS_CURSOR)
  65.         local
  66.             a_cell: like current_cell
  67.         do
  68.             -- TODO: !!!
  69.             --check False end
  70.             Result := True
  71.         end
  72.  
  73.     before_root: BOOLEAN
  74.     after_leaf: BOOLEAN
  75.  
  76. feature -- Cursor movement
  77.  
  78.     to_root is
  79.             -- Move cursor to a leaf(reachable from the current cell).
  80.         do
  81.             before_root := False
  82.             current_cell := container.root_cell
  83.             after_leaf := current_cell = Void
  84.             update_children_cursor
  85.         end;
  86.  
  87.     down is
  88.         require
  89.             no_leaf: not is_leaf
  90.             current_child_not_off: not child_off
  91.         do
  92.             if
  93.                 before_root
  94.             then
  95.                 current_cell := container.root_cell
  96.             else
  97.                 current_cell := children_cursor.current_cell
  98.             end
  99.             after_leaf := current_cell = Void
  100.             update_children_cursor
  101.         end
  102.  
  103.     up is
  104.         local
  105.             p: like current_cell
  106.         do
  107.             p := current_cell
  108.             if after_leaf then
  109.                 -- TODO: !!!
  110.                 check False end
  111.             else
  112.                 current_cell := current_cell.parent
  113.             end
  114.             update_children_cursor
  115.             children_cursor.go_to_cell (p)
  116.  
  117.         end
  118.  
  119.  
  120.     off: BOOLEAN is
  121.             -- Is there no item at cursor position?
  122.         do
  123.             Result := current_cell = Void
  124.         end
  125.  
  126.     item: G is
  127.             -- Item at cursor position
  128.         do
  129.             Result := current_cell.item
  130.         end;
  131.  
  132.     has (v: like item): BOOLEAN is
  133.             -- Has the subtree item v?
  134.             -- (comparsion by reference)
  135.         local
  136.             cs: like Current
  137.         do
  138.             if
  139.                 v = item
  140.             then
  141.                 Result := True
  142.             else
  143.                 if
  144.                     not is_leaf
  145.                 then
  146.                     from
  147.                         cs := clone (Current)
  148.                         cs.update_children_cursor
  149.                         cs.child_start
  150.                     until
  151.                         cs.child_off or Result = True
  152.                     loop
  153.                         cs.down
  154.                         if
  155.                             cs.has (v) = True
  156.                         then
  157.                             Result := True
  158.                         end
  159.                         cs.up
  160.                         cs.child_forth
  161.                     end
  162.                 end
  163.             end
  164.         end
  165.  
  166.  
  167. feature {DS_LINKED_TREE_CURSOR, DS_LINKED_TREE} -- Implementation
  168.     current_cell: DS_LINKED_TREE_CELL [G]
  169.  
  170.     children_cursor: DS_LINKED_TREE_CHILDREN_CURSOR [G]
  171.             -- cursor to children.
  172.             -- if you store a reference to this `children_cursor'
  173.             -- be aware that it does not automatically update
  174.             -- when you traverse up or down in the tree
  175.  
  176.     children: DS_LINKED_TREE_CHILDREN_LIST [G] is
  177.         do
  178.             Result := current_cell.children
  179.         end
  180.  
  181.  
  182. feature -- {NONE}
  183.     update_children_cursor is
  184.         do
  185.             if
  186.                 off
  187.             then
  188.                 children_cursor := Void
  189.             else
  190.                 children_cursor := current_cell.children.new_cursor
  191.             end
  192.         end
  193.  
  194. feature -- Children Cursor
  195.     child_index: INTEGER is
  196.             -- Index of current position
  197.         do
  198.             Result := children_cursor.index
  199.         end
  200.  
  201.     child_item: G is
  202.             -- Item at cursor position
  203.         do
  204.             Result := children_cursor.item
  205.         end
  206.     
  207. feature -- Status report
  208.  
  209.     child_after: BOOLEAN is
  210.             -- Is there invalid position to right of cursor?
  211.         do
  212.             Result := children_cursor.after
  213.         end
  214.  
  215.     child_before: BOOLEAN is
  216.             -- Is there no valid position to left of cursor?
  217.         do
  218.             Result := children_cursor.before
  219.         end
  220.  
  221.     is_first_child: BOOLEAN is
  222.             -- Is cursor on the first item?
  223.         do
  224.             Result := children_cursor.is_first
  225.         end
  226.  
  227.     is_last_child: BOOLEAN is
  228.             -- Is cursor on the last child_item?
  229.         do
  230.             Result := children_cursor.is_last
  231.         end
  232.  
  233.     is_valid_child: BOOLEAN is
  234.             -- Is cursor valid?
  235.         do
  236.             Result := children_cursor.is_valid
  237.         end
  238.  
  239.     child_off: BOOLEAN is
  240.             -- Is there no child_item at cursor position?
  241.         do
  242.             Result := children_cursor.off
  243.         end
  244.  
  245.     valid_child_index (i: INTEGER): BOOLEAN is
  246.             -- Is `i' a valid index value?
  247.         do
  248.             Result := children_cursor.valid_index (i)
  249.         end
  250.     
  251. feature -- Cursor movement
  252.  
  253.     child_back is
  254.             -- Move cursor to previous position.
  255.         do
  256.             children_cursor.back
  257.         end
  258.  
  259.     child_finish is
  260.             -- Move cursor to last position.
  261.         do
  262.             children_cursor.finish
  263.         end
  264.  
  265.     child_forth is
  266.             -- Move cursor to next position.
  267.         do
  268.             children_cursor.forth
  269.         end
  270.  
  271.     child_go_to (i: INTEGER) is
  272.             -- Move cursor to `i'-th position.
  273.         do
  274.             children_cursor.go_to (i)
  275.         end
  276.  
  277.     child_search_back (v: G) is
  278.             -- Move to first position at or before current
  279.             -- position where `child_item' and `v' are equal.
  280.             -- (Use `searcher''s comparison criterion from `children_cursor.container'.)
  281.             -- Move `before' if not found.
  282.         do
  283.             children_cursor.search_back (v)
  284.         end
  285.  
  286.     child_search_forth (v: G) is
  287.             -- Move to first position at or after current
  288.             -- position where `child_item' and `v' are equal.
  289.             -- (Use `searcher''s comparison criterion from `children_cursor.container'.)
  290.             -- Move `after' if not found.
  291.         do
  292.             children_cursor.search_forth (v)
  293.         end
  294.  
  295.     child_start is
  296.             -- Move cursor to first position.
  297.         do
  298.             children_cursor.start
  299.         end
  300.     
  301. feature -- Element change
  302.  
  303.     child_put (v: G) is
  304.             -- Replace child_item at cursor position by `v'.
  305.         do
  306.             children_cursor.put (v)
  307.         end
  308. feature
  309.  
  310. end -- class DS_LINKED_TREE_CURSOR
  311.  
  312. --|-------------------------------------------------------------------------
  313. --| eXML, Eiffel XML Parser Toolkit
  314. --| Copyright (C) 1999  Andreas Leitner
  315. --| See the file forum.txt included in this package for licensing info.
  316. --|
  317. --| Comments, Questions, Additions to this library? please contact:
  318. --|
  319. --| Andreas Leitner
  320. --| Arndtgasse 1/3/5
  321. --| 8010 Graz
  322. --| Austria
  323. --| email: andreas.leitner@teleweb.at
  324. --| web: http://exml.dhs.org
  325. --|-------------------------------------------------------------------------