PageRenderTime 20ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/iterator/internal/avl_tree_iterator.e

http://github.com/tybor/Liberty
Specman e | 118 lines | 100 code | 15 blank | 3 comment | 7 complexity | 003d1bbb366285ad63e2afa14f46cff9 MD5 | raw file
  1deferred class AVL_TREE_ITERATOR[E_]
  2
  3inherit
  4   RECYCLABLE
  5
  6--         4
  7--      2     6
  8--     1 3   5 7
  9
 10feature {}
 11   nodes: STACK[AVL_TREE_NODE[E_]]
 12   cur: AVL_TREE_NODE[E_]
 13
 14   go_leftmost (node: AVL_TREE_NODE[E_])
 15      require
 16         node = nodes.top
 17      local
 18         left: like node
 19      do
 20         left := node.left
 21         if left = Void then
 22            cur := node
 23         else
 24            nodes.push(left)
 25            go_leftmost(left)
 26         end
 27      end
 28
 29   go_next_node (node: AVL_TREE_NODE[E_])
 30      require
 31         node = nodes.top
 32      local
 33         right: like node
 34      do
 35         if cur = node then
 36            right := node.right
 37            if right /= Void then
 38               nodes.pop
 39               nodes.push(right)
 40               go_leftmost(right)
 41            else
 42               nodes.pop
 43               if nodes.is_empty then
 44                  cur := Void
 45               else
 46                  cur := nodes.top
 47               end
 48            end
 49         else
 50            cur := node
 51         end
 52      end
 53
 54   go_first (root: AVL_TREE_NODE[E_])
 55      do
 56         from
 57         until
 58            nodes.is_empty
 59         loop
 60            nodes.pop
 61         end
 62         nodes.push(root)
 63         go_leftmost(root)
 64      end
 65
 66   go_next
 67      require
 68         not nodes.is_empty
 69      do
 70         go_next_node(nodes.top)
 71      end
 72
 73feature {ANY}
 74   generation: INTEGER
 75
 76feature {RECYCLING_POOL}
 77   recycle
 78      do
 79         nodes_pool.recycle(nodes)
 80      end
 81
 82feature {}
 83   new_nodes: like nodes
 84      do
 85         if nodes_pool.is_empty then
 86            create Result.make
 87         else
 88            Result := nodes_pool.item
 89         end
 90      end
 91
 92   nodes_pool: RECYCLING_POOL[STACK[AVL_TREE_NODE[E_]]]
 93      local
 94         key: FIXED_STRING
 95      do
 96         Result := nodes_pool_memory
 97         if Result = Void then
 98            key := generating_type.intern
 99            Result ::= nodes_pools.fast_reference_at(key)
100            if Result = Void then
101               create Result.make
102               nodes_pools.add(Result, key)
103            end
104            nodes_pool_memory := Result
105         end
106      end
107
108   nodes_pool_memory: like nodes_pool
109
110   nodes_pools: HASHED_DICTIONARY[RECYCLING_POOL[STACK[AVL_TREE_NODE_ANY]], FIXED_STRING]
111      once
112         create Result.make
113      end
114
115invariant
116   nodes /= Void
117
118end -- class AVL_TREE_ITERATOR