<commit_before>use geohash::interval::*;
use geohash::*;
use std::str;


pub fn encode(lat:f64, lon:f64, precision:uint) -> ~str {
  let mut ilat = Interval{ lo:-MAX_LAT, hi:MAX_LAT };
  let mut ilon = Interval{ lo:-MAX_LON, hi:MAX_LON };

  let bit_count : uint    = 5 * precision;
  let mut bits: ~[bool]   = ~[];
  let mut result: ~[char] = ~[];

  for i in range(0, bit_count) {
    if i.is_even() {
      let is_higher = lon > ilon.median();
      bits.push(is_higher);
      ilon.contract(if is_higher {UpperHalf} else {LowerHalf});
    } else {
      let is_higher = lat > ilat.median();
      bits.push(is_higher);
      ilat.contract(if is_higher {UpperHalf} else {LowerHalf});
    }
  }

  for char_bits in bits.chunk_iter(5) {
    let result_char = base32::encode_vec(char_bits).unwrap();
    result.push(result_char);
  }

  str::from_chars(result)
}

#[test]
fn test_encode_good() {
  let lat = 57.649109959602356;
  let lon = 10.407439023256302;

  assert!( Geohash::encode(lat, lon, 2)  == ~"u4" );
  assert!( Geohash::encode(lat, lon, 4)  == ~"u4pr" );
  assert!( Geohash::encode(lat, lon, 6)  == ~"u4pruy" );
  assert!( Geohash::encode(lat, lon, 8)  == ~"u4pruydq" );
}<commit_msg>Fixes encoding by using >= instead of ><commit_after>use geohash::interval::*;
use geohash::*;
use std::str;


pub fn encode(lat:f64, lon:f64, precision:uint) -> ~str {
  let mut ilat = Interval{ lo:-MAX_LAT, hi:MAX_LAT };
  let mut ilon = Interval{ lo:-MAX_LON, hi:MAX_LON };

  let bit_count : uint    = 5 * precision;
  let mut bits: ~[bool]   = ~[];
  let mut result: ~[char] = ~[];

  for i in range(0, bit_count) {
    if i.is_even() {
      let is_higher = lon >= ilon.median();
      bits.push(is_higher);
      ilon.contract(if is_higher {UpperHalf} else {LowerHalf});
    } else {
      let is_higher = lat >= ilat.median();
      bits.push(is_higher);
      ilat.contract(if is_higher {UpperHalf} else {LowerHalf});
    }
  }

  for char_bits in bits.chunk_iter(5) {
    let result_char = base32::encode_vec(char_bits).unwrap();
    result.push(result_char);
  }

  str::from_chars(result)
}

#[test]
fn test_encode_good() {
  let lat = 57.649109959602356;
  let lon = 10.407439023256302;

  assert!( Geohash::encode(lat, lon, 2)  == ~"u4" );
  assert!( Geohash::encode(lat, lon, 4)  == ~"u4pr" );
  assert!( Geohash::encode(lat, lon, 6)  == ~"u4pruy" );
  assert!( Geohash::encode(lat, lon, 8)  == ~"u4pruydq" );
  assert!( Geohash::encode(lat, lon, 10) == ~"u4pruydqqv" );
  assert!( Geohash::encode(lat, lon, 11) == ~"u4pruydqqvj" );
}
{'fraction_non_alphanumeric': 0.17785630153121318, 'fraction_numerical': 0.04240282685512368, 'mean_word_length': 3.6496350364963503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1630148', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1020, 'n_words': 222}