File: arrayed_collection.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (122 lines) | stat: -rw-r--r-- 3,172 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
-- This  file  is  free software,  which comes along with SmartEiffel.  This
-- software is distributed in the hope that it will be useful,  but  WITHOUT
-- ANY  WARRANTY;  without  even the  implied warranty of MERCHANTABILITY or
-- FITNESS FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
-- this header is kept unaltered, and a notification of the changes is added.
-- You  are  allowed  to  redistribute it and sell it, alone or as a part of
-- another product.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
--			   - University of Nancy 1 - FRANCE
-- Copyright(C) 2003:      INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
--			   - University of Nancy 2 - FRANCE
--
--		 Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
--			   Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
deferred class ARRAYED_COLLECTION[E]
   --
   -- Common root for ARRAY[E] and FIXED_ARRAY[E].
   --

inherit COLLECTION[E]

feature {ARRAYED_COLLECTION}

   storage: NATIVE_ARRAY[E]
         -- Internal access to storage location.

feature

   capacity: INTEGER
         -- Internal storage capacity in number of item.

   upper: INTEGER
         -- Upper index bound.

   subarray(min, max: INTEGER): like Current is
	 -- New collection consisting of items at indexes in [`min' .. `max'].
         -- Result has the same dynamic type as `Current'.
	 -- See also `slice'.
      require
         lower <= min
         max <= upper
         min <= max + 1
      deferred
      ensure
         same_dynamic_type(Result)
         Result.count = (max - min + 1)
         Result.lower = min or Result.lower = 0
      end

feature -- Implementation of deferred:

   first: like item is
      do
         Result := storage.item(0)
      end

   last: like item is
      do
         Result := item(upper)
      end

   add(element: like item; index: INTEGER) is
      do
         if index = upper + 1 then
            add_last(element)
         else
            add_last(element)
            move(index,upper - 1,1)
            put(element,index)
         end
      end

   remove_last is
      do
         upper := upper - 1
      end

   replace_all(old_value, new_value: like item) is
      do
         storage.replace_all(old_value,new_value,count - 1)
      end

   fast_replace_all(old_value, new_value: like item) is
      do
         storage.fast_replace_all(old_value,new_value,count - 1)
      end

feature -- Interfacing with C:

   to_external: POINTER is
         -- Gives C access into the internal `storage' of the ARRAY.
         -- Result is pointing the element at index `lower'.
         --
         -- NOTE: do not free/realloc the Result. Resizing of the array
         --       can makes this pointer invalid.
      require
         not is_empty
      do
         Result := storage.to_pointer
      ensure
         Result.is_not_null
      end

feature {ARRAYED_COLLECTION}

   set_upper(new_upper: like upper) is
      do
         upper := new_upper
      end

invariant

   capacity >= (upper - lower + 1)

   capacity > 0 implies storage.is_not_null

end -- ARRAYED_COLLECTION[E]