<commit_before>/*
 *  @author <NAME>
 *  @author <NAME>
 *  
 *  Copyright 2013 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

package com.signalcollect.triplerush
import scala.Option.option2Iterable
import scala.runtime.ScalaRunTime
import scala.util.hashing.MurmurHash3._
import scala.language.implicitConversions

object EfficientIndexPattern {

  implicit def longToIndexPattern(l: Long): EfficientIndexPattern = new EfficientIndexPattern(l)

  @inline def embed2IntsInALong(i1: Int, i2: Int): Long = {
    ((i2 | 0l) << 32) | (i1 & 0x00000000FFFFFFFFL)
  }

  @inline def apply(pattern: TriplePattern): Long = {
    apply(pattern.s, pattern.p, pattern.o)
  }

  @inline def apply(s: Int, p: Int, o: Int): Long = {
    assert(
      s >= 0 &&
        p >= 0 &&
        o >= 0,
      "EfficientIndexPattern does not support variables.")
    assert(
      s == 0 ||
        p == 0 ||
        o == 0,
      "EfficientIndexPattern needs for at least one ID to be a wildcard.")
    if (s != 0) {
      val first = s
      val second = if (o != 0) {
        o
      } else {
        p | Int.MinValue
      }
      embed2IntsInALong(first, second)
    } else {
      val first = p | Int.MinValue
      val second = o
      embed2IntsInALong(first, second)
    }
  }
}

class EfficientIndexPattern(val id: Long) extends AnyVal {

  @inline def isQueryId: Boolean = {
    id < 0 && id.toInt < 0
  }

  @inline def toTriplePattern = TriplePattern(s, p, o)

  @inline def parentIds: List[Long] = {
    toTriplePattern.parentPatterns.map(_.toEfficientIndexPattern)
  }

  @inline def parentIdDelta(parentPattern: Long): Int = {
    import EfficientIndexPattern._
    if (parentPattern.s == 0 && s != 0) {
      s
    } else if (parentPattern.p == 0 && p != 0) {
      p
    } else if (parentPattern.o == 0 && o != 0) {
      o
    } else {
      throw new Exception(s"$parentPattern is not a parent pattern of $this")
    }
  }

  @inline def extractFirst = id.toInt

  @inline def extractSecond = (id >> 32).toInt

  @inline def s = {
    val first = extractFirst
    if (first < 0) {
      0
    } else {
      first
    }
  }

  @inline def o = {
    if (id < 0) { // second < 0
      0
    } else {
      extractSecond
    }
  }

  @inline def p = {
    val first = extractFirst
    if (first < 0) {
      first & Int.MaxValue
    } else {
      if (id < 0) { // second < 0
        extractSecond & Int.MaxValue
      } else {
        0
      }
    }
  }

}

/**
 * Pattern of 3 expressions (subject, predicate object).
 * They are represented as Int, but can be converted to Expression implicitly and for free (value class).
 */<commit_msg>Improved Long decoding performance.<commit_after>/*
 *  @author <NAME>
 *  @author <NAME>
 *  
 *  Copyright 2013 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

package com.signalcollect.triplerush
import scala.Option.option2Iterable
import scala.runtime.ScalaRunTime
import scala.util.hashing.MurmurHash3._
import scala.language.implicitConversions

object EfficientIndexPattern {

  implicit def longToIndexPattern(l: Long): EfficientIndexPattern = new EfficientIndexPattern(l)

  @inline def embed2IntsInALong(i1: Int, i2: Int): Long = {
    ((i2 | 0l) << 32) | (i1 & 0x00000000FFFFFFFFL)
  }

  @inline def apply(pattern: TriplePattern): Long = {
    apply(pattern.s, pattern.p, pattern.o)
  }

  @inline def apply(s: Int, p: Int, o: Int): Long = {
    assert(
      s >= 0 &&
        p >= 0 &&
        o >= 0,
      "EfficientIndexPattern does not support variables.")
    assert(
      s == 0 ||
        p == 0 ||
        o == 0,
      "EfficientIndexPattern needs for at least one ID to be a wildcard.")
    if (s != 0) {
      val first = s
      val second = if (o != 0) {
        o
      } else {
        p | Int.MinValue
      }
      embed2IntsInALong(first, second)
    } else {
      val first = p | Int.MinValue
      val second = o
      embed2IntsInALong(first, second)
    }
  }
}

class EfficientIndexPattern(val id: Long) extends AnyVal {

  @inline def isQueryId: Boolean = {
    id < 0 && id.toInt < 0
  }

  @inline def toTriplePattern: TriplePattern = {
    val first = extractFirst
    val second = extractSecond
    val s = math.max(0, first)
    val o = math.max(0, second)
    val p = if (first < 0) {
      first & Int.MaxValue
    } else {
      if (second < 0) { // second < 0
        second & Int.MaxValue
      } else {
        0
      }
    }
    TriplePattern(s, p, o)
  }

  @inline def parentIds: List[Long] = {
    toTriplePattern.parentPatterns.map(_.toEfficientIndexPattern)
  }

  @inline def parentIdDelta(parentPattern: Long): Int = {
    import EfficientIndexPattern._
    if (parentPattern.s == 0 && s != 0) {
      s
    } else if (parentPattern.p == 0 && p != 0) {
      p
    } else if (parentPattern.o == 0 && o != 0) {
      o
    } else {
      throw new Exception(s"$parentPattern is not a parent pattern of $this")
    }
  }

  @inline def extractFirst = id.toInt

  @inline def extractSecond = (id >> 32).toInt

  @inline def s = math.max(0, extractFirst)

  @inline def o = math.max(0, extractSecond)

  @inline def p = {
    val first = extractFirst
    if (first < 0) {
      first & Int.MaxValue
    } else {
      if (id < 0) { // second < 0
        extractSecond & Int.MaxValue
      } else {
        0
      }
    }
  }

}

/**
 * Pattern of 3 expressions (subject, predicate object).
 * They are represented as Int, but can be converted to Expression implicitly and for free (value class).
 */
{'fraction_non_alphanumeric': 0.10787127964949388, 'fraction_numerical': 0.017676386161051518, 'mean_word_length': 2.394871794871795, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '1564011', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2086, 'n_words': 688}