Advertisement
PhilDrummer

Assignment_9 03 LINKED_BAG.e

Nov 29th, 2014
2,756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 2.23 KB | None | 0 0
  1. class
  2.     LINKED_BAG [G]
  3.  
  4. feature -- Access
  5.  
  6.     occurrences (v: G): INTEGER
  7.             -- Number of occurrences of `v'.
  8.         local
  9.             c: BAG_CELL [G]
  10.         do
  11.             from
  12.                 c := first
  13.             until
  14.                 c = Void or else c.value = v
  15.             loop
  16.                 c := c.next
  17.             end
  18.             if c /= Void then
  19.                 Result := c.count
  20.             end
  21.         ensure
  22.             non_negative_result: Result >= 0
  23.         end
  24.  
  25. feature -- Element change
  26.  
  27.     add (v: G; n: INTEGER)
  28.             -- Add `n' copies of `v'.
  29.         require
  30.             n_positive: n > 0
  31.         local
  32.             next_cell, specific: BAG_CELL [G]
  33.         do
  34.             if
  35.                 occurrences (v) <= 0
  36.             then
  37.                 if
  38.                     first = void
  39.                 then
  40.                     create first.make (v)
  41.                     first.set_count (n)
  42.                     last := first
  43.                 else
  44.                     create next_cell.make (v)
  45.                     next_cell.set_count (n)
  46.                     last.set_next (next_cell)
  47.                     last := last.next
  48.                 end
  49.             else
  50.                 from
  51.                     specific := first
  52.                 until
  53.                     specific.value = v
  54.                 loop
  55.                     specific := specific.next
  56.                 end
  57.                 specific.set_count (specific.count + n)
  58.             end
  59.         ensure
  60.             n_more: occurrences (v) = old occurrences (v) + n
  61.         end
  62.  
  63.     remove (v: G; n: INTEGER)
  64.             -- Remove as many copies of `v' as possible, up to `n'.
  65.         require
  66.             n_positive: n > 0
  67.         local
  68.             specific: BAG_CELL [G]
  69.         do
  70.             from
  71.                 specific := first
  72.             until
  73.                 specific.value = v
  74.             loop
  75.                 specific := specific.next
  76.             end
  77.             if
  78.                 (specific.count - n) > 0
  79.             then
  80.                 specific.set_count (specific.count - n)
  81.             else
  82.                 from
  83.                     specific := first
  84.                 until
  85.                     specific.next.value = v
  86.                 loop
  87.                     specific := specific.next
  88.                 end
  89.                 specific.set_next (specific.next.next)
  90.             end
  91.         ensure
  92.             n_less: occurrences (v) = (old occurrences (v) - n).max (0)
  93.         end
  94.  
  95.     subtract (other: LINKED_BAG [G])
  96.             -- Remove all elements of `other'.
  97.         require
  98.             other_exists: other /= Void
  99.         local
  100.             iteration, specific: BAG_CELL [G]
  101.         do
  102.             from
  103.                 iteration := other.first
  104.             until
  105.                 iteration = void
  106.             loop
  107.                 if
  108.                     iteration /= void and then occurrences (iteration.value) > 0
  109.                 then
  110.                     remove (iteration.value, iteration.count)
  111.                 end
  112.                 iteration := iteration.next
  113.             end
  114.         end
  115.  
  116. feature {LINKED_BAG} -- Implementation
  117.  
  118.     first: BAG_CELL [G]
  119.             -- First cell.
  120.  
  121.     last: BAG_CELL [G]
  122.             -- Last cell.
  123.  
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement