Created
August 27, 2013 22:00
-
-
Save dacc/6359738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created with IntelliJ IDEA. | |
* User: dacc | |
* Date: 8/27/13 | |
* Time: 10:01 AM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
package main | |
import ( | |
"io/ioutil" | |
"strings" | |
"errors" | |
"fmt" | |
"strconv" | |
"log" | |
) | |
func panicIfNotNil(err error) { | |
if err != nil { panic(err) } | |
} | |
func parseUint32s(strings []string) (uints []uint32, err error) { | |
uints = make([]uint32, len(strings)) | |
for i, s := range strings { | |
result, err := strconv.ParseUint(s, 10, 32) | |
if err != nil { break } | |
uints[i] = uint32(result) | |
} | |
return | |
} | |
func readLines(file string) (lines []string, err error) { | |
log.Println("reading in", file, "...") | |
bytes, err := ioutil.ReadFile(file) | |
lines_raw := strings.Split(string(bytes), "\n") | |
lines = lines_raw[0:len(lines_raw) - 1] | |
return | |
} | |
func loadGraph() (titles []string, adjacencies map[uint32][]uint32, err error) { | |
titles, err = readLines("titles-sorted.txt") | |
if err != nil { return } | |
adjacency_lines, err := readLines("links-simple-sorted.txt") | |
if err != nil { return } | |
var origin uint64 | |
var targets []uint32 | |
adjacencies = make(map[uint32][]uint32) | |
for i, line := range adjacency_lines { | |
components := strings.Split(line, " ") | |
origin_component := components[0] | |
if ! strings.HasSuffix(origin_component, ":") { | |
err = errors.New(fmt.Sprintf("Bad adjacency line at %d: '%s'", i, line)) | |
if err != nil { return } | |
} | |
origin, err = strconv.ParseUint(strings.TrimSuffix(origin_component, ":"), 10, 32) | |
if err != nil { return } | |
targets, err = parseUint32s(components[1:]) | |
if err != nil { return } | |
adjacencies[uint32(origin)] = targets | |
if i % 100000 == 0 { | |
log.Println("parsed", i, "/", len(adjacency_lines), "adjacency lists...") | |
} | |
} | |
return | |
} | |
func main() { | |
titles, adjacencies, err := loadGraph() | |
if err != nil { panic(err) } | |
log.Println("titles:", len(titles), "adjacencies", len(adjacencies)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment