Simple IFTI use

Kevin Bealer Kevin_member at pathlink.com
Sun Apr 2 22:19:38 PDT 2006


Often when working with D arrays, I miss the "doubling" effect of push_back from
C++ std::vector.  With IFTI, this is possible (trivial, really) in D.  I put
some code here - the idea is to use existing arrays, no special classes, but
have an "append" functionality, like vector::push_back().

: // -*- c++ -*-
:
: import std.string;
:
: template push_back(T) {
:     // The first arg is inout only for performance reasons.
:
:     void push_back(inout T elem, inout T[] vec, inout int vsize)
:     {
:         assert(vec.length >= vsize);
:
:         if (vec.length != vsize) {
:             vec.length = (vec.length
:                           ? vec.length * 2
:                           : 4); // arbitrary
:         }
:
:         vec[vsize++] = elem;
:     }
: };
:
: // Usage
:
: import simplevec;
:
: Foo[] stuff; int stuff_size;
:
: Foo f = new Foo;
:
: push_back(f, stuff, stuff_size);

Kevin





More information about the Digitalmars-d mailing list