These are chat archives for mdedetrich/scala-json-ast

5th
Apr 2016
Matthew de Detrich
@mdedetrich
Apr 05 2016 12:57

@Ichoran this is the implementation I did for hashcode.


  private[ast] def numericStringHashcode(value: String): Int = {
    var result = 7

    val split = value.split("e|E")

    {
      var i = 0
      val length = split(0).length

      if (split(0)(0) == '-') {
        result = result * 31
      }

      while (i < length) {
        val char = split(0)(i)
        if (char == '.') {
          result = 31 * result
        } else {
          if (char != '0') {
            result = 31 * result + char: Int
          }
        }

        i = i + 1
      }
    }

    if (split.length > 1) {
      result = 31 * result

      {
        var i = 0
        val length = split(1).length

        while (i < length) {
          val char = split(1)(i)
          if (char != '0') {
            result = 31 * result + char: Int
          }
          i = i + 1
        }
      }
    }

    result
  }

I think the reasoning behind it is fine, let me know if it looks good to you. Ill push the final revision of scala-json-ast if all is good

Matthew de Detrich
@mdedetrich
Apr 05 2016 13:08
Actually I can probably improve the performance a bit more by removing the split and detecting e|E while using a break
Matthew de Detrich
@mdedetrich
Apr 05 2016 13:15
Yeah this version should be a lot faster
  private[ast] def numericStringHashcode(value: String): Int = {
    var result = 7

    {
      var i = 0
      val length = value.length

      if (value(0) == '-') {
        result = result * 31
      }

      while (i < length) {
        val char = value(i)
        if (!(char == 'e' || char == 'E')) {
          if (char == '.') {
            result = 31 * result
          } else {
            if (char != '0') {
              result = 31 * result + char: Int
            }
          }
        } else {
          result = 31 * result
        }

        i = i + 1
      }
    }

    result
  }
@Ichoran Not sure how much ignoring 0 will effect collisions though
Ill actually have a look to see what Java does
Matthew de Detrich
@mdedetrich
Apr 05 2016 13:22
Oh wow, the java json AST was dropped, nvm then
Matthew de Detrich
@mdedetrich
Apr 05 2016 13:27
@Ichoran Also I just realised that in your jsonal, it doesn’t take into account E which is an alias for e when doing exponent
Matthew de Detrich
@mdedetrich
Apr 05 2016 13:33
Just created Ichoran/kse#1
Also I will probably implement some proper tests for these methods before pushing
Matthew de Detrich
@mdedetrich
Apr 05 2016 14:00
@Ichoran I am not sure if its worth it to detect if the number can fit as a long so you can do the (value ^ (value >>> 32)): Int trick
nafg
@nafg
Apr 05 2016 18:06
A new JSON library! http://eed3si9n.com/sjson-new
Matthew de Detrich
@mdedetrich
Apr 05 2016 23:23
Not sure if aprils fools or not, lol