<commit_before>package alg

import (
	"graph"
	"container/heap"
	"container/list"
)

func isInList(w []graph.Way, s graph.Node) (bool,graph.Way) {
	var resultway graph.Way
	for _, way := range w {
		if s == way.Node {
			return true,way
		}
	}
	return false,resultway
}

// A slightly optimized version of dijkstras algorithm
// Takes an graph as argument and returns an list of vertices in order
// of the path
func Dijkstra(s, t []graph.Way) (float64, *list.List, *list.List,graph.Way,graph.Way) {
	d := make(map[graph.Node]float64)                // I assume distance is an integer
	p := make(map[graph.Node]graph.Node)               // Predecessor list
	ep := make(map[graph.Node]graph.Edge) // Edge Predecessors
	q := NewPriorityQueue(100 /* initialCapacity */) // 100 is just a first guess
	final := make(map[graph.Node]bool)
	for _, tar := range t {
		final[tar.Node] = false
	}
	for _, str := range s {
		priority := str.Length
		x := NewElement(str.Node, priority) // TODO check this cast
		heap.Push(&q, x)
		d[str.Node] = priority
	}
	for !q.Empty() {
		currElem := (heap.Pop(&q)).(*Element) // Get the first element
		curr := currElem.Value.(graph.Node)            // Unbox the node
		if isfinal, _ := final[curr]; isfinal {                    
			final[curr] = true
			finished := true
			for _, node := range final {
				finished = finished && node
			}
			if !finished {
				break
			}
		}
		currDist := d[curr]
		for _, e := range curr.Edges() {
			n := e.EndPoint()
			if dist, ok := d[n]; ok {
				if tmpDist := currDist + e.Length(); tmpDist < dist {
					q.ChangePriority(currElem, tmpDist) // TODO again check cast
					d[n] = tmpDist
					p[n] = curr
					ep[n]=e
				}
			} else {
				d[n] = currDist + e.Length()
				p[n] = curr
				elem := NewElement(n, currDist)  // TODO again check cast
				heap.Push(&q, elem)
			}
		}
	}
	path := list.New()
	edges := list.New()
	// Construct the list by moving from t to s
	first := true
	var curr graph.Node
	var currdist float64
	var endway graph.Way
	var startway graph.Way
	for _, targetnode := range t {
		tmpnode := targetnode.Node
		tmpdist := d[tmpnode] + targetnode.Length
		if first {
			curr = tmpnode
			currdist = tmpdist
			endway = targetnode
		} else {
			if tmpdist<currdist {
				curr = tmpnode
				currdist = tmpdist
				endway = targetnode
			}
		}
	}
	var start bool
	for start,startway = isInList(s, curr);!start;start,startway=isInList(s,curr) {
		path.PushFront(curr)
		curr = p[curr]
		edges.PushFront(ep[curr])
	}
	path.PushFront(curr)
	// TODO fix, t[0] is not necessarily optimal
	return currdist, path, edges,startway,endway
}
<commit_msg>fixed breaking condition<commit_after>package alg

import (
	"graph"
	"container/heap"
	"container/list"
)

func isInList(w []graph.Way, s graph.Node) (bool,graph.Way) {
	var resultway graph.Way
	for _, way := range w {
		if s == way.Node {
			return true,way
		}
	}
	return false,resultway
}

// A slightly optimized version of dijkstras algorithm
// Takes an graph as argument and returns an list of vertices in order
// of the path
func Dijkstra(s, t []graph.Way) (float64, *list.List, *list.List,graph.Way,graph.Way) {
	d := make(map[graph.Node]float64)                // I assume distance is an integer
	p := make(map[graph.Node]graph.Node)               // Predecessor list
	ep := make(map[graph.Node]graph.Edge) // Edge Predecessors
	q := NewPriorityQueue(100 /* initialCapacity */) // 100 is just a first guess
	final := make(map[graph.Node]bool)
	for _, tar := range t {
		final[tar.Node] = false
	}
	for _, str := range s {
		priority := str.Length
		x := NewElement(str.Node, priority) // TODO check this cast
		heap.Push(&q, x)
		d[str.Node] = priority
	}
	for !q.Empty() {
		currElem := (heap.Pop(&q)).(*Element) // Get the first element
		curr := currElem.Value.(graph.Node)            // Unbox the node
		if isfinal, _ := final[curr]; isfinal {                    
			final[curr] = true
			finished := true
			for _, node := range final {
				finished = finished && node
			}
			if finished {
				break
			}
		}
		currDist := d[curr]
		for _, e := range curr.Edges() {
			n := e.EndPoint()
			if dist, ok := d[n]; ok {
				if tmpDist := currDist + e.Length(); tmpDist < dist {
					q.ChangePriority(currElem, tmpDist) // TODO again check cast
					d[n] = tmpDist
					p[n] = curr
					ep[n]=e
				}
			} else {
				d[n] = currDist + e.Length()
				p[n] = curr
				ep[n]=e
				elem := NewElement(n, currDist)  // TODO again check cast
				heap.Push(&q, elem)
			}
		}
	}
	path := list.New()
	edges := list.New()
	// Construct the list by moving from t to s
	first := true
	var curr graph.Node
	var currdist float64
	var endway graph.Way
	var startway graph.Way
	for _, targetnode := range t {
		tmpnode := targetnode.Node
		tmpdist := d[tmpnode] + targetnode.Length
		if first {
			curr = tmpnode
			currdist = tmpdist
			endway = targetnode
			first = false
		} else {
			if tmpdist<currdist {
				curr = tmpnode
				currdist = tmpdist
				endway = targetnode
			}
		}
	}
	var start bool
	for start,startway = isInList(s, curr);!start;start,startway=isInList(s,curr) {
		path.PushFront(curr)
		curr = p[curr]
		edges.PushFront(ep[curr])
	}
	path.PushFront(curr)
	// TODO fix, t[0] is not necessarily optimal
	return currdist, path, edges,startway,endway
}

{'fraction_non_alphanumeric': 0.13407505185743918, 'fraction_numerical': 0.004902885159343767, 'mean_word_length': 2.7563739376770537, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3175833', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1873, 'n_words': 521}