Giter VIP home page Giter VIP logo

single_allocation_buffer.zig's Introduction

single_allocation_buffer.zig

It's basically just a slice with some convenience methods around resizing and reallocating it.

Comes with a Managed and Unmanaged variant.

Use cases

I mostly use this inside of intern tables for things like big integers and strings where intermediate operations (arithmetic, concatenation, etc.) need temporary buffers

Example

// Some silly big integer arithmetic
pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    var sab = single_allocation_buffer.Managed.init(gpa.allocator());
    defer sab.deinit();

    var limbs: [32]big_math.Limb = undefined;
    var a = BigIntMut.init(limbs[0..16], 12345_67890_12345_67890);
    const b = BigIntMut.init(limbs[17..], 67890_12345_67890_12345).toConst();

    {
        a.mul(
            a.toConst(),
            b,
            try sab.alloc(big_math.Limb, big_math.int.calcMulLimbsBufferLen(a.len, b.limbs.len, 2)),
            null,
        );

        // uses a safety lock to track when allocations are live,
        // use unlockBuffer to say the buffer is unused
        sab.unlockBuffer();
    }

    // or use the *Temporary functions to allocate without taking the lock

    a.sqrt(
        a.toConst(),
        try sab.allocTemporary(big_math.Limb, big_math.int.calcSqrtLimbsBufferLen(a.toConst().bitCountAbs())),
    );

    std.log.info("{}", .{a.toConst()});
}

const std = @import("std");
const big_math = std.math.big;
const BigInt = big_math.int.Const;
const BigIntMut = big_math.int.Mutable;
const single_allocation_buffer = @import("single_allocation_buffer.zig");

Licensing

Everything in this repository is released to the public domain under the Creative Commons Zero v1.0 Universal License

single_allocation_buffer.zig's People

Contributors

euclidianace 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.