Class: AutoC::Vector
- Inherits:
-
Collection
- Object
- Code
- Type
- Collection
- AutoC::Vector
- 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
- cols=“2*”
-
|=== |void ~type~Copy(Type *
dst, Type *src) | Create a new vectordstfilled with the contents ofsrc. A copy operation is performed on every element insrc.NOTE: Previous contents of
dstis overwritten.|void ~type~Ctor(Type *
self, size_tsize) | Create a new vectorselfof sizesize. The elements are initialized with either supplied or generated default parameterless constructor.WARNING:
sizemust be greater than zero.NOTE: Previous contents of
selfis overwritten.|void ~type~Dtor(Type *
self) | Destroy vectorself. Stored elements are destroyed as well by calling the respective destructors.|int ~type~Equal(Type *
lt, Type *rt) | Return non-zero value if vectorsltandrtare considered equal by contents and zero value otherwise.|size_t ~type~Identify(Type *
self) | Return hash code for vectorself. |===Basic operations
- cols=2*
-
|=== |E ~type~Get(Type *
self, size_tindex) | Return a copy of the element stored inselfat positionindex.WARNING:
indexmust be a valid index otherwise the behavior is undefined. See ~type~Within().|void ~type~Resize(Type *
self, size_tsize) | Set new size of vectorselftosize.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:
sizemust be greater than zero.|void ~type~Set(Type *
self, size_tindex, Ewhat) |Store a copy of the element
whatin vectorselfat positionindexdestroying previous contents.WARNING:
indexmust be a valid index otherwise the behavior is undefined. See ~type~Within().|size_t ~type~Size(Type *
self) | Return number of elements stored in vectorself.|void ~type~Sort(Type *
self) | Perform a sorting operation on the contents of vectorselfutilizing either generated of user supplied ordering functions.|int ~type~Within(Type *
self, size_tindex) | Return non-zero value ifindexis a valid index and zero value otherwise. Valid index belongs to the range 0 … ~type~Size()-1. |===Iteration
- cols=2*
-
|=== |void ~it~Ctor(IteratorType *
it, Type *self) | Create a new forward iteratoriton vectorself.NOTE: Previous contents of
itis overwritten.|void ~it~CtorEx(IteratorType *
it, Type *self, intforward) | Create a new iteratoriton vectorself. Non-zero value offorwardspecifies a forward iterator, zero value specifies a backward iterator.NOTE: Previous contents of
itis overwritten.|int ~it~Move(IteratorType *
it) | Advance iterator position ofitand return non-zero value if new position is valid and zero value otherwise.|E ~it~Get(IteratorType *
it) | Return a copy of current element pointed to by the iteratorit.WARNING: current position must be valid otherwise the behavior is undefined. See ~it~Move(). |===
Constant Summary
Constants inherited from Type
Instance Attribute Summary
Attributes inherited from Collection
Attributes inherited from Type
Instance Method Summary collapse
- #ctor(*args) ⇒ Object
- #write_exported_declarations(stream, declare, define) ⇒ Object
- #write_exported_types(stream) ⇒ Object
- #write_implementations(stream, define) ⇒ Object
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
#ctor(*args) ⇒ Object
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 |
#write_exported_declarations(stream, declare, define) ⇒ Object
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 |
#write_exported_types(stream) ⇒ Object
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 |
#write_implementations(stream, define) ⇒ Object
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 |