<commit_before>package thx.fp;

import haxe.ds.Option;
import thx.Tuple;
using thx.Options;

abstract Map<K, V>(MapImpl<K, V>) from MapImpl<K, V> to MapImpl<K, V> {
  inline public static function empty<K, V>() : Map<K, V>
    return Tip;
  inline public static function singleton<K, V>(k : K, v : V) : Map<K, V>
    return Bin(1, k, v, Tip, Tip);
  inline public static function bin<K, V>(k : K, v : V, lhs : Map<K, V>, rhs : Map<K, V>) : Map<K, V>
    return Bin(lhs.size() + 1 + rhs.size() + 1, k, v, lhs, rhs);

  public function get(key : K, comparator : K -> K -> Int) : Option<V> {
    switch this {
      case Tip:
        return None;
      case Bin(size, xkey, xvalue, lhs, rhs):
        var c = comparator(key, xkey);
        if(c < 0)
          return lhs.get(key, comparator);
        else if(c > 0)
          return rhs.get(key, comparator);
        else
          return Some(xvalue);
    }
  }

  public function getAlt(key : K, alt : V, comparator : K -> K -> Int) : V
    return get(key, comparator).toValueWithAlt(alt);

  public function getTuple(key : K, comparator : K -> K -> Int) : Option<Tuple<K, V>> {
    switch this {
      case Tip:
        return None;
      case Bin(size, xkey, xvalue, lhs, rhs):
        var c = comparator(key, xkey);
        if(c < 0)
          return lhs.getTuple(key, comparator);
        else if(c > 0)
          return rhs.getTuple(key, comparator);
        else
          return Some(new Tuple(xkey, xvalue));
    }
  }

  public function exists(key : K, ?comparator : K -> K -> Int) : Bool
    return get(key, comparator).toBool();

  public function size()
    return switch this {
      case Tip: 0;
      case Bin(size, _, _, _, _): size;
    };

  inline static var delta = 5;
  inline static var ratio = 2;
  function balance(key : K, value : V, lhs : Map<K, V>, rhs : Map<K, V>) : Map<K, V> {
    var ls = lhs.size(),
        rs = rhs.size();
    if(ls + rs <= 1)
      return Bin(ls + rs + 1, key, value, lhs, rhs);
    else if(rs >= delta * ls)
      return rotateLeft(key, value, lhs, rhs);<commit_msg>use Ord instead of Int comparison<commit_after>package thx.fp;

import haxe.ds.Option;
import thx.Tuple;
import thx.Ord;
using thx.Options;

abstract Map<K, V>(MapImpl<K, V>) from MapImpl<K, V> to MapImpl<K, V> {
  inline public static function empty<K, V>() : Map<K, V>
    return Tip;
  inline public static function singleton<K, V>(k : K, v : V) : Map<K, V>
    return Bin(1, k, v, Tip, Tip);
  inline public static function bin<K, V>(k : K, v : V, lhs : Map<K, V>, rhs : Map<K, V>) : Map<K, V>
    return Bin(lhs.size() + 1 + rhs.size() + 1, k, v, lhs, rhs);

  public function get(key : K, comparator : Ord<K>) : Option<V> {
    switch this {
      case Tip:
        return None;
      case Bin(size, xkey, xvalue, lhs, rhs):
        var c = comparator(key, xkey);
        switch c {
          case LT:
            return lhs.get(key, comparator);
          case GT:
            return rhs.get(key, comparator);
          case EQ:
            return Some(xvalue);
        };
    }
  }

  public function getAlt(key : K, alt : V, comparator : Ord<K>) : V
    return get(key, comparator).toValueWithAlt(alt);

  public function getTuple(key : K, comparator : Ord<K>) : Option<Tuple<K, V>> {
    switch this {
      case Tip:
        return None;
      case Bin(size, xkey, xvalue, lhs, rhs):
        var c = comparator(key, xkey);
        switch c {
          case LT:
            return lhs.getTuple(key, comparator);
          case GT:
            return rhs.getTuple(key, comparator);
          case EQ:
            return Some(new Tuple(xkey, xvalue));
        }
    }
  }
/*
  public function set(key : K, value : V, comparator : Ord<K>) : Map<K, V> {

  }
*/
  public function exists(key : K, comparator : Ord<K>) : Bool
    return get(key, comparator).toBool();

  public function size()
    return switch this {
      case Tip: 0;
      case Bin(size, _, _, _, _): size;
    };
/*
  public function toStringWithShow(showKey : K -> String, showValue : V -> String) : String {

  }
*/
  // utility methods
  inline static var delta = 5;
  inline static var ratio = 2;
  function balance(key : K, value : V, lhs : Map<K, V>, rhs : Map<K, V>) : Map<K, V> {
    var ls = lhs.size(),
        rs = rhs.size();
    if(ls + rs <= 1)
      return Bin(ls + rs + 1, key, value, lhs, rhs);
    else if(rs >= delta * ls)
      return rotateLeft(key, value, lhs, rhs);
{'fraction_non_alphanumeric': 0.14598375451263537, 'fraction_numerical': 0.004512635379061372, 'mean_word_length': 2.323088455772114, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1022574', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1516, 'n_words': 499}