Giter VIP home page Giter VIP logo

scanlibrary's Introduction

Prefix Sum Scan Library

Overview

This C++ library provides template functions for computing prefix sums (also known as scans) on vectors. It supports both inclusive and exclusive scans, and is implemented using C++ templates to work with various data types. Features

Inclusive Scan: Calculates the running total of elements, including the current element.
Exclusive Scan: Calculates the running total of elements, excluding the current element.
Two modes of operation: with or without an output vector.
Templated implementation to support different data types.

Requirements

C++ Compiler with C++11 support or higher.
STL (Standard Template Library).

Usage

  • Including the Library
    #include "scan.hpp"

Function Templates

  • exclusivePrefixSum Computes the exclusive prefix sum of an input vector. Syntax:

    void exclusivePrefixSum(std::vector<T> &arr, std::vector<int> &prefixSum)
  • inclusivePrefixSum Computes the inclusive prefix sum of an input vector. Syntax:

    void inclusivePrefixSum(std::vector<T> &arr, std::vector<int> &prefixSum)
  • scan Overloaded function for both inclusive and exclusive scans. Syntax:

    //With output vector: 
    void scan<ScanType scanType, typename T>(const std::vector<T>& input, std::vector<T>& output)
    
    //Without output vector: 
    void scan<ScanType scanType, typename T>(std::vector<T>& input)

Example Usage

#include <iostream>
#include <vector>

#include "scan.hpp"

int main() {
    std::vector<int> data = {1, 2, 3, 4, 5};
    std::vector<int> result(data.size());

    // Test inclusive scan
    scan<ScanType::Inclusive>(data, result);
    std::cout << "Inclusive Scan: ";
    for (const auto& num : result) 
        std::cout << num << " ";
    std::cout << std::endl;

    // Test exclusive scan
    scan<ScanType::Exclusive>(data, result);
    std::cout << "Exclusive Scan: ";
    for (const auto& num : result) 
        std::cout << num << " ";
    std::cout << std::endl;

    return 0;
}
  • To compile
    g++ -std=c++17 -O3 main.cpp -o main

Contributing

Contributions to improve this library are welcome. Please ensure that your code adheres to the existing style and includes tests for new features.

License

Unlicensed.

scanlibrary's People

Contributors

abhijeetkumar96 avatar

Watchers

 avatar

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.