Giter VIP home page Giter VIP logo

clang_averaged_buffer's Introduction

Averaged Buffer

  • Averaged Buffer is an wrapper to handle an array and serve an averaged value of the array by some algorithm (e.g. simple averaging, weighted averaging).
  • Able to use via an OOP-like interface in C language.

Features

  • Store int32_t values into the buffer and serve its averaged value.
  • Able to handle via the same interface for different averaging algorithm. So you only need modification for configuration of the buffer, need NO modification for your application.

Requirements

  • C99 or above
  • Memory allocation methods (e.g. malloc / free via stdlib.h. Your own implemented memory allocator is also available.)

Author / License

Installation

  • Clone this repository and place source files in your source code directory.

Usage

  • Averaged Buffer has an OOP-like interface: it must implement IAveragedBuffer interface.
  • The APIs of IAveragedBuffer interface must pass the pointer of itself as the 1st argument like below.
/* Create instance */
IAveragedBuffer* pThis = AveragedBufferCreate(...);
/* Add new value: pass `IAveragedBuffer*` to 1st argument */
AveragedBufferAdd(pThis, 100);
/* Acquire the average: pass `IAveragedBuffer*` to 1st argument */
AveragedBufferType value = AveragedBufferAverage(pThis);

Create the instance

  • The instance of the buffer must be created via AveragedBufferCreate() API with the configuration.

  • As shown in following example,

    • The configuration is vary for different instance.
    • But the API to create the instance is same.
/**
 * @brief   Configuration of the Averaged Buffer
 * @attention   The entity of configuration must be placed in the static area.
 *              The buffer might cause an unexpected behavior if configuration
 *              is placed in the stack area.
 */
static const AveragedBufferConfig configForSimple = {
    AVERAGED_BUFFER_ALGO_SIMPLE,    /* Averaging algorithm */
    128,    /* Length of the buffer */
    NULL    /* Parameter for averaging algorithm */
};

/**
 * @brief   Entity of simple averaging buffer
 */
static IAveragedBuffer* pSimple = AveragedBufferCreate(&configForSimple);

/**
 * @brief   Entity of weighted averaging buffer
 */
static IAveragedBuffer* pWeighted;

void func_initialize(void)
{
    /** Configuration for weighted averaging buffer
     * @attention   The buffer might cause an unexpected behavior if
     *              you remove `static` at next line.
     */
    static const AveragedBufferConfig configForWeighted = {
        AVERAGED_BUFFER_ALGO_WEIGHTED,
        10,
        &weightedConfig
    };
    /* Create the instance of weighted averaging buffer */
    pWeighted = AveragedBufferCreate(&configForWeighted);
}

Configuration

  • Configuration is provided via AveragedBufferConfig structure.
  • It has following parameters.
Parameter name Type Description
Algorithm enum AveragedBufferAlgorithm Averaging algorithm
Length size_t Length of the buffer
Parameter void* Averaging parameters for some algorithm

Averaging parameter

For simple averaging buffer

  • There is no averaging parameters.
  • Hence Parameter must be specified as NULL.

For weighted averaging buffer

  • Not implemented yet for current version.

Adding the value

  • The value that has AveragedBufferType type will be added via AveragedBufferAdd() API.
void func_add(AveragedBufferType Value)
{
    /* Add value to each Averaged Buffers */
    AveragedBufferAdd(pSimple, Value);
    AveragedBufferAdd(pWeighted, Value);
}

Acquire the average

  • Able to acquire the average via AverageBufferAverage() API.
  • 2nd argument is an option: pass NOT NULL pointer if you need the error code of this API.
void func_average(void)
{
    /** Acquire the average of simple averaging buffer.
     * The error code of this API will be disposed because
     * 2nd argument is NULL.
     */
    AveragedBufferType value1 = AveragedBufferAverage(pSimple, NULL);
    /** Acquire the average of weighted averaging buffer.
     * The error code of this API will be stored into `ErrorCode`.
     */
    AveragedBufferError ErrorCode;
    AveragedBufferType value2 = AveragedBufferAverage(pWeighted, &ErrorCode);
}

TODO

  • Implement weighted averaging algorithm

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.