Skip to content

Instantly share code, notes, and snippets.

@kevinAlbs
Created November 29, 2022 13:35
Show Gist options
  • Save kevinAlbs/602f47e00788ad16dc1286dcfa444420 to your computer and use it in GitHub Desktop.
Save kevinAlbs/602f47e00788ad16dc1286dcfa444420 to your computer and use it in GitHub Desktop.
import sys
import re
contents = sys.stdin.read()
# Here is a sample input:
# contents = """
# {&getEdgesDouble, 0.00000000000000000, boost::none, boost::none, 1, {
# "1",
# "10",
# "100",
# "1000",
# "10000",
# "100000",
# "1000000",
# "10000000",
# "100000000",
# "1000000000",
# "10000000000",
# "100000000000",
# "1000000000000",
# "10000000000000",
# "100000000000000",
# "1000000000000000",
# "10000000000000000",
# "100000000000000000",
# "1000000000000000000",
# "10000000000000000000",
# "100000000000000000000",
# "1000000000000000000000",
# "10000000000000000000000",
# "100000000000000000000000",
# "1000000000000000000000000",
# "10000000000000000000000000",
# "100000000000000000000000000",
# "1000000000000000000000000000",
# "10000000000000000000000000000",
# "100000000000000000000000000000",
# "1000000000000000000000000000000",
# "10000000000000000000000000000000",
# "100000000000000000000000000000000",
# "1000000000000000000000000000000000",
# "10000000000000000000000000000000000",
# "100000000000000000000000000000000000",
# "1000000000000000000000000000000000000",
# "10000000000000000000000000000000000000",
# "100000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000000000000000000000",
# "10000000000000000000000000000000000000000000000000000000000000",
# "100000000000000000000000000000000000000000000000000000000000000",
# "1000000000000000000000000000000000000000000000000000000000000000",
# "root",
# }},
# """
lines = contents.splitlines ()
edges = []
import textwrap
tmpl = """
{{
.value = {value},
.sparsity = {sparsity},
.expectEdges = {{
{expectEdgesString}
}}
}},
"""
print ("""// This is a copy of test vectors from the mongodb/mongo repository.
// The order of edges is rearranged to permit exact matching.
""")
lineno = 0
import sys
for line in lines:
lineno += 1
match = re.match(r".*\{.+, (.+), (.+), (.+), ([-0-9]+),", line)
if match != None:
value = match.group(1)
min = match.group(2)
max = match.group(3)
sparsity = match.group(4)
match = re.match (r'.*"(.*)",', line)
if match != None:
edges.append('"' + match.group(1) + '"')
elif re.match (r'.*\}\},', line) != None:
# Rewrite edges. "root" should be first. Then leaf. Then remaining.
edges.insert(0, edges[-2])
edges.insert(0, '"root"')
edges = edges[0:-2]
if value is None:
print (f"value is unexpectedly None on line {lineno}")
sys.exit(1)
if sparsity is None:
print (f"sparsity is unexpectedly None on line {lineno}")
sys.exit(1)
if value == "std::numeric_limits<double>::min()":
value = "DBL_MIN"
if value == "std::numeric_limits<double>::max()":
value = "DBL_MAX"
got = tmpl.format(
value=value,
sparsity=sparsity,
expectEdgesString= textwrap.indent (",\n".join(edges), " ")
)
print(got.strip())
edges = []
value = None
min = None
max = None
sparsity = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment