Coder Social home page Coder Social logo

buckets's Introduction

buckets

buckets is a crate for sorting lots of things at once.

Installation

Add this to your Cargo.toml:

[dependencies]
mean = "0.1"

Usage

use buckets::SortWithBucket;
let mut input: Vec<u16> = vec![65444, 50, 12532, 121];
input.sort_with_buckets();

assert_eq!(input, vec![50, 121, 12532, 65444])

#![no_std]

Coming later.

Nightly

This is nightly because it's blocked on generic_const_exprs, because I need a constant used in the output of a trait that is also defined in that trait for arrays. Hopefully I can think of another way to do this.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Licenses

buckets's People

Contributors

frogtd avatar

Watchers

 avatar

buckets's Issues

nightly comment;

Hi @frogtd

Accidentally turn into your repository.

This is nightly because it's blocked on generic_const_exprs, because I need a constant used in the output of a trait that is also defined in that trait for arrays. Hopefully I can think of another way to do this.

I am guessing if the traits you're using is necessary?

Here's a patch work, there might be errors here not sure.

use core::mem;

pub trait SortWithBucket {
    fn sort_with_bucket(&mut self);
}

macro_rules! impl_bucket_sort {
    ($($t:ty),*) => {
        $(
            impl SortWithBucket for Vec<$t> {
                fn sort_with_bucket(&mut self) {
                    let mut buckets = [<$t as WeirdTrick>::VEC; 256];
                    for x in 0..mem::size_of::<$t>() {
                        for y in self.drain(..) {
                            let bucket = &y.to_ne_bytes()[x];
                            buckets[*bucket as usize].push(y);
                        }
                        for bucket in buckets.iter_mut() {
                            self.append(bucket);
                        }
                    }
                }
            }
        )*
    };
    () => {

    };
}

trait WeirdTrick: Sized {
    const VEC: Vec<Self>;
}

impl<T: Sized> WeirdTrick for T {
    const VEC: Vec<Self> = Vec::new();
}

impl_bucket_sort!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let mut vector = vec![1u16, 7, 3, 64444, 5, 4214, 5, 8, 9, 10000];
        let mut vector_clone = vector.clone();
        vector_clone.sort();
        vector.sort_with_bucket();

        assert_eq!(vector, vector_clone);
    }
}

PS: WeirdTrick is really weird.
PS2: Good use of macros ๐Ÿ‘

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.