Changeset a8e2215


Ignore:
Timestamp:
Feb 6, 2025, 2:50:36 PM (8 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
cd28605
Parents:
301e9f7
Message:

Tidy vector-iterator POC and remedy its warnings

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/collections/vector2.hfa

    r301e9f7 ra8e2215  
    1313// Update Count     : 2
    1414//
     15
     16// This libcfa header and the vector-demo test form a standalone POC.
     17// The 'vector' is really a host for a style of iterator.
     18// The iterators represent a style proposed to be part of "how CFA collections should do iterators."
     19// The vector API is not built out, beyond what's needed to test the iterators.
    1520
    1621#pragma once
     
    7580    void ?{}( vector_exit(T) &, vector(T) * ) = void;
    7681
    77     void ^?{}( vector_exit(T) & );
    7882    void ?{}( vector_exit(T) &, vector_transit(T) & );
    7983    void ?{}( vector_exit(T) &, vector_exit(T) & );
     84    void ^?{}( vector_exit(T) & );
    8085
    8186    T ?`val( vector_exit(T) & src );
    8287    void ?=?( vector_exit(T) & dst, T val );
    83     T & ?=?( T & dst, vector_exit(T) & src );
    84     void ?*=?( T & dst, vector_exit(T) & src );
     88    void ?*=?( T & dst, vector_exit(T) & src ); // wish it were ?=?, but that's not working
    8589
    8690    bool ?`moveNext( vector_exit(T) & it );
     
    141145    void ^?{}( vector_transit(T) & ) {}
    142146
    143 
    144     vector_transit(T) ?[?]( vector( T ) & vec, ptrdiff_t idx ) {
    145         // call autogen constructor deleted at end of hfa
    146         vector_transit(T) ret = { & vec, idx };
    147         return ret;
    148     }
    149147
    150148    T & findElemMem_$( vector(T) & v, ptrdiff_t idx ) {
     
    176174        this.invec_$->exit_refcount_$ ++;
    177175    }
     176
    178177    void ?{}( vector_exit(T) & this, vector_exit(T) & src ){
    179178        ( this.invec_$ ){ src.invec_$ };
     
    189188    T ?`val( vector_exit(T) & src ) {
    190189        return *src.item_$;
     190    }
     191
     192    void ?=?( vector_exit(T) & dst, T val ) {
     193        *dst.item_$ = val;
    191194    }
    192195
     
    243246    // vec internals
    244247
    245     void grow( vector( T ) & this ) {
     248    void grow_$( vector( T ) & this ) {
    246249        size_t newCapacity = 2 * (this.buffer_end_$ - this.buffer_first_$);
    247250        T * newItems = aalloc( newCapacity );
     
    263266    }
    264267
     268    bool inRange_$( T * query, T * from, T * to) {
     269        if (from == to) return false;
     270        if (from < to) return from <= query && query < to;
     271        return query < to || from <= query;
     272    }
     273
    265274    // vec api
    266275
     
    269278        if (col`length >= col`capacity) {
    270279            assert (col`length == col`capacity);
    271             grow(col);
     280            grow_$(col);
    272281        }
    273282        // call autogen constructor deleted at end of hfa
     
    280289    }
    281290
     291    vector_transit(T) ?[?]( vector( T ) & vec, ptrdiff_t idx ) {
     292        // call autogen constructor deleted at end of hfa
     293        vector_transit(T) ret = { & vec, idx };
     294        return ret;
     295    }
     296
    282297    vector_exit(T) ?`origin( vector( T ) & vec ) {
    283298
     
    294309    }
    295310
    296     bool inRange_$( T * query, T * from, T * to) {
    297         if (from == to) return false;
    298         if (from < to) return from <= query && query < to;
    299         return query < to || from <= query;
     311    size_t ?`capacity( vector(T) & v ) {
     312        return v.buffer_end_$ - v.buffer_first_$;
     313    }
     314
     315    size_t ?`length( vector(T) & v ) {
     316        if (v.elems_first_$ == 0p) return 0;
     317        if (v.elems_first_$ < v.elems_end_$ ) return v.elems_end_$ - v.elems_first_$;
     318        return v.buffer_end_$ - v.elems_first_$ + v.elems_end_$ - v.buffer_first_$;
    300319    }
    301320
     
    304323        if (col`length >= col`capacity) {
    305324            assert (col`length == col`capacity);
    306             grow(col);
     325            grow_$(col);
    307326        }
    308327       
     
    339358    }
    340359
    341     size_t ?`capacity( vector(T) & v ) {
    342         return v.buffer_end_$ - v.buffer_first_$;
    343     }
    344 
    345     size_t ?`length( vector(T) & v ) {
    346         if (v.elems_first_$ == 0p) return 0;
    347         if (v.elems_first_$ < v.elems_end_$ ) return v.elems_end_$ - v.elems_first_$;
    348         return v.buffer_end_$ - v.elems_first_$ + v.elems_end_$ - v.buffer_first_$;
    349     }
    350 
    351 
    352360} // forall T
    353361
  • tests/Makefile.am

    r301e9f7 ra8e2215  
    6262# Tests that need investigation from the CFA team about why they require lax wflags.  Goal is to eliminate this list.
    6363WFLAGS_OPT_LAX_TO_INVESTIGATE = \
    64         collections/vector-demo \
    6564        concurrency/actors/dynamic \
    6665        concurrency/actors/executor \
  • tests/collections/vector-demo.cfa

    r301e9f7 ra8e2215  
    138138    assert( v`capacity == 4 && v`length == 4 );
    139139
    140     push_last(v, -100); // v is [0.0, 98.6, 0.1, 0.2, 0.3]; iter at 0.1, but RTP it's not looking at the old memory's 0.1
     140    push_last(v, -100); // v is [0.0, 98.6, 0.1, 0.2, -100]; iter at 0.1, but RTP it's not looking at the old memory's 0.1
    141141
    142142    // that made it bigger
    143143    assert( v`capacity >  5 && v`length == 5 );
    144144
    145     v[2] = -0.1f;  // v is [0.0, 98.6, -0.1, 0.2, 0.3]; iter at -0.1, where only the new memory had that change
     145    v[2] = -0.1f;  // v is [0.0, 98.6, -0.1, 0.2, -100]; iter at -0.1, where only the new memory had that change
    146146
    147147    float val3 = iter`val;
     
    158158
    159159    while( vector_exit(float) it = v`origin; it`moveNext ) {
    160         toPrint *= it;
     160        toPrint *= it;  // wish for ?=?, but it's not working
    161161        printf("loop sees %f\n", toPrint);
    162162    }
     163
     164    // todo: loop a permit
    163165}
    164166
Note: See TracChangeset for help on using the changeset viewer.