stack4.e 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. -- Copyright (C) 2008-2018 Lorenzo Caminiti
  2. -- Distributed under the Boost Software License, Version 1.0 (see accompanying
  3. -- file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  4. -- See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  5. //[meyer97_stack4_e
  6. -- Extra spaces, newlines, etc. for visual alignment with this library code.
  7. indexing
  8. destription: "Dispenser with LIFO access policy and a fixed max capacity."
  9. class interface STACK4[G] creation make -- Interface only (no implementation).
  10. invariant
  11. count_non_negative: count >= 0
  12. count_bounded: count <= capacity
  13. empty_if_no_elements: empty = (count = 0)
  14. feature -- Initialization
  15. -- Allocate stack for a maximum of n elements.
  16. make(n: INTEGER) is
  17. require
  18. non_negative_capacity: n >= 0
  19. ensure
  20. capacity_set: capacity = n
  21. end
  22. feature -- Access
  23. -- Max number of stack elements.
  24. capacity: INTEGER
  25. -- Number of stack elements.
  26. count: INTEGER
  27. -- Top element.
  28. item: G is
  29. require
  30. not_empty: not empty -- i.e., count > 0
  31. end
  32. feature -- Status report
  33. -- Is stack empty?
  34. empty: BOOLEAN is
  35. ensure
  36. empty_definition: result = (count = 0)
  37. end
  38. -- Is stack full?
  39. full: BOOLEAN is
  40. ensure
  41. full_definition: result = (count = capacity)
  42. end
  43. feature -- Element change
  44. -- Add x on top.
  45. put(x: G) is
  46. require
  47. not_full: not full
  48. ensure
  49. not_empty: not empty
  50. added_to_top: item = x
  51. one_more_item: count = old count + 1
  52. end
  53. -- Remove top element.
  54. remove is
  55. require
  56. not_empty: not empty -- i.e., count > 0
  57. ensure
  58. not_full: not full
  59. one_fewer_item: count = old count - 1
  60. end
  61. end -- class interface STACK4
  62. -- End.
  63. //]