Other articles

  1. Multipattern Search

    This is a simple multipattern search algorithm. It isn't particularly speedy (grep -Ff words target which uses Aho-Corasick will outperform this algorithm) but it is quick to implement . It reads a list of words, sorts them into sublists grouped by length, and converts each sublist into a trie. Then, for …

    read more
  2. Chunk Data For Streaming

    NOTE: I originally posted this on Snipplr.

    Chunks data into block_size blocks for streaming, adds null padding.

    import math
    is_debug = False
    def chunk(data, block_size, padding=b'\x00'):
        data_size       = len(data)
        padding_size    = block_size - (data_size % block_size) if (data_size > block_size) else (block_size - data_size)
        is_padded       = (padding_size > 0) and (padding_size != block_size)
        total_blocks    = math.ceil …
    read more
  3. k-d Tree

    NOTE: I originally posted this on Snipplr.

    make-kd-node [median left right]: Creates a node in a kd-tree.

    make-kd-tree [k depth points]: Creates a kd-tree of kd-nodes. TODO: Not stack safe. Use loop/recur.

    kd-nearest-neighbor [a-point kd-tree]: Returns the nearest neighboring point to a given point, using a kd-tree.

    (ns kd-tree …
    read more
  4. Axis-Aligned Bounding Box

    NOTE: I originally posted this on Snipplr.

    make-aabb [a-point b-point] [a-point b-point]: Creates a 3D Axis-Aligned Bounding Box (AABB).

    aabb-contains? [a-box a-point]: Returns true if a 3D AABB contains a given point.

    (ns aabb)
    (defn ^{
        :doc "Creates a 3D Axis-Aligned Bounding Box (AABB)."
    } make-aabb [a-point b-point] [a-point b-point])
    (defn ^{
        :doc …
    read more