Skip to content
BitVecRunLengths

BitVecRunLengths

BitVecRunLengths

Description

Analyze run lengths in a bitvector. A run is a sequence of consecutive identical bits. Results array must be pre-allocated with sufficient space.

Parameters

Name Direction Description
bv in Bitvector to analyze
runs out Array to store run lengths
values out Array to store run values (true/false)
max_runs in Maximum number of runs to store

Usage example (from documentation)

  u64 run_lengths[50];
  bool run_values[50];
  u64 count = BitVecRunLengths(&flags, run_lengths, run_values, 50);

Returns

Number of runs found

Usage example (Cross-references)

Usage examples (Cross-references)
    // Foreach functions
    
    u64 BitVecRunLengths(BitVec *bv, u64 *runs, bool *values, u64 max_runs) {
        ValidateBitVec(bv);
        if (!runs || !values || max_runs == 0) {
    
    bool test_bitvec_run_lengths_basic(void) {
        WriteFmt("Testing BitVecRunLengths basic functionality\n");
    
        BitVec bv     = BitVecInit();
        u64  runs[10];
        bool values[10];
        u64  count = BitVecRunLengths(&bv, runs, values, 10);
    
        // Should find 5 runs
    
    bool test_bitvec_run_lengths_edge_cases(void) {
        WriteFmt("Testing BitVecRunLengths edge cases\n");
    
        bool result = true;
        u64    runs[5];
        bool   values[5];
        u64    count = BitVecRunLengths(&empty_bv, runs, values, 5);
        result       = result && (count == 0);
        BitVecDeinit(&empty_bv);
        BitVec single_bv = BitVecInit();
        BitVecPush(&single_bv, true);
        count  = BitVecRunLengths(&single_bv, runs, values, 5);
        result = result && (count == 1);
        result = result && (runs[0] == 1 && values[0] == true);
        BitVec single_false_bv = BitVecInit();
        BitVecPush(&single_false_bv, false);
        count  = BitVecRunLengths(&single_false_bv, runs, values, 5);
        result = result && (count == 1);
        result = result && (runs[0] == 1 && values[0] == false);
            BitVecPush(&all_true_bv, true);
        }
        count  = BitVecRunLengths(&all_true_bv, runs, values, 5);
        result = result && (count == 1);
        result = result && (runs[0] == 10 && values[0] == true);
        u64  alt_runs[10];
        bool alt_values[10];
        count  = BitVecRunLengths(&alternating_bv, alt_runs, alt_values, 10);
        result = result && (count == 7); // Each bit is its own run
        // Verify alternating pattern
    
    bool test_bitvec_run_lengths_boundary_conditions(void) {
        WriteFmt("Testing BitVecRunLengths boundary conditions\n");
    
        BitVec bv     = BitVecInit();
        u64  runs[3];
        bool values[3];
        u64  count = BitVecRunLengths(&bv, runs, values, 3);
    
        // Should only capture first 3 runs due to limit
    
        // Test with max_runs = 1
        count  = BitVecRunLengths(&bv, runs, values, 1);
        result = result && (count == 1);
        result = result && (runs[0] == 1 && values[0] == true);
        u64  large_runs[8];
        bool large_values[8];
        count  = BitVecRunLengths(&bv, large_runs, large_values, 8);
        result = result && (count == 8);
    
    bool test_bitvec_run_lengths_stress_test(void) {
        WriteFmt("Testing BitVecRunLengths stress test\n");
    
        bool result = true;
        u64  runs[200];
        bool values[200];
        u64  count = BitVecRunLengths(&large_bv, runs, values, 200);
    
        // Should have 100 runs (100 blocks)
        u64  small_runs[10];
        bool small_values[10];
        count = BitVecRunLengths(&large_bv, small_runs, small_values, 10);
    
        // Expected runs: [4T, 4F, 3T, 3F, 1T, 4F, 3T] = 7 runs
    
    bool test_bitvec_run_lengths_null_bv(void) {
        WriteFmt("Testing BitVecRunLengths with NULL bitvector\n");
    
        u64  runs[5];
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(NULL, runs, values, 5);
    
        return true; // Should never reach here
    
    bool test_bitvec_run_lengths_null_runs(void) {
        WriteFmt("Testing BitVecRunLengths with NULL runs array\n");
    
        BitVec bv = BitVecInit();
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(&bv, NULL, values, 5);
    
        BitVecDeinit(&bv);
    
    bool test_bitvec_run_lengths_null_values(void) {
        WriteFmt("Testing BitVecRunLengths with NULL values array\n");
    
        BitVec bv = BitVecInit();
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(&bv, runs, NULL, 5);
    
        BitVecDeinit(&bv);
    
    bool test_bitvec_run_lengths_zero_max_runs(void) {
        WriteFmt("Testing BitVecRunLengths with zero max_runs\n");
    
        BitVec bv = BitVecInit();
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(&bv, runs, values, 0);
    
        BitVecDeinit(&bv);
    
    bool test_bitvec_run_lengths_basic(void) {
        WriteFmt("Testing BitVecRunLengths basic functionality\n");
    
        BitVec bv     = BitVecInit();
        u64  runs[10];
        bool values[10];
        u64  count = BitVecRunLengths(&bv, runs, values, 10);
    
        // Should find 5 runs
    
    bool test_bitvec_run_lengths_edge_cases(void) {
        WriteFmt("Testing BitVecRunLengths edge cases\n");
    
        bool result = true;
        u64    runs[5];
        bool   values[5];
        u64    count = BitVecRunLengths(&empty_bv, runs, values, 5);
        result       = result && (count == 0);
        BitVecDeinit(&empty_bv);
        BitVec single_bv = BitVecInit();
        BitVecPush(&single_bv, true);
        count  = BitVecRunLengths(&single_bv, runs, values, 5);
        result = result && (count == 1);
        result = result && (runs[0] == 1 && values[0] == true);
        BitVec single_false_bv = BitVecInit();
        BitVecPush(&single_false_bv, false);
        count  = BitVecRunLengths(&single_false_bv, runs, values, 5);
        result = result && (count == 1);
        result = result && (runs[0] == 1 && values[0] == false);
            BitVecPush(&all_true_bv, true);
        }
        count  = BitVecRunLengths(&all_true_bv, runs, values, 5);
        result = result && (count == 1);
        result = result && (runs[0] == 10 && values[0] == true);
        u64  alt_runs[10];
        bool alt_values[10];
        count  = BitVecRunLengths(&alternating_bv, alt_runs, alt_values, 10);
        result = result && (count == 7); // Each bit is its own run
        // Verify alternating pattern
    
    bool test_bitvec_run_lengths_boundary_conditions(void) {
        WriteFmt("Testing BitVecRunLengths boundary conditions\n");
    
        BitVec bv     = BitVecInit();
        u64  runs[3];
        bool values[3];
        u64  count = BitVecRunLengths(&bv, runs, values, 3);
    
        // Should only capture first 3 runs due to limit
    
        // Test with max_runs = 1
        count  = BitVecRunLengths(&bv, runs, values, 1);
        result = result && (count == 1);
        result = result && (runs[0] == 1 && values[0] == true);
        u64  large_runs[8];
        bool large_values[8];
        count  = BitVecRunLengths(&bv, large_runs, large_values, 8);
        result = result && (count == 8);
    
    bool test_bitvec_run_lengths_stress_test(void) {
        WriteFmt("Testing BitVecRunLengths stress test\n");
    
        bool result = true;
        u64  runs[200];
        bool values[200];
        u64  count = BitVecRunLengths(&large_bv, runs, values, 200);
    
        // Should have 100 runs (100 blocks)
        u64  small_runs[10];
        bool small_values[10];
        count = BitVecRunLengths(&large_bv, small_runs, small_values, 10);
    
        // Expected runs: [4T, 4F, 3T, 3F, 1T, 4F, 3T] = 7 runs
    
    bool test_bitvec_run_lengths_null_bv(void) {
        WriteFmt("Testing BitVecRunLengths with NULL bitvector\n");
    
        u64  runs[5];
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(NULL, runs, values, 5);
    
        return true; // Should never reach here
    
    bool test_bitvec_run_lengths_null_runs(void) {
        WriteFmt("Testing BitVecRunLengths with NULL runs array\n");
    
        BitVec bv = BitVecInit();
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(&bv, NULL, values, 5);
    
        BitVecDeinit(&bv);
    
    bool test_bitvec_run_lengths_null_values(void) {
        WriteFmt("Testing BitVecRunLengths with NULL values array\n");
    
        BitVec bv = BitVecInit();
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(&bv, runs, NULL, 5);
    
        BitVecDeinit(&bv);
    
    bool test_bitvec_run_lengths_zero_max_runs(void) {
        WriteFmt("Testing BitVecRunLengths with zero max_runs\n");
    
        BitVec bv = BitVecInit();
    
        // This should cause LOG_FATAL and terminate the program
        BitVecRunLengths(&bv, runs, values, 0);
    
        BitVecDeinit(&bv);
Last updated on