• Stars
    star
    14
  • Rank 1,392,131 (Top 29 %)
  • Language
    Crystal
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is a Crystal implementation of a Splay Tree; which is a type of binary search tree that is semi-balanced and that tends to self-optimize so that the most accessed items are the fastest to retrieve.

SplayTreeMap CI SplayTreeMap Build Docs

GitHub release GitHub commits since latest release (by SemVer)

A splay tree is a type of binary search tree that self organizes so that the most frequently accessed items tend to be towards theroot of the tree, where they can be accessed more quickly.

This implementation provides a hash-like interface, and it provides a couple features not typically found in Splay Trees -- efficient removal of the items that are generally least frequently accessed, and an extra fast search option.

Leaf Pruning

Because splay trees tend to organize themselves with the most frequently accessed elements towards the root of the tree, the least frequently accessed items tend to migrate towards the leaves of the tree. This implementation offers a method that can be used to prune its leaves, which generally has the effect of removing the least frequently accessed items from the tree.

This is useful if the data structure is being used to implement a cache, as it can be used to control the size of the cache while generaly keeping the most useful items in the cache without any other extensive bookkeeping.

Search without Splaying

A splay operation is generally performed on any access to a splay tree. This is the operation that moves the most important items towards the root. This operation has a cost to it, however, and there are times when it is desireable to search the hash without a splay operation occuring for the key that is searched. This results in a faster search operation, at the cost of not performing any efficiency improving structural changes to the tree. This should not be the primary search method that is used, but it can be useful at the right time.

Maximum Size

If #maxsize is set to an integer alue, then the splay tree will perform a prune operation when the maximum size of the tree is reached. This is useful for implementing a cache.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      splay_tree_map:
        github: wyhaines/splay_tree_map.cr
  2. Run shards install

Usage

Full documentation can be found at: https://wyhaines.github.io/splay_tree_map.cr/index.html

require "splay_tree_map"

Generally, the data structure is used like a hash.

stm = SplayTreeMap(String, String).new
stm.maxsize = 10

stm["this"] = "that"
stm["something"] = "else"
stm["junk"] = "pile"

if stm.has_key?("this")
  puts stm["this"]
end

stm.delete("junk")

puts stm.obtain("something") # This finds, but doesn't splay.

stm.prune # remove all leaves

Testing

To run the specs run crystal spec. To run specs with more debugging output use LOG_LEVEL=DEBUG crystal spec.

TODO

Experiment with other variations of splay operations, such as lazy semi-splay to see if performance can be improved. Right now this isn't any better than just using a Hash and arbitrarily deleting half of the hash if it grows too big.

Credits

This implementation is derived from the incomplete and broken implementation in the Crystalline shard found at https://github.com/jtomschroeder/crystalline

Contributing

  1. Fork it (https://github.com/wyhaines/splay_tree_map/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

GitHub code size in bytes GitHub issues

More Repositories

1

swiftiply

A high performance clustering proxy / web server for web applications.
Ruby
68
star
2

scrawls

A Simple Ruby Web Server
Ruby
37
star
3

analogger

A very fast distributed asynchronous logging service
Ruby
25
star
4

iowa

A fast, productive, component based web development framework for Ruby.
Ruby
21
star
5

defined.cr

This shard provides facilities for checking whether a constant exists at compile time, and for a variety of different conditional compilation options. Code can be conditionally compiled based on the existence of a constant, version number constraints, or whether an environment variable is set truthy or not.
Crystal
17
star
6

opentelemetry-instrumentation.cr

Bundled integrations for opentelemetry-api.cr (https://github.com/wyhaines/opentelemetry-api.cr).
Crystal
16
star
7

csuuid.cr

This is a small UUID library that implements a chronologically sortable UUID.
Crystal
15
star
8

datapack.cr

You are building an application, be it web or something else. It has data -- images, CSS, JS, CSV files, whatever -- and you want to be able to easily bundle those into your compiled executable. This shard gives you a simple utility to add this data to your executable, access it, and manage it.
Crystal
13
star
9

opentelemetry-api.cr

The core of open telemetry instrumentation is the OpenTelemetry API/SDK. The initial aim of this shard is to implement the OpenTelemetry specification for metrics, traces, and logs.
Crystal
12
star
10

Send.cr

This shard implements a usable dynamic dispatch function for Crystal code, modeled on the Ruby #send function of the same name.
Crystal
12
star
11

tracer.cr

This library implements a method call tracing facility that can be used to generate either summary or detailed logs of method calls, or to inject arbitrary code execution into method calls.
Crystal
10
star
12

rubyconfmini_2022_crystal_for_rubyists_workshop

HTML
8
star
13

base58.cr

This is a performance optimized implementation of base58 binary-to-text encoding algorithm.
Crystal
8
star
14

opentelemetry-sdk.cr

This repository contains an implementation of an OpenTelemetry SDK for Crystal
Crystal
7
star
15

git-index.cr

This tool makes a local database of git repositories, indexed by initial commit hashes.
Crystal
5
star
16

bus.cr

It is sometimes useful to have a pubsub type message bus inside your software. This library implements a bus to send messages to interested subscribers. Those subscribers can reply to those messages, and are guaranteed that the reply will be routed back to the sender.
Crystal
5
star
17

crypt-isaac

A Ruby implementation of the cryptographically secure ISAAC pseudorandom number generator, with both pure Ruby and C extension implementations.
Ruby
5
star
18

nbchannel.cr

A NBChannel is a non-blocking channel. Normal Crystal channels block on send and on receive. The NBChannel will never block on send, and there are both blocking and nonblocking receive calls available. The channel should work both for single producer, single consumer scenarios, and for scenarios with multiple producers or multiple consumers.
Crystal
5
star
19

ParseDate.cr

This is a small library that attempts to parse a wide variety of date formats with a single call.
Crystal
4
star
20

Chord

A Ruby implementation of the Chord protocol.
Ruby
4
star
21

ruby-gnome2

A mirror of the ruby-gnome2 project.
4
star
22

timeout.cr

This implements a very simple timeout method for Crystal. Use it to wrap code in a timeout, so that execution of that code can not continue forever.
Crystal
4
star
23

find.cr

Crystal
3
star
24

Floodgate

A fast, easy to use, customizeable caching reverse proxy implemented substantially in Ruby
3
star
25

for.cr

This tiny little shard implements a "for" loop for Crystal by leveraging macros.
Crystal
3
star
26

wisteria

An extremely fast event based microframework/webserver written with Ruby.
Ruby
3
star
27

apm.cr

This is an open source APM agent for Crystal. Initial development is targeting the OpenTelemetry spec for data exchange, but it may also support the New Relic specific protocol. It should, however, be usable with any provider that permits OpenTelementry ingest.
Crystal
3
star
28

shellac

Shellac is a very simple proof-of-concept caching reverse proxy. It is intended mostly to demonstrate a concept, though it could be refined into something more polished.
Ruby
3
star
29

hpack.cr

This shard is a standalone implementation of HPack, the HTTP2 header compression algorithm.
Crystal
3
star
30

advent-of-code-2022

Here are my Advent of Code solutions for 2022.
Rust
3
star
31

alias_method.cr

Crystal does not provide a ready-to-use mechanism for creating method aliases, and the general Crystal code style recommendation is that one should avoid having multiple names that invoke the same method. However, there are times where creating method aliases is useful. This shard creates an alias_method macro that can be used to easily create method aliases which are functionally identical to the original method.
Crystal
3
star
32

eaba2-tools

A small collection of tools for dice rolling, probability simulations, and similar things for use with my EABA2 games.
2
star
33

lockfile.cr

This shard implements a portable lockfile, for use with the Crystal Language.
Crystal
2
star
34

evita

A spike to play with some ideas surrounding a chat bot implementation.
Crystal
2
star
35

NewRelic.cr

This is a binding to the New Relic C SDK, so that Crystal programs can add observability features with New Relic.
Crystal
2
star
36

hash_serializable.cr

The module provides a Hash::Serializeable module which, like JSON::Serializeable and YAML::Serializeable, can be mixed into a class to allow the class's ivars to be serialized out to a hash, and to allow a hash to be instantiated as an instance of a class.
Crystal
2
star
37

learning-crystal-via-conways-game-of-life

This repository is to be used as a lesson or guided workshop to introduce Crystal to someone who already knows how to program, by building a Crystal implementation of Conway's game of life. It includes a codespace to facilitate this lesson.
Shell
2
star
38

time-ext.cr

This is a very small extension of the Time class for Crystal. It has been extracted from another project as it provides a small number of logical and useful extensions to Time.
Crystal
2
star
39

simple_irc.cr

This is a simple IRC protocol implementation. No bundled bot capabilities or anything fancy. Just the basics, made easy to use.
Crystal
2
star
40

git-index

This is a very simple little tool that creates an index of git repositories, keyed by the commit hash codes for the first and second commits in the repository.
Ruby
2
star
41

liquidcrystal

This is a port of the Liquid gem from Shopify, for the Crystal language. It aims to be as compatible as possible with the upstream source of truth that is the Shopify Ruby implementation of Liquid.
Crystal
2
star
42

jekyll-include-with-frontmatter

Implements a couple tags to include files that contain front matter. Inspired by a desire to be able to edit Includes with Netflify-CMS.
Ruby
2
star
43

content

My content workspace
1
star
44

SwiftRPC

An RPC library implementation.
Ruby
1
star
45

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
1
star
46

crystal-conf-1.0-dynamic-crystal

Find ye herein all of the code examples that are used in my talk.
1
star
47

analogger.cr

This is a port of https://github.com/wyhaines/analogger using Crystal.
Crystal
1
star
48

crypt-xorshift

Implementation of a few variants of the Xorshift family of pseudo random number generators for Ruby, with both pure Ruby and C extension implementations.
Ruby
1
star
49

swiftcore-tasks

This little helper code for managing a queue of tasks to execute is used by multiple software packages, so I am packaging it for reuse.
Ruby
1
star
50

TwitchEventSub.cr

This shard encapsulates logic and handling for interacting with the Twitch EventSub API.
Crystal
1
star
51

simplereactor

A very, very barebones event reactor with timer support, implemented with Ruby, backed by either a Ruby/select() based IO system or a Nio4r based IO system.
Ruby
1
star
52

scrawls-ioengine-multithread

A multithreaded io engine for Scrawls.
Ruby
1
star
53

serf-handler

A simple little library that provides a basic framework for Ruby powered Serf handlers.
Ruby
1
star
54

apm-therelicans

This code is the code that was used for an article on how to build an APM system, written for therelicans.com.
Crystal
1
star
55

ridgepole-executor

This is a script intended to be ran by ridgepole as an external script for performing database changes. It will vector blocking alters through a tool to perform them without full table locks, like pt-osc, while sending the safe SQL to the database more directly.
Ruby
1
star
56

serf-handler.cr

This implements a port of the Ruby Serf Handler gem -- https://github.com/wyhaines/serf-handler -- as a library that can be used to easily build compiled, distributable handler binaries using the Crystal language.
Crystal
1
star