func tarjanSort(successors map[bson.ObjectId][]bson.ObjectId) [][]bson.ObjectId { // http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm data := &tarjanData{ successors: successors, nodes: make([]tarjanNode, 0, len(successors)), index: make(map[bson.ObjectId]int, len(successors)), } for id := range successors { id := bson.ObjectId(string(id)) if _, seen := data.index[id]; !seen { data.strongConnect(id) } } // Sort connected components to stabilize the algorithm. for _, ids := range data.output { if len(ids) > 1 { sort.Sort(idList(ids)) } } return data.output }
func bid(n int) bson.ObjectId { return bson.ObjectId(fmt.Sprintf("%024d", n)) }