Code Monkey home page Code Monkey logo

segmap's Introduction

segmap exposes SegmentMap, a map data structure whose keys are stored as ranges. Contiguous and overlapping ranges that map to the same value are coalesced into a single range.

A correspoinding SegmentSet structure is also provided.

The Segment<T> Type

SegmentMap supports all types of input range types in the same map and coerces them all to a common range type for internal representation. A Segment<T> is always represented as increasing, so "backwards" ranges will be flipped for insertion.

Most methods on SegmentMap and SegmentSet accept a generic argument for the range, which only needs to implement RangeBounds.

Example: use with Chrono

use chrono::offset::TimeZone;
use chrono::{Duration, Utc};
use segmap::SegmentMap;

let people = ["Alice", "Bob", "Carol"];
let mut roster = SegmentMap::new();

// Set up initial roster.
let start_of_roster = Utc.ymd(2019, 1, 7);
let mut week_start = start_of_roster;
for _ in 0..3 {
    for person in people {
        let next_week = week_start + Duration::weeks(1);
        roster.insert(week_start..next_week, person);
        week_start = next_week;
    }
}

// Bob is covering Alice's second shift (the fourth shift overall).
let fourth_shift_start = start_of_roster + Duration::weeks(3);
let fourth_shift_end = fourth_shift_start + Duration::weeks(1);
roster.insert(fourth_shift_start..fourth_shift_end, "Bob");

// Print out the roster, and observe that
// the fourth and fifth shifts have been coalesced
// into one range.
for (range, &person) in roster.iter() {
    let start = *range.start_value().unwrap();
    let duration = *range.end_value().unwrap() - start;
    println!("{} ({}): {}", start, duration, person);
}

// Output:
// 2019-01-07UTC (P7D): Alice
// 2019-01-14UTC (P7D): Bob
// 2019-01-21UTC (P7D): Carol
// 2019-01-28UTC (P14D): Bob
// 2019-02-11UTC (P7D): Carol
// 2019-02-18UTC (P7D): Alice
// 2019-02-25UTC (P7D): Bob
// 2019-03-04UTC (P7D): Carol

Building without the Rust standard library

This crate can work without the full standard library available (e.g. when running on bare metal without an operating system) but relies on the presence of a global allocator โ€” i.e. it links the core and alloc crates, but not std.

Presently there is no functionality in the crate that require the standard library. Such functionality will likely be introduced in the future, and will be gated behind a default-on std feature.

See The Rust Programming Language book for general information about operating without the standard library.

segmap's People

Contributors

ecclarke42 avatar h33p avatar jeffparsons avatar nmathewson avatar

Stargazers

 avatar

segmap's Issues

Tests for Iterators and Set Operations

Generate tests for RangeMap iterators. At least:

  • RangeMap::iter_subset and RangeMap::subset
  • RangeMap::iter_complement and RangeMap::complement
  • RangeMap::iter_gaps and RangeMap::gaps

And RangeSet iterators and operations, at least:

  • RangeSet::iter_subset and RangeSet::subset
  • RangeSet::iter_complement and RangeSet::complement
  • RangeSet::iter_gaps and RangeSet::gaps
  • RangeSet::iter_difference and RangeSet::differenece
  • RangeSet::iter_symmetric_difference and RangeSet::symmetric_differenece
  • RangeSet::iter_intersection and RangeSet::intersection
  • RangeSet::iter_union and RangeSet::union
  • RangeSet::is_disjoint
  • RangeSet::is_subset

BitOr and BitAnd seem to give wrong answers

fn segmap_intersection_repro() {
    let set0 = segmap::SegmentSet::<i32>::from_iter([1..=3].into_iter());
    let set1 = segmap::SegmentSet::<i32>::from_iter([2..=4].into_iter());
    let union = &set0 | &set1;
    println!("{union:?}"); // got {[1, 3], [2, 4]}, expect ranges to be merged
    let intersection = set0 & set1;
    println!("{intersection:?}"); // got {}, expect {[2, 3]}
}

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.