Skip to content

A simple, lightweight iterator implementation for C to easily traverse through collections of elements.

License

DilemaFixer/Cterator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Cterator

Build Status Version License Language

A simple, lightweight iterator implementation for C to easily traverse through collections of elements.

🚀 Installation

Cross-Platform (Linux, macOS, Windows)

curl -o cterator.c https://raw.githubusercontent.com/DilemaFixer/Cterator/main/cterator.c
curl -o cterator.h https://raw.githubusercontent.com/DilemaFixer/Cterator/main/cterator.h

📋 API and Usage Examples

Usage Example

#include "cterator.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    // Create an array of string pointers
    char *strings[] = {"Hello", "World", "Cterator", "Example"};
    
    // Create an iterator for these strings
    cterator *ct = new_cterator((void **)strings, 4);
    
    // Method 1: Manual iteration
    ct_reset(ct);
    while (ct_next(ct)) {
        char *str = ct_get(ct);
        printf("%s\n", str);
    }
    
    // Method 2: Using the foreach macro
    ct_reset(ct);
    char *item;
    foreach(item, ct) {
        printf("%s\n", item);
    }
    
    // Clean up
    free_cterator(ct);
    return 0;
}

Core Structure

typedef struct cterator {
    void **items;    // Array of elements to iterate through
    size_t count;    // Total number of elements
    size_t current;  // Current position in the array
} cterator;

Creation and Destruction Functions

// Create a new iterator for an array of pointers
cterator *new_cterator(void **items, size_t count);

// Free the iterator (does not free the item array)
void free_cterator(cterator *ct);

Iterator Operations

// Get the current element without advancing the iterator
void *ct_get(cterator *ct);

// Move to the next element and return whether there are more elements
int ct_next(cterator *ct);

// Reset the iterator to the beginning
void ct_reset(cterator *ct);

Convenience Macros

// Foreach macro for easy iteration
#define foreach(item, iterator) \
    for(int _keep = (ct_next(iterator) ? 1 : 0); \
        _keep && ((item = ct_get(iterator)) || 1); \
        _keep = ct_next(iterator) ? 1 : 0)

Required header: cterator.h

About

A simple, lightweight iterator implementation for C to easily traverse through collections of elements.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages