     Cells.sortXY pattern
     key = patternKey pattern
     if (result = @pattern2result[key])?
-      return unwrapResult result
+      #console.log "#### FIrst hit!"
+      return result
     else
-      result = {resolution:null} #result would be calculated later - now only make an empty placeholder
-      @registerResult pattern, result, key
+      @stashKeys pattern, key
                 
     bestPatternSearch = new Maximizer Cells.energy
     bestPatternSearch.put pattern

    @maxPopulation = 1024
    @maxSize = 100
    @symmetries = ruleSpatialSymmetries rule

  registerResult: (pattern, result, mainKey) ->
      #Register a pattern with given result, under all keys
      pattern2result = @pattern2result
      pattern2result[mainKey] = result
      for tfm in @symmetries
        transformedKey = patternKey Cells.transform pattern, tfm
        if transformedKey isnt mainKey
          #assert that key either not present, or refers the same result
          #if pattern2result[transformedKey]?
          #  
          pattern2result[transformedKey] = result


  analyse: (pattern) ->
    result = @_analyse pattern
    result.hits += 1
    return result

  _analyse: (pattern) -> #result

      key = patternKey curPattern
      #Maybe, we already had this value?
      if (knownResult = @pattern2result[key])?
        #Write current result to the stashed keys
        #console.log "#### known result:"+JSON.stringify(knownResult)
        result = knownResult
        break

      #maybe, analysis has finished?
      if Cells.areEqual pattern, curPattern
        result = @makePeriodFoundResult dx, dy, iter, bestPatternSearch.getArg()
        break
        
      #previously unknown pattern. Remember its keys
      @stashKeys curPattern, key

      #no cycle. Fine.
      bestPatternSearch.put curPattern
      if curPattern.length > maxPopulation
        result = @makePatternTooBigResult()
        break
      if Math.max( bounds[2]-bounds[0], bounds[3]-bounds[1]) > maxSize
        result = @makePatternToWideResult()
        break

    unless result?
      #After the given number of iterations, nothing was found.
      #Give up.
      result = @makeCycleNotFoundResult()

    @writeResult result
    return result


  makePeriodFoundResult: (dx, dy, period, bestPattern)->
    #console.log "#### period found"
    [bestPattern, dx0, dy0] =
         Cells.canonicalize_spaceship bestPattern, @rule, dx, dy
    return {
      resolution: Resolution.HAS_PERIOD
      dx: dx0
      dy: dy0
      period: period
      cells: bestPattern
      hits: 0
    }
    
  makePatternToWideResult: ->
    #console.log "#### pattern too wide"
    return {
      resolution: Resolution.TOO_WIDE
      hits: 0
    }
  makePatternTooBigResult: ->   
    #console.log "#### pattern too big"
    return {
      resolution: Resolution.OVERPOPUPATION
      hits: 0
    }
  makeCycleNotFoundResult: ->
    #console.log "#### no cycle found"
    return {
      resolution: Resolution.NO_PERIOD
      hits: 0
    }

  #Truncate memo table to the given number of records, removing items with the least hit count
  truncateTable: (maxRecords) ->
    if maxRecords < 0
      throw new Error "Number of records must be positive"
    pattern2result = @pattern2result
    keysWithHits = ([result.hits, key] for key, result of pattern2result)
    #sort keys by hit count, more hits go first
    keysWithHits.sort (kh1, kh2) -> kh2[0]-kh1[0]
    #remove the rest keys
