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 an ascending sort. See typeSortEx().

NOTE : optional operation.

void typeSortEx(Type * self, int ascending)

NOTE : optional operation.

Perform a sort operation on the contents of vector self utilizing either generated of user supplied ordering functions. If the ascending is non-zero, perform the sorting in ascending order otherwise perform the soring in descending order.

Note that this operation is defined only if element type is orderable, e.g. has equality testing and comparison operations defined.

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().

Instance Attribute Summary

Attributes inherited from Collection

#element, #it_ref

Attributes inherited from Type

#type, #type_ref

Instance Method Summary collapse

Methods inherited from Collection

#==, #comparable?, #copyable?, #destructible?, #entities, #hash, #hashable?, #initializable?

Methods inherited from Type

#==, #abort, #assert, #calloc, coerce, #comparable?, #copyable?, #destructible?, #entities, #extern, #free, #hash, #hashable?, #initializable?, #inline, #malloc, #method_missing, #orderable?, #prefix, #private?, #public?, #static, #static?, #write_decls, #write_defs, #write_intf

Methods inherited from Code

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

Constructor Details

#initialize(*args) ⇒ Vector

Returns a new instance of Vector.



132
133
134
135
136
137
# File 'lib/autoc/collection/vector.rb', line 132

def initialize(*args)
  super
  # Override the default type constructor as the Vector's requires one extra parameter
  # Note that this makes the Vector instance non-constructible
  @ctor = define_redirector(:ctor, Function::Signature.new([type_ref^:self, :size_t^:element_count]))
end

Dynamic Method Handling

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

Instance Method Details

#constructible?Boolean

No default constructor provided

Returns:

  • (Boolean)


140
# File 'lib/autoc/collection/vector.rb', line 140

def constructible?; false end

#write_impls(stream, define) ⇒ Object



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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/autoc/collection/vector.rb', line 218

def write_impls(stream, define)
  super
  stream << %$
    static void #{allocate}(#{type_ref} 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} #{ctor.definition} {
      size_t index;
      #{assert}(self);
      #{allocate}(self, element_count);
      for(index = 0; index < #{size}(self); ++index) {
        #{element.ctor("self->values[index]")};
      }
    }
    #{define} #{dtor.definition} {
      size_t index;
      #{assert}(self);
      for(index = 0; index < #{size}(self); ++index) {
        #{element.dtor("self->values[index]")};
      }
      #{free}(self->values);
    }
    #{define} #{copy.definition} {
      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} #{equal.definition} {
      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} #{identify.definition} {
      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;
    }
    #{define} void #{resize}(#{type_ref} 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_ref} values = (#{element.type_ref})#{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;
      }
    }
  $
  stream << %$
    static int #{ascend}(void* lp_, void* rp_) {
      #{element.type_ref} lp = (#{element.type_ref})lp_;
      #{element.type_ref} rp = (#{element.type_ref})rp_;
      if(#{element.equal("*lp", "*rp")}) {
        return 0;
      } else if(#{element.less("*lp", "*rp")}) {
        return -1;
      } else {
        return +1;
      }
    }
    static int #{descend}(void* lp_, void* rp_) {
      return -#{ascend}(lp_, rp_);
    }
    #{define} void #{sortEx}(#{type_ref} self, int ascending) {
      typedef int (*F)(const void*, const void*);
      #{assert}(self);
      qsort(self->values, #{size}(self), sizeof(#{element.type}), ascending ? (F)#{ascend} : (F)#{descend});
    }
  $ if element.orderable?
end

#write_intf_decls(stream, declare, define) ⇒ Object



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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/autoc/collection/vector.rb', line 163

def write_intf_decls(stream, declare, define)
  super
  stream << %$
    #{declare} #{ctor.declaration};
    #{declare} #{dtor.declaration};
    #{declare} #{copy.declaration};
    /* TODO #{copyRange}() */
    #{declare} #{equal.declaration};
    #{declare} #{identify.declaration};
    #{declare} void #{resize}(#{type_ref}, size_t);
    #{define} size_t #{size}(#{type_ref} self) {
      #{assert}(self);
      return self->element_count;
    }
    #{define} int #{within}(#{type_ref} self, size_t index) {
      #{assert}(self);
      return index < #{size}(self);
    }
    #{define} #{element.type} #{get}(#{type_ref} 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_ref} self, size_t index, #{element.type} value) {
      #{assert}(self);
      #{assert}(#{within}(self, index));
      #{element.dtor("self->values[index]")};
      #{element.copy("self->values[index]", "value")};
    }
    #define #{itCtor}(self, type) #{itCtorEx}(self, type, 1)
    #{define} void #{itCtorEx}(#{it_ref} self, #{type_ref} vector, int forward) {
      #{assert}(self);
      #{assert}(vector);
      self->vector = vector;
      self->forward = forward;
      self->index = forward ? -1 : #{size}(vector);
    }
    #{define} int #{itMove}(#{it_ref} self) {
      #{assert}(self);
      if(self->forward) ++self->index; else --self->index;
      return #{within}(self->vector, self->index);
    }
    #{define} #{element.type} #{itGet}(#{it_ref} self) {
      #{assert}(self);
      return #{get}(self->vector, self->index);
    }
  $
  stream << %$
    #define #{sort}(self) #{sortEx}(self, 1)
    #{declare} void #{sortEx}(#{type_ref}, int);
  $ if element.orderable?
end

#write_intf_types(stream) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/autoc/collection/vector.rb', line 142

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