~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty-proposed

« back to all changes in this revision

Viewing changes to yo/containers/array.yo

  • Committer: Package Import Robot
  • Author(s): Frank B. Brokken
  • Date: 2013-05-30 13:32:18 UTC
  • mfrom: (1.1.24)
  • Revision ID: package-import@ubuntu.com-20130530133218-k39mr5uredd093jr
Tags: 9.7.2-1
New upstream release, repairs several minor left-over flaws.
This release incorporates 9.7.0 and 9.7.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
The ti(array) class implements a 
 
2
    hi(array: fixed-size) i(fixed-size array).  Before using the tt(array)
 
3
container the tthi(array) header file must have been included.
 
4
 
 
5
    To define a tt(std::array) both the data type of its elements and its size
 
6
must be specified: the data type is given after an opening angular bracket,
 
7
immediately following the `tt(array)' container name. The array's size is
 
8
provided after the data type specification. Finally, a closing angular bracket
 
9
completes the array's type. Specifications like this are common practice with
 
10
containers.  The combination of tt(array), type and size defines a
 
11
em(type). As a result, tt(array<string, 4>) defines another type than
 
12
tt(array<string, 5>), and a function explicitly defining an tt(array<Type, N>)
 
13
parameter will not accept an tt(array<Type, M>) argument if tt(N) and tt(M)
 
14
are unequal.
 
15
 
 
16
The array's size may may be defined as 0 (although such an array probably has
 
17
little use as it cannot store any element). The elements of an array are
 
18
stored contiguously. If tt(array<Type, N> arr) has been defined, then tt(&a[n]
 
19
+ m == &a[n + m), assuming tt(0 <= n < N) and  assuming tt(0 <= n + m < N).
 
20
 
 
21
    The following constructors, operators, and member functions are available:
 
22
    itemization(
 
23
    it() hi(array constructors) Constructors:
 
24
        itemization(
 
25
        it() A tt(array) may be constructed with a fixed number tt(N) of
 
26
default elements:
 
27
        verb(
 
28
    array<string, N> object;
 
29
        )
 
30
        it() An initial subset of the elements of an array may be 
 
31
initialized using a brace delimited initializer list:
 
32
        verb(
 
33
    array<double, 4> dArr = {1.2, 2.4};
 
34
        )
 
35
    Here tt(dArr) is defined as an array of 4 element, with tt(dArr[0]) and
 
36
tt(dArr[1]) initialized to, respectively 1.2 and 2.4, and tt(dArr[2]) and
 
37
tt(dArr[3]) initialized to 0.  A attractive characteristic of arrays (and
 
38
other containers) is that containers initialize hi(containers: initialization)
 
39
their data elements to the data type's default value. The data type's
 
40
em(default constructor) is used for this i(initialization). With non-class
 
41
data types the value 0 is used.  So, for an tt(array<double, 4>) array we know
 
42
that all but its explicitly initialized elements are initialized to
 
43
zero. 
 
44
        it() A array may be initialized using a copy constructor:
 
45
        verb(
 
46
    extern array<string, 5> container;
 
47
    array<string, 5> object(container);
 
48
        )
 
49
        )        
 
50
    it() In addition to the standard operators for containers, the tt(array)
 
51
supports the i(index operator), which can be used to retrieve or reassign
 
52
individual elements of the array. Note that the elements which are indexed
 
53
must exist. For example, having defined an empty array a statement like
 
54
tt(iarr[0] = 18) produces an error, as the array is empty. Note that 
 
55
tt(operator[]) does em(not) respect its
 
56
 i(array bounds). If you want run-time array bound checking, use the array's
 
57
tt(at) member.
 
58
    it() The tt(array) class offers the following
 
59
 hi(array: member functions) member functions:
 
60
        itemization(
 
61
        ithtq(at)(Type &at(size_t idx))
 
62
           (returns a reference to the array's element at index position
 
63
            tt(idx). If tt(idx) exceeds the array's size a
 
64
            tt(std::out_of_range) exception is thrown.)
 
65
        ithtq(back)(Type &back())
 
66
           (returns a reference to the last element in the array. It is the
 
67
            i(responsibility of the programmer) to use the member only if the
 
68
            array is not empty.)
 
69
        ithtq(begin)(array::iterator begin())
 
70
           (returns an i(iterator) pointing to the first
 
71
            element in the array, returning tt(end) if the array is empty.)
 
72
        ithtq(cbegin)(array::const_iterator cbegin())
 
73
           (returns a i(const_iterator) pointing to the first element in the
 
74
            array, returning tt(cend) if the array is empty.)
 
75
        ithtq(cend)(array::const_iterator cend())
 
76
           (returns a i(const_iterator) pointing just beyond the array's last
 
77
            element.)
 
78
        ithtq(crbegin)(array::const_reverse_iterator crbegin())
 
79
           (returns a i(const_reverse_iterator) pointing to the last element
 
80
            in the array, returning tt(crend) if the array is empty.)
 
81
        ithtq(crend)(array::const_reverse_iterator crend())
 
82
           (returns a i(const_reverse_iterator) pointing just before the
 
83
            array's first element.)
 
84
        ithtq(data)(value_type *data())
 
85
           (returns a pointer to the array's first data element. With a const
 
86
            array a tt(value_type const *) is returned.)
 
87
        ithtq(empty)(bool empty())
 
88
           (returns tt(true) if the array contains no elements.)
 
89
        ithtq(end)(array::iterator end())
 
90
           (returns an iterator pointing beyond the last element in the
 
91
            array.)
 
92
        ithtq(fill)(void fill(Type const &item))
 
93
           (fills all the array's elements with a copy of tt(item))
 
94
        ithtq(front)(Type &front())
 
95
           (returns a reference to the first element in the array. It is the
 
96
            responsibility of the programmer to use the member only if
 
97
            the array is not empty.)
 
98
        ithtq(rbegin)(array::reverse_iterator rbegin())
 
99
           (hi(reverse_iterator) this member returns an iterator pointing to
 
100
            the last element in the array.)
 
101
        ithtq(rend)(array::reverse_iterator rend())
 
102
           (returns an iterator pointing before the first element in the
 
103
            array.)
 
104
        ithtq(size)(constexpr size_t size())
 
105
           (returns the number of elements the array contains.)
 
106
        ithtq(swap)(void swap(<array<Type, N> &other))
 
107
           (swaps the contents of the current and other array. The array
 
108
            other's data type and size must be equal to the data type and size
 
109
            of the object calling tt(swap).)
 
110
        )
 
111
    )
 
112
    Using an tt(array) rather than a standard bf(C) style array offers several
 
113
advantages:
 
114
    itemization(
 
115
    it() All its elements are immediately initialized;
 
116
    it() Introspection is possible (e.g., tt(size) can be used);
 
117
    it() The tt(array) container can be used in the context of templates,
 
118
        there code is developed that operates on data types that become
 
119
        available only after the code itself has been developed;
 
120
    it() Since tt(array) supports reverse iterators, it can be immediately be
 
121
        used with generic algorithms performing `reversed' operations (e.g.,
 
122
        to perform a descending rather than ascending sort (cf. section
 
123
        ref(SORT)))
 
124
    )
 
125
    In general, when looking for a sequential data structure, the tt(array) or
 
126
tt(vector) (introduced in the next section) should be your `weapon of
 
127
choice'. Only if these containers demonstrably do not fit the problem at hand
 
128
you should use another type of container.
 
129
 
 
130