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
void typeCopy(Type * |
Create a new vector NOTE: Previous contents of |
void typeCtor(Type * |
Create a new vector WARNING: NOTE: Previous contents of |
void typeDtor(Type * |
Destroy vector |
int typeEqual(Type * |
Return non-zero value if vectors |
size_t typeIdentify(Type * |
Return hash code for vector |
Basic operations
E typeGet(Type * |
Return a copy of the element stored in WARNING: |
void typeResize(Type * |
Set new size of vector 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: |
void typeSet(Type * |
Store a copy of the element WARNING: |
size_t typeSize(Type * |
Return number of elements stored in vector |
void typeSort(Type * |
Perform a sorting operation on the contents of vector |
int typeWithin(Type * |
Return non-zero value if |
Iteration
void itCtor(IteratorType * |
Create a new forward iterator NOTE: Previous contents of |
void itCtorEx(IteratorType * |
Create a new iterator NOTE: Previous contents of |
int itMove(IteratorType * |
Advance iterator position of |
E itGet(IteratorType * |
Return a copy of current element pointed to by the iterator WARNING: current position must be valid otherwise the behavior is undefined. See itMove(). |
Constant Summary
Instance Attribute Summary
Attributes inherited from Collection
Attributes inherited from Type
Instance Method Summary (collapse)
- - (Object) ctor(*args)
- - (Object) write_exported_declarations(stream, declare, define)
- - (Object) write_exported_types(stream)
- - (Object) write_implementations(stream, define)
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 |