<commit_before>package org.gavrog.scratch

import java.io.{DataInputStream, EOFException, InputStream}
import java.lang.ProcessBuilder

import scala.collection.mutable.HashMap

object Cyclo {
  type Graph = Map[Int, List[Int]]
  
  var total_count = 0

  def degree_sequences(n: Int, m: Int) = {
    def seq(n: Int, m: Int, min_d: Int) : Seq[List[Int]] =
      if (n * min_d > m)
        Nil
      else if (n == 0)
        if (m == 0) List(Nil) else Nil
      else for (i <- 0 to m / min_d if i <= n;
                s <- seq(n - i, m - i * min_d, min_d + 1))
        yield i :: s
    
    seq(n, 2 * m, 3)
  }
  
  def read_graphs(is: InputStream) : Stream[Graph] = {
    val src = new DataInputStream(is)
    
    val first_byte = try {
      Some(src.readByte)
    } catch {
      case ex: EOFException => None
    }
    
    first_byte match {
      case None => Stream.empty
      case Some(x) => {
        def next: Int = if (x == 0) src.readUnsignedShort else src.readByte
        val size: Int = if (x == 0) next else x
        val adj = new HashMap[Int, List[Int]]
        var i = 1
        while (i < size) {
          val j = next
          if (j == 0) i += 1
          else {
            adj(i) = j :: adj.getOrElse(i, Nil)
            adj(j) = i :: adj.getOrElse(j, Nil)
          }
        }
        Stream.cons(Map(adj.toSeq: _*), read_graphs(is))
      }
    }
  }
  
  def simplified(gr: Graph) = {
    var old2new  : Map[Int, Int]    = Map().withDefaultValue(0)
    var new_edges: List[(Int, Int)] = Nil
    
    for ((i, neighbors) <- gr)
      if (neighbors.size == 2)
        new_edges = (neighbors(0), neighbors(1)) :: new_edges
      else old2new += (i -> (old2new.size + 1))
    
    val output = new HashMap[Int, List[Int]]
    
    for ((i, neighbors) <- gr if old2new(i) != 0)
      output(old2new(i)) = neighbors map old2new filter (0 !=)
    
    for ((i, j) <- new_edges) {
      val a = old2new(i)
      val b = old2new(j)
      output(a) = b :: output(a)
      if (a != b) output(b) = a :: output(b)
    }
    
    Map(output.toSeq: _*)
  }
  
  def edges(gr: Graph) =
    for ((i, neighbors) <- gr; j <- neighbors if i <= j) yield (i, j)
  
  def generate(seq: List[Int]) : Seq[Graph] = {
    val args   = "mgraph_p" :: seq.map(_.toString) ::: List("o", "p")
    val proc   = new ProcessBuilder(args.toArray: _*).start
    val result = read_graphs(proc.getInputStream)
    proc.waitFor
    result
  }
  
  def main(args : Array[String]) : Unit = {
    try {
      val k = args(0).toInt
      for (n <- 0 to 2 * (k - 1);
           s <- degree_sequences(n, n + k - 1);
           l <- 0 to k)
      {
        val graphs = generate(0 :: l :: s)
        val n = graphs.size
        println("# Found %d graph%s with degree sequence (%s) and %d loop%s%s"
                format (n, if (n == 1) "" else "s",
                        s map (_.toString) reduceLeft(_ + ", " + _),
                        l, if (l == 1) "" else "s",
                        if (n > 0) ":" else "."))
        for (g  <- graphs map simplified)
          println(edges(g) map (_.toString) reduceLeft(_ + _))
        total_count += n
      }

      println
      println("# " + "=" * 72)
      println("# A total of %d genus %d graphs were generated."
              format (total_count, k))
    } catch {
      case ex: Throwable => println(ex)
    }
  }
}<commit_msg>Cyclo: avoids the word "genus" in the output.<commit_after>package org.gavrog.scratch

import java.io.{DataInputStream, EOFException, InputStream}
import java.lang.ProcessBuilder

import scala.collection.mutable.HashMap

object Cyclo {
  type Graph = Map[Int, List[Int]]
  
  var total_count = 0

  def degree_sequences(n: Int, m: Int) = {
    def seq(n: Int, m: Int, min_d: Int) : Seq[List[Int]] =
      if (n * min_d > m)
        Nil
      else if (n == 0)
        if (m == 0) List(Nil) else Nil
      else for (i <- 0 to m / min_d if i <= n;
                s <- seq(n - i, m - i * min_d, min_d + 1))
        yield i :: s
    
    seq(n, 2 * m, 3)
  }
  
  def read_graphs(is: InputStream) : Stream[Graph] = {
    val src = new DataInputStream(is)
    
    val first_byte = try {
      Some(src.readByte)
    } catch {
      case ex: EOFException => None
    }
    
    first_byte match {
      case None => Stream.empty
      case Some(x) => {
        def next: Int = if (x == 0) src.readUnsignedShort else src.readByte
        val size: Int = if (x == 0) next else x
        val adj = new HashMap[Int, List[Int]]
        var i = 1
        while (i < size) {
          val j = next
          if (j == 0) i += 1
          else {
            adj(i) = j :: adj.getOrElse(i, Nil)
            adj(j) = i :: adj.getOrElse(j, Nil)
          }
        }
        Stream.cons(Map(adj.toSeq: _*), read_graphs(is))
      }
    }
  }
  
  def simplified(gr: Graph) = {
    var old2new  : Map[Int, Int]    = Map().withDefaultValue(0)
    var new_edges: List[(Int, Int)] = Nil
    
    for ((i, neighbors) <- gr)
      if (neighbors.size == 2)
        new_edges = (neighbors(0), neighbors(1)) :: new_edges
      else old2new += (i -> (old2new.size + 1))
    
    val output = new HashMap[Int, List[Int]]
    
    for ((i, neighbors) <- gr if old2new(i) != 0)
      output(old2new(i)) = neighbors map old2new filter (0 !=)
    
    for ((i, j) <- new_edges) {
      val a = old2new(i)
      val b = old2new(j)
      output(a) = b :: output(a)
      if (a != b) output(b) = a :: output(b)
    }
    
    Map(output.toSeq: _*)
  }
  
  def edges(gr: Graph) =
    for ((i, neighbors) <- gr; j <- neighbors if i <= j) yield (i, j)
  
  def generate(seq: List[Int]) : Seq[Graph] = {
    val args   = "mgraph_p" :: seq.map(_.toString) ::: List("o", "p")
    val proc   = new ProcessBuilder(args.toArray: _*).start
    val result = read_graphs(proc.getInputStream)
    proc.waitFor
    result
  }
  
  def main(args : Array[String]) : Unit = {
    try {
      val k = args(0).toInt
      for (n <- 0 to 2 * (k - 1);
           s <- degree_sequences(n, n + k - 1);
           l <- 0 to k)
      {
        val graphs = generate(0 :: l :: s)
        val n = graphs.size
        println("# Found %d graph%s with degree sequence (%s) and %d loop%s%s"
                format (n, if (n == 1) "" else "s",
                        s map (_.toString) reduceLeft(_ + ", " + _),
                        l, if (l == 1) "" else "s",
                        if (n > 0) ":" else "."))
        for (g  <- graphs map simplified)
          println(edges(g) map (_.toString) reduceLeft(_ + _))
        total_count += n
      }

      println
      println("# " + "=" * 72)
      println("# A total of %d graphs with cyclomatic number %d were generated."
              format (total_count, k))
    } catch {
      case ex: Throwable => println(ex)
    }
  }
}
{'fraction_non_alphanumeric': 0.1499484460156135, 'fraction_numerical': 0.011489173663278833, 'mean_word_length': 1.7839278392783928, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 17, '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': '109139', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2370, 'n_words': 693}