Class: AutoC::Vector

Inherits:
Collection show all
Defined in:
lib/autoc/collection/vector.rb

Overview

Vector is an ordered random access sequence container.

The collection’s C++ counterpart is std::vector<> template class.

Generated C interface

Collection management

void typeCopy(Type * dst, Type * src)

Create a new vector dst filled with the contents of src. A copy operation is performed on every element in src.

NOTE: Previous contents of dst is overwritten.

void typeCtor(Type * self, size_t size)

Create a new vector self of size size. The elements are initialized with either supplied or generated default parameterless constructor.

WARNING: size must be greater than zero.

NOTE: Previous contents of self is overwritten.

void typeDtor(Type * self)

Destroy vector self. Stored elements are destroyed as well by calling the respective destructors.

int typeEqual(Type * lt, Type * rt)

Return non-zero value if vectors lt and rt are considered equal by contents and zero value otherwise.

size_t typeIdentify(Type * self)

Return hash code for vector self.

Basic operations

E typeGet(Type * self, size_t index)

Return a copy of the element stored in self at position index.

WARNING: index must be a valid index otherwise the behavior is undefined. See typeWithin().

void typeResize(Type * self, size_t size)

Set new size of vector self to size.

If new size is greater than the old one, extra elements are created with default parameterless constructors. If new size is smaller the the old one, excessive elements are destroyed.

WARNING: size must be greater than zero.

void typeSet(Type * self, size_t index, E what)

Store a copy of the element what in vector self at position index destroying previous contents.

WARNING: index must be a valid index otherwise the behavior is undefined. See typeWithin().

size_t typeSize(Type * self)

Return number of elements stored in vector self.

void typeSort(Type * self)

Perform a sorting operation on the contents of vector self utilizing either generated of user supplied ordering functions.

int typeWithin(Type * self, size_t index)

Return non-zero value if index is a valid index and zero value otherwise. Valid index belongs to the range 0 … typeSize()-1.

Iteration

void itCtor(IteratorType * it, Type * self)

Create a new forward iterator it on vector self.

NOTE: Previous contents of it is overwritten.

void itCtorEx(IteratorType * it, Type * self, int forward)

Create a new iterator it on vector self. Non-zero value of forward specifies a forward iterator, zero value specifies a backward iterator.

NOTE: Previous contents of it is overwritten.

int itMove(IteratorType * it)

Advance iterator position of it and return non-zero value if new position is valid and zero value otherwise.

E itGet(IteratorType * it)

Return a copy of current element pointed to by the iterator it.

WARNING: current position must be valid otherwise the behavior is undefined. See itMove().

Constant Summary

Instance Attribute Summary

Attributes inherited from Collection

#element

Attributes inherited from Type

#type

Instance Method Summary (collapse)

Methods inherited from Collection

coerce, #copy, #dtor, #entities, #equal, #identify, #initialize, #less

Methods inherited from Type

#abort, #assert, #calloc, #entities, #extern, #free, #initialize, #inline, #malloc, #method_missing, #static, #write_decls, #write_defs, #write_intf

Methods inherited from Code

#attach, #entities, #priority, #source_size, #write_decls, #write_defs, #write_intf

Constructor Details

This class inherits a constructor from AutoC::Collection

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AutoC::Type

Instance Method Details

- (Object) ctor(*args)



120
121
122
# File 'lib/autoc/collection/vector.rb', line 120

def ctor(*args)
  args.empty? ? super() : raise("#{self.class} provides no default constructor")
end

- (Object) write_exported_declarations(stream, declare, define)



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/autoc/collection/vector.rb', line 145

def write_exported_declarations(stream, declare, define)
  stream << %$
    #{declare} void #{ctor}(#{type}*, size_t);
    #{declare} void #{dtor}(#{type}*);
    #{declare} void #{copy}(#{type}*, #{type}*);
    #{declare} int #{equal}(#{type}*, #{type}*);
    #{declare} void #{resize}(#{type}*, size_t);
    #{declare} size_t #{identify}(#{type}*);
    #{define} size_t #{size}(#{type}* self) {
      #{assert}(self);
      return self->element_count;
    }
    #{define} int #{within}(#{type}* self, size_t index) {
      #{assert}(self);
      return index < #{size}(self);
    }
    #{define} #{element.type} #{get}(#{type}* self, size_t index) {
      #{element.type} result;
      #{assert}(self);
      #{assert}(#{within}(self, index));
      #{element.copy("result", "self->values[index]")};
      return result;
    }
    #{define} void #{set}(#{type}* self, size_t index, #{element.type} value) {
      #{assert}(self);
      #{assert}(#{within}(self, index));
      #{element.dtor("self->values[index]")};
      #{element.copy("self->values[index]", "value")};
    }
    #{declare} void #{sort}(#{type}*);
    #define #{itCtor}(self, type) #{itCtorEx}(self, type, 1)
    #{define} void #{itCtorEx}(#{it}* self, #{type}* vector, int forward) {
      #{assert}(self);
      #{assert}(vector);
      self->vector = vector;
      self->forward = forward;
      self->index = forward ? -1 : #{size}(vector);
    }
    #{define} int #{itMove}(#{it}* self) {
      #{assert}(self);
      if(self->forward) ++self->index; else --self->index;
      return #{within}(self->vector, self->index);
    }
    #{define} #{element.type} #{itGet}(#{it}* self) {
      #{assert}(self);
      return #{get}(self->vector, self->index);
    }
  $
end

- (Object) write_exported_types(stream)



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/autoc/collection/vector.rb', line 124

def write_exported_types(stream)
  stream << %$
    /***
    **** #{type}<#{element.type}> (#{self.class})
    ***/
  $ if public?
  stream << %$
    typedef struct #{type} #{type};
    typedef struct #{it} #{it};
    struct #{type} {
      #{element.type}* values;
      size_t element_count;
    };
    struct #{it} {
      #{type}* vector;
      int index;
      int forward;
    };
  $
end

- (Object) write_implementations(stream, define)



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/autoc/collection/vector.rb', line 195

def write_implementations(stream, define)
  stream << %$
    static void #{allocate}(#{type}* self, size_t element_count) {
      #{assert}(self);
      #{assert}(element_count > 0);
      self->element_count = element_count;
      self->values = (#{element.type}*)#{malloc}(element_count*sizeof(#{element.type})); #{assert}(self->values);
    }
    #{define} void #{ctor}(#{type}* self, size_t element_count) {
      size_t index;
      #{assert}(self);
      #{allocate}(self, element_count);
      for(index = 0; index < #{size}(self); ++index) {
        #{element.ctor("self->values[index]")};
      }
    }
    #{define} void #{dtor}(#{type}* self) {
      size_t index;
      #{assert}(self);
      for(index = 0; index < #{size}(self); ++index) {
        #{element.dtor("self->values[index]")};
      }
      #{free}(self->values);
    }
    #{define} void #{copy}(#{type}* dst, #{type}* src) {
      size_t index, size;
      #{assert}(src);
      #{assert}(dst);
      #{allocate}(dst, size = #{size}(src));
      for(index = 0; index < size; ++index) {
        #{element.copy("dst->values[index]", "src->values[index]")};
      }
    }
    #{define} int #{equal}(#{type}* lt, #{type}* rt) {
      size_t index, size;
      #{assert}(lt);
      #{assert}(rt);
      if(#{size}(lt) == (size = #{size}(rt))) {
        for(index = 0; index < size; ++index) {
          if(!#{element.equal("lt->values[index]", "rt->values[index]")}) return 0;
        }
        return 1;
      } else
        return 0;
    }
    #{define} void #{resize}(#{type}* self, size_t new_element_count) {
      size_t index, element_count, from, to;
      #{assert}(self);
      if((element_count = #{size}(self)) != new_element_count) {
        #{element.type}* values = (#{element.type}*)#{malloc}(new_element_count*sizeof(#{element.type})); #{assert}(values);
        from = AUTOC_MIN(element_count, new_element_count);
        to = AUTOC_MAX(element_count, new_element_count);
        for(index = 0; index < from; ++index) {
          values[index] = self->values[index];
        }
        if(element_count > new_element_count) {
          for(index = from; index < to; ++index) {
            #{element.dtor("self->values[index]")};
          }
        } else {
          for(index = from; index < to; ++index) {
            #{element.ctor("values[index]")};
          }
        }
        #{free}(self->values);
        self->values = values;
        self->element_count = new_element_count;
      }
    }
    #{define} size_t #{identify}(#{type}* self) {
      size_t index, result = 0;
      #{assert}(self);
      for(index = 0; index < self->element_count; ++index) {
        result ^= #{element.identify("self->values[index]")};
        result = AUTOC_RCYCLE(result);
      }
      return result;
    }
    static int #{comparator}(void* lp_, void* rp_) {
      #{element.type}* lp = (#{element.type}*)lp_;
      #{element.type}* rp = (#{element.type}*)rp_;
      if(#{element.equal("*lp", "*rp")}) {
        return 0;
      } else if(#{element.less("*lp", "*rp")}) {
        return -1;
      } else {
        return +1;
      }
    }
    #{define} void #{sort}(#{type}* self) {
      typedef int (*F)(const void*, const void*);
      #{assert}(self);
      qsort(self->values, #{size}(self), sizeof(#{element.type}), (F)#{comparator});
    }
  $
end