Class DS_ARRAYED_STACK PreviousNext

note
description:

    "Stacks (Last-In, First-Out) implemented with arrays"

library:    "Gobo Eiffel Structure Library"
author:     "Eric Bezault <ericb@gobosoft.com>"
copyright:  "Copyright (c) 1999-2001, Eric Bezault and others"
license:    "MIT License"
class interface
DS_ARRAYED_STACK [G]
inherit
DS_STACK [G]
    DS_DISPENSER [G]
        DS_SEARCHABLE [G]
            DS_CONTAINER [G]
DS_RESIZABLE [G]
    DS_CONTAINER [G]
create
make (n: INTEGER)
        -- Create an empty stack and allocate
        -- memory space for at least n items.
        -- Use `=' as comparison criterion.
    require
        positive_n: n >= 0
    ensure
        empty: is_empty
        capacity_set: capacity = n
make_equal (n: INTEGER)
        -- Create an empty stack and allocate
        -- memory space for at least n items.
        -- Use equal as comparison criterion.
    require
        positive_n: n >= 0
    ensure
        empty: is_empty
        capacity_set: capacity = n
make_default
        -- Create an empty stack and allocate memory
        -- space for at least default_capacity items.
        -- Use `=' as comparison criterion.
        -- (From DS_CONTAINER.)
    ensure
        empty: is_empty
        capacity_set: capacity = default_capacity
feature -- Access
item: G
        -- Item at top of stack
        -- (From DS_DISPENSER.)
    require
        not_empty: not is_empty
equality_tester: DS_EQUALITY_TESTER [G]
        -- Equality tester;
        -- A void equality tester means that `='
        -- will be used as comparison criterion.
        -- (From DS_SEARCHABLE.)
feature -- Measurement
count: INTEGER
        -- Number of items in stack
        -- (From DS_CONTAINER.)
capacity: INTEGER
        -- Maximum number of items in stack
        -- (From DS_RESIZABLE.)
default_capacity: INTEGER
        -- Initial capacity in make_default
        -- (Default value: 10)
        -- (From DS_RESIZABLE.)
    ensure
        default_capacity_positive: Result >= 0
occurrences (v: G): INTEGER
        -- Number of times v appears in stack
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SEARCHABLE.)
    ensure
        positive: Result >= 0
        has: has (v) implies Result >= 1
feature -- Status report
has (v: G): BOOLEAN
        -- Does stack include v?
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SEARCHABLE.)
    ensure
        not_empty: Result implies not is_empty
is_empty: BOOLEAN
        -- Is stack empty?
        -- (From DS_CONTAINER.)
is_full: BOOLEAN
        -- Is list  full?
        -- (From DS_RESIZABLE.)
extendible (n: INTEGER): BOOLEAN
        -- May stack be extended with n items?
    require
        positive_n: n >= 0
    ensure
        enough_space: Result implies capacity >= count + n
same_items (v, u: G): BOOLEAN
        -- Are v and u considered equal?
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SEARCHABLE.)
same_equality_tester (other: DS_SEARCHABLE [G]): BOOLEAN
        -- Does container use the same comparison
        -- criterion as other?
        -- (From DS_SEARCHABLE.)
    require
        other_not_void: other /= Void
equality_tester_settable (a_tester: like equality_tester): BOOLEAN
        -- Can set_equality_tester be called with a_tester
        -- as argument in current state of container?
        -- (Default answer: True.)
        -- (From DS_SEARCHABLE.)
feature -- Comparison
is_equal (other: like Current): BOOLEAN
        -- Is current stack equal to other?
        -- (From GENERAL.)
    require
        other_not_void: other /= Void
    ensure
        consistent: standard_is_equal (other) implies Result
        same_type: Result implies same_type (other)
        symmetric: Result implies other.is_equal (Current)
        same_count: Result implies count = other.count
feature -- Duplication
copy (other: like Current)
        -- Copy other to current stack.
        -- (From GENERAL.)
    require
        other_not_void: other /= Void
        type_identity: same_type (other)
    ensure
        is_equal: is_equal (other)
feature -- Setting
set_equality_tester (a_tester: like equality_tester)
        -- Set equality_tester to a_tester.
        -- A void equality tester means that `='
        -- will be used as comparison criterion.
        -- (From DS_SEARCHABLE.)
    require
        equality_tester_settable: equality_tester_settable (a_tester)
    ensure
        equality_tester_set: equality_tester = a_tester
feature -- Element change
put (v: G)
        -- Push v on stack.
        -- (From DS_DISPENSER.)
    require
        extendible: extendible (1)
    ensure
        one_more: count = old count + 1
        pushed: item = v
force (v: G)
        -- Push v on stack.
        -- Resize stack if needed.
        -- (From DS_DISPENSER.)
    ensure
        one_more: count = old count + 1
        pushed: item = v
replace (v: G)
        -- Replace top item by v.
        -- (From DS_STACK.)
    require
        not_empty: not is_empty
    ensure
        same_count: count = old count
        replaced: item = v
extend (other: DS_LINEAR [G])
        -- Add items of other to stack.
        -- Add other.first first, etc.
        -- (From DS_DISPENSER.)
    require
        other_not_void: other /= Void
        extendible: extendible (other.count)
    ensure
        new_count: count = old count + other.count
append (other: DS_LINEAR [G])
        -- Add items of other to stack.
        -- Add other.first first, etc.
        -- Resize stack if needed.
        -- (From DS_DISPENSER.)
    require
        other_not_void: other /= Void
    ensure
        new_count: count = old count + other.count
feature -- Removal
remove
        -- Remove top item from stack.
        -- (From DS_DISPENSER.)
    require
        not_empty: not is_empty
    ensure
        one_less: count = old count - 1
prune (n: INTEGER)
        -- Remove n items from stack.
        -- (From DS_DISPENSER.)
    require
        valid_n: 0 <= n and n <= count
    ensure
        new_count: count = old count - n
keep (n: INTEGER)
        -- Keep n items in stack.
        -- (From DS_DISPENSER.)
    require
        valid_n: 0 <= n and n <= count
    ensure
        new_count: count = n
wipe_out
        -- Remove all items from stack.
        -- (From DS_CONTAINER.)
    ensure
        wiped_out: is_empty
feature -- Resizing
resize (n: INTEGER)
        -- Resize stack so that it can contain
        -- at least n items. Do not lose any item.
        -- (From DS_RESIZABLE.)
    require
        n_large_enough: n >= capacity
    ensure
        capacity_set: capacity = n
invariant
positive_count: count >= 0
empty_definition: is_empty = (count = 0)
    -- (From DS_CONTAINER.)
count_constraint: count <= capacity
full_definition: is_full = (count = capacity)
    -- (From DS_RESIZABLE.)
end -- class DS_ARRAYED_STACK

Copyright 1999, Eric Bezault
mailto:
ericb@gobosoft.com
http:
//www.gobosoft.com
Last Updated: 25 September 1999

HomeTocPreviousNext