Created
August 27, 2013 18:58
-
-
Save dacc/6357580 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" | |
) | |
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) { | |
fmt.Printf("Reading %s...", file) | |
bytes, err := ioutil.ReadFile(file) | |
fmt.Print("Done.") | |
lines = strings.Split(string(bytes), "\n") | |
return | |
} | |
func loadGraph() (titles []string, adjacencies [][]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 } | |
adjacencies = make([][]uint32, len(adjacency_lines) + 1) | |
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 { break } | |
} | |
origin, err := strconv.ParseUint(strings.TrimSuffix(origin_component, ":"), 10, 32) | |
if err != nil { break } | |
targets, err := parseUint32s(components[1:]) | |
if err != nil { break } | |
if int(origin) > len(adjacencies) - 1 { | |
err = errors.New(fmt.Sprintf("origin to large line: %d, value: %d", i, origin)) | |
break | |
} | |
adjacencies[origin] = targets | |
} | |
return | |
} | |
func main() { | |
titles, adjacencies, err := loadGraph() | |
if err != nil { panic(err) } | |
fmt.Println("titles: %d, adjacencies: %d", len(titles), len(adjacencies)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment