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

/tutorial/iterator/example1.e

http://github.com/tybor/Liberty
Specman e | 111 lines | 96 code | 11 blank | 4 comment | 0 complexity | 22917a104ea0d7fa52c9fe00a5b5eb8d MD5 | raw file
  1class EXAMPLE1
  2   --
  3   -- Using ITERATOR allows the user to traverse any aggregate object
  4   -- in a more abstract way.
  5   --
  6
  7create {ANY}
  8   make
  9
 10feature {ANY}
 11   my_loop (type: STRING; my_iterator: ITERATOR[CHARACTER])
 12      local
 13         element: CHARACTER
 14      do
 15         io.put_string("Loop on ")
 16         io.put_string(type)
 17         io.put_string(": ")
 18         from
 19            my_iterator.start
 20         until
 21            my_iterator.is_off
 22         loop
 23            element := my_iterator.item
 24            io.put_character(element)
 25            my_iterator.next
 26         end
 27
 28         io.put_new_line
 29      end
 30
 31feature {}
 32   make
 33      do
 34         use_iterator_on_string
 35         use_iterator_on_array
 36         use_iterator_on_fixed_array
 37         use_iterator_on_linked_list
 38         use_iterator_on_two_way_linked_list
 39         io.put_string("Do you like ITERATORs ?%N")
 40         io.put_string("So let's see what happends with a DICTIONARY.%N")
 41         use_iterator_on_dictionary
 42         io.put_string("And this is not a bug!%N")
 43      end
 44
 45   use_iterator_on_string
 46      local
 47         iterator: ITERATOR[CHARACTER]
 48      do
 49         iterator := ("foo").new_iterator
 50         my_loop("STRING", iterator)
 51      end
 52
 53   use_iterator_on_array
 54      local
 55         iterator: ITERATOR[CHARACTER]
 56      do
 57         iterator := {FAST_ARRAY[CHARACTER] << 'b', 'a', 'r' >> }.new_iterator
 58         my_loop("ARRAY", iterator)
 59      end
 60
 61   use_iterator_on_fixed_array
 62      local
 63         iterator: ITERATOR[CHARACTER]; my_fa: FAST_ARRAY[CHARACTER]
 64      do
 65         create my_fa.with_capacity(4)
 66         my_fa.add_last('f')
 67         my_fa.add_last('O')
 68         my_fa.add_last('o')
 69         iterator := my_fa.new_iterator
 70         my_loop("FAST_ARRAY", iterator)
 71      end
 72
 73   use_iterator_on_linked_list
 74      local
 75         iterator: ITERATOR[CHARACTER]; ll: LINKED_LIST[CHARACTER]
 76      do
 77         create ll.make
 78         ll.add_last('B')
 79         ll.add_last('a')
 80         ll.add_last('R')
 81         iterator := ll.new_iterator
 82         my_loop("LINKED_LIST", iterator)
 83      end
 84
 85   use_iterator_on_two_way_linked_list
 86      local
 87         iterator: ITERATOR[CHARACTER]; twll: TWO_WAY_LINKED_LIST[CHARACTER]
 88      do
 89         create twll.make
 90         twll.add_last('F')
 91         twll.add_last('o')
 92         twll.add_last('O')
 93         iterator := twll.new_iterator
 94         my_loop("TWO_WAY_LINKED_LIST", iterator)
 95      end
 96
 97   use_iterator_on_dictionary
 98      local
 99         iterator: ITERATOR[CHARACTER]; d: DICTIONARY[CHARACTER, CHARACTER]
100      do
101         create {HASHED_DICTIONARY[CHARACTER, CHARACTER]} d.make
102         d.put('o', 'B')
103         d.put('f', 'a')
104         d.put('O', 'R')
105         iterator := d.new_iterator_on_items
106         my_loop("DICTIONARY (items)", iterator)
107         iterator := d.new_iterator_on_keys
108         my_loop("DICTIONARY (keys)", iterator)
109      end
110
111end -- class EXAMPLE1