Пример #1
0
// Sort performs a topological sort of the graph
// https://en.wikipedia.org/wiki/Topological_sorting
//
// If the graph can be topologically sorted the result will
// contain the sorted nodes.
//
// If the graph cannot be sorted in case of circular dependencies,
// then the result will contain the remaining nodes from the graph,
// which are the ones causing the circular dependency.
func (g *Graph) Sort() ([]*Node, error) {
	var sorted []*Node

	// Iteratively find and remove nodes from the graph which have no edges.
	// If at some point there are still nodes in the graph and we cannot find
	// nodes without edges, that means we have a circular dependency
	for len(g.Nodes) > 0 {
		// Contains the ready nodes, which have no edges to other nodes
		//ready := make([]*Node, 0)
		ready := mapset.NewSet()

		// Find the nodes with no edges
		for _, node := range g.Nodes {
			if len(node.Edges) == 0 {
				ready.Add(node)
			}
		}

		// If there aren't any ready nodes, then we have a cicular dependency
		if ready.Cardinality() == 0 {
			// The remaining nodes in the graph are the ones causing the
			// circular dependency.
			var remaining []*Node
			for _, n := range g.Nodes {
				remaining = append(remaining, n)
			}
			return remaining, ErrCircularDependency
		}

		// Remove the ready nodes and add them to the sorted result
		for item := range ready.Iter() {
			node := item.(*Node)
			delete(g.Nodes, node.Name)
			sorted = append(sorted, node)
		}

		// Remove ready nodes from the remaining node edges as well
		for _, node := range g.Nodes {
			// Add the remaining nodes in a set
			currentEdgeSet := mapset.NewSet()
			for _, edge := range node.Edges {
				currentEdgeSet.Add(edge)
			}

			newEdgeSet := currentEdgeSet.Difference(ready)
			node.Edges = make([]*Node, 0)
			for edge := range newEdgeSet.Iter() {
				node.Edges = append(node.Edges, edge.(*Node))
			}
		}
	}

	return sorted, nil
}
Пример #2
0
func hosts(entries discovery.Entries) mapset.Set {
	hosts := mapset.NewSet()
	for _, entry := range entries {
		hosts.Add(entry.Host)
	}
	return hosts
}
Пример #3
0
func makeSet(ss []string) mapset.Set {
	set := mapset.NewSet()
	for _, s := range ss {
		set.Add(s)
	}
	return set
}
Пример #4
0
// Create a new watcher.
func NewWatcher() *watcher {
	w := &watcher{
		InterfaceAdded:   make(chan string),
		InterfaceRemoved: make(chan string),
		oldNames:         set.NewSet(),
	}
	go w.watch()
	return w
}
Пример #5
0
func main() {
	requiredClasses := set.NewSet()
	requiredClasses.Add("Cooking")
	requiredClasses.Add("English")
	requiredClasses.Add("Math")
	requiredClasses.Add("Biology")

	scienceClasses := set.NewSet()
	scienceClasses.Add("Biology")
	scienceClasses.Add("Chemistry")

	electiveClasses := set.NewSet()
	electiveClasses.Add("Welding")
	electiveClasses.Add("Music")
	electiveClasses.Add("Automotive")

	bonusClasses := set.NewSet()
	bonusClasses.Add("Go Programming")
	bonusClasses.Add("Python Programming")

	allClasses := requiredClasses.Union(scienceClasses).
		Union(electiveClasses).Union(bonusClasses)

	fmt.Println(scienceClasses.Contains("Cooking"))
	//Returns: false

	// what are all the classes I can take that are not science classes?
	fmt.Println(allClasses.Difference(scienceClasses))
	//Returns: Set{Music, Automotive, Go Programming, Python Programming, Cooking, English, Math, Welding}

	// Which of the science classes are also required classes?
	fmt.Println(scienceClasses.Intersect(requiredClasses))
	//Returns: Set{Biology}

	// How many bonus classes are offered for this winter schedule?
	fmt.Println(bonusClasses.Cardinality())
	//Returns: 2

	// Check exist
	fmt.Println(allClasses.Contains("Welding", "Automotive", "English"))
	//Returns: true
}
func (suite *StatsTestSuite) Test_Regex() {
	// We'll make a new client with actual regexps.
	pageViewRegexes := make([]map[string]string, 0)
	httpsRequestRegexes := []map[string]string{make(map[string]string), make(map[string]string)}
	httpsRequestRegexes[0]["regex"] = `^[a-z0-9\.]*\.(example\.com)$`
	httpsRequestRegexes[0]["replace"] = "$1"
	httpsRequestRegexes[1]["regex"] = `^.*example\.org$`
	httpsRequestRegexes[1]["replace"] = "replacement"
	regexps, _ := MakeRegexps(pageViewRegexes, httpsRequestRegexes)

	suite.httpClient = &http.Client{
		Transport: &http.Transport{
			Dial: makeStatsDialer(_SERVER_ID, regexps),
		},
	}

	// Using both HTTP and HTTPS will help us to exercise both methods of hostname parsing
	for _, scheme := range []string{"http", "https"} {
		// No subdomain, so won't match regex
		url := fmt.Sprintf("%s://example.com/index.html", scheme)
		resp, err := suite.httpClient.Get(url)
		suite.Nil(err)
		resp.Body.Close()

		// Will match the first regex
		url = fmt.Sprintf("%s://www.example.com/index.html", scheme)
		resp, err = suite.httpClient.Get(url)
		suite.Nil(err)
		resp.Body.Close()

		// Will match the second regex
		url = fmt.Sprintf("%s://example.org/index.html", scheme)
		resp, err = suite.httpClient.Get(url)
		suite.Nil(err)
		resp.Body.Close()

		payload := GetForServer(_SERVER_ID)
		suite.NotNil(payload, "should get stats because we made HTTP reqs; %s", scheme)

		expectedHostnames := mapset.NewSet()
		expectedHostnames.Add("(OTHER)")
		expectedHostnames.Add("example.com")
		expectedHostnames.Add("replacement")

		hostnames := make([]interface{}, 0)
		for hostname := range payload.hostnameToStats {
			hostnames = append(hostnames, hostname)
		}

		actualHostnames := mapset.NewSetFromSlice(hostnames)

		suite.Equal(expectedHostnames, actualHostnames, "post-regex hostnames should be processed as expecteds; %s", scheme)
	}
}
Пример #7
0
func readConfig() {
	config = make([]Entry, 0)
	configMap = make(map[string]Entry)
	file, _ := os.Open("conf.json")
	decoder := json.NewDecoder(file)
	err := decoder.Decode(&config)
	if err != nil {
		fmt.Println("error:", err)
	}
	for id, entry := range config {
		configMap[entry.Name] = config[id]
	}
	toRun = set.NewSet()
}
Пример #8
0
func main() {
	readConfig()

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		run := false
		delay := 1000 * time.Millisecond
		timer := time.NewTimer(delay)
		updated := set.NewSet()
		for {
			select {
			case <-timer.C:
				if run {
					run = false
					filesUpdated(updated)
					updated.Clear()
				}
			case event := <-watcher.Events:
				timer.Reset(delay)
				//if event.Op&fsnotify.Write == fsnotify.Write {
				run = true
				updated.Add(event.Name)
				//}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	wd, err := os.Getwd()
	if err == nil {
		fmt.Println("Monitoring", wd)
		err = watcher.Add(wd)
	}
	if err != nil {
		log.Fatal(err)
	}
	<-done
}
Пример #9
0
// Refresh the list of interfaces.
func (w *watcher) refresh() error {
	newNames := set.NewSet()
	ifis, err := net.Interfaces()
	if err != nil {
		return err
	}
	for _, ifi := range ifis {
		if ifi.Flags&net.FlagUp != 0 && ifi.Flags&(net.FlagBroadcast|net.FlagMulticast) != 0 {
			newNames.Add(ifi.Name)
		}
	}
	for name := range newNames.Difference(w.oldNames).Iter() {
		w.InterfaceAdded <- name.(string)
	}
	for name := range w.oldNames.Difference(newNames).Iter() {
		w.InterfaceRemoved <- name.(string)
	}
	w.oldNames = newNames
	return nil
}
Пример #10
0
// NewHostDiscovery function creates a host discovery object
func NewHostDiscovery(watcher discovery.Watcher) HostDiscovery {
	return &hostDiscovery{watcher: watcher, nodes: mapset.NewSet(), stopChan: make(chan struct{})}
}
Пример #11
0
// NewHostDiscovery function creates a host discovery object
func NewHostDiscovery() HostDiscovery {
	return &hostDiscovery{nodes: mapset.NewSet(), stopChan: make(chan struct{})}
}
Пример #12
0
func (om *ohmModel) Set(key string) ohmSet {
	return ohmSet(set.NewSet())
}