GithubHelp home page GithubHelp logo

sharoniv0x86 / libc-stl Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 20 KB

A C++ STL implementation in C.

License: MIT License

C 100.00%
c cpp cpp17 dynamic-memory-allocation library stl-algorithms stl-containers

libc-stl's Introduction

LibC-STL: A C Implementation of Standard Template Library (STL)

Overview

C-STL is a project aimed at implementing a subset of the functionality provided by the C++ Standard Template Library (STL) in pure C. The motivation behind this project stems from the efficiency and versatility of the C++ STL, which has been widely acclaimed as one of the most efficient code libraries in C++. This projects aims at implementing C++ STL in C as efficient and as easy to use as possible.

Motivation

The C++ STL is renowned for its rich collection of data structures, algorithms, and utilities, which simplify and streamline the process of software development in C++. Recognizing the value and efficiency of the STL, this project seeks to bridge this gap by bringing some of the essential features of the STL to the C programming language.

Project Goals

  • Implement core data structures such as vectors, linked lists, stacks, queues, maps, and sets in C.
  • Develop efficient algorithms for common operations like sorting, searching, and manipulation of data structures.
  • Provide a user-friendly interface and intuitive API for easy integration into C projects.
  • Ensure compatibility and portability across different platforms and compilers.
  • Foster a community of contributors and users to collaborate on further enhancements and improvements.

Key Features

  • Vector: Resizable arrays similar to vectors in C++. Example
  • Iterators: C++ style iterators to iterate containers efficiently.
  • Linked List: Singly linked lists, doubly linked lists, and circular linked lists. [to be implemened]
  • Stacks and Queues: Implementations using arrays or linked lists. [to be implemened]
  • Maps and Sets: Hash maps or binary search trees for key-value pairs and unique elements. [to be implemened]
  • String Manipulation: Functions for manipulating strings, including concatenation, substring extraction, and searching. [to be implemened]
  • Algorithms: Common algorithms for sorting, searching, and manipulation of sequences. [to be implemened]
  • Error Handling: Mechanisms for error handling and exception management. [to be implemened]

Contributing

Contributors are welcomed add more functionalities or fixes..

  • Raise an issue about the about feature you want to add and wait for it to assigned to you.
  • Or check any existing issue if you want to work on them Issues
  • Edit your code and open the PR, documentation will be taken care of. Feel free to share if you have anything extra to add in docs.
  • Make sure to add block comments above your added feature.

License

This project is licensed under the MIT License. Feel free to use, modify, and distribute the code according to the terms of the license.

Note from maintainer

This project serves as a learning endeavor for me to explore dynamic memory management in C, gaining insights into the STL in C++, and honing skills in library design and DSA. I'm eager to see this project flourish and evolve further.

Example

#include "./include/Vector.h"

int main() {
    // Creating vectors for different data types
    Vector *int_vector = create_vector(5, INT);
    Vector *float_vector = create_vector(5, FLOAT);
    Vector *char_vector = create_vector(5, CHAR);
    Vector *string_vector = create_vector(5, STR);
    Vector *double_vector = create_vector(5, DOUBLE);

    // Pushing data into the vectors
    pb_int(int_vector, 10);
    pb_int(int_vector, 20);
    pb_int(int_vector, 30);

    pb_float(float_vector, 3.14f);
    pb_float(float_vector, 2.718f);
    pb_float(float_vector, 1.618f);

    pb_char(char_vector, 'a');
    pb_char(char_vector, 'b');
    pb_char(char_vector, 'c');

    pb_string(string_vector, "The cat meows");
    pb_string(string_vector, "The dog barks");
    pb_string(string_vector, "The cow moos");

    //Inputting string with fgets
    printf("Enter the string: ");
    char strr[30] = " ";
    pb_string(string_vector, fgets(strr, sizeof(strr), stdin));

    pb_double(double_vector, 3.14159);
    pb_double(double_vector, 2.71828);
    pb_double(double_vector, 1.41421);

    // Accessing elements in the vectors
    printf("Int Vector:\n");
    for (size_t i = 0; i < int_vector->size; ++i) {
        int *value = (int *)at(int_vector, i);
        printf("%d\n", *value);
    }

    printf("Float Vector:\n");
    for (size_t i = 0; i < float_vector->size; ++i) {
        float *value = (float *)at(float_vector, i);
        printf("%f\n", *value);
    }

    printf("Char Vector:\n");
    for (size_t i = 0; i < char_vector->size; ++i) {
        char *value = (char *)at(char_vector, i);
        printf("%c\n", *value);
    }

    printf("String Vector: ");
    for (size_t i = 0; i < string_vector->size; ++i) {
        puts((char *)(at(string_vector, i)));
    }

    // Deleting vectors to free memory
    delete_vector(int_vector);
    delete_vector(float_vector);
    delete_vector(char_vector);
    delete_vector(string_vector);

    return 0;
}

Feedback and Support

If you have any feedback, suggestions, or questions regarding the project, please feel free to open an issue.

Acknowledgements

We would like to express our gratitude to the creators and maintainers of the C++ STL, whose work has inspired this project. Additionally, we thank all contributors and users for their valuable feedback and contributions.

libc-stl's People

Contributors

sharoniv0x86 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

libc-stl's Issues

Implementing error handling.

This is kinda important for now, the error handling or using assertions for compile time checking. I havent found any use case for using assertions for compile time checking, can probably use it in create_vector function.

    Vector *int_vector = create_vector(5, INT);
    Vector *float_vector = create_vector(5, FLOAT);
    Vector *char_vector = create_vector(5, CHAR);
    Vector *string_vector = create_vector(5, STR);

Maybe this can require compile time checking, although if you pass Vector *char_vector = create_vector(5, INT); this will produce error and will exit.

In the functions like

push_back_int(...)
push_back_float(...)
push_back_double(...)

These along with some other functions, they return 1 on success and 0 on failure, the idea is to have some predefined macros with error codes and user can choose whether they want to handle error or not by comparing the error codes against the functions returned values.

Adding documentation.

Currently the Vector container defined in include/Vector.h is mission detailed documentation, i will be working on adding documentation myself maybe directly with readme files or using gituhb wiki, but currently we only have the usage example of vector.

Another idea is to add comment block over each function in the Vector.h but that will significantly increase the LoCs so this is kept as a future idea when the functions/methods definitions and declarations are moved into separate files .c and .h files, or a static library used is added.

Implementing Iterators for iterating containers.

Implementing Basic iterators for iterating containers and adding more iterator methods

My basic idea is to create a iterator struct inside the vector struct so that each vector will have a iterator associated with it.
The iterator struct will have defined methods like the C++ iterators have See so that we can user (.) operator to call the iterator associated with it.

And making each iterator struct's object/variable as a stand alone datatype which can be used to iterate vector for now only, but the issue with this is whether to choose if the iterator will have a type, like in C++ there are iterator of all datatype so how to implement something similar in this scenario.

Feel free to share any ideas.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.