// File: /workspace/github_cpp_makefile_star2_O2/Windfisch[P]production-flow/actiongraph.cpp
// Function: ActionGraph::dijkstra(Factory::FactoryConfiguration)
pair<Factory::FactoryConfiguration, double> ActionGraph::dijkstra(Factory::FactoryConfiguration initial_config)
{
	auto start_node = make_unique<ActionGraph::Node>();
	start_node->conf = initial_config;
	start_node->current_item_type = item_t(MAX_ITEM-1);
	start_node->total_cost = 0.;

	vector< unique_ptr< ActionGraph::Node> > openlist;
	vector< unique_ptr< ActionGraph::Node> > closedlist;
	openlist.push_back(move(start_node));

	while (!openlist.empty())
	{
		cout << "openlist has size " << openlist.size() << ", total expanded = " << openlist.size() + closedlist.size() << endl;

		// find and remove smallest element
		auto smallest = openlist.begin();
		for (auto it = openlist.begin(); it != openlist.end(); it++)
			if ((*it)->total_cost < (*smallest)->total_cost)
				smallest=it;
		
		closedlist.push_back(move(*smallest));
		auto& nodeptr = closedlist.back();
		*smallest = move(openlist.back());
		openlist.pop_back();

		cout << "inspecting item: " << nodeptr->current_item_type << ", ";
		cout << "nodes:";
		for (auto lvl : nodeptr->conf.facility_levels)
			cout << " " << lvl;
		cout << ", edges:";
		for (auto lvl : nodeptr->conf.transport_levels)
			cout << " " << lvl;
		cout << endl;


		// expand node
		auto successor_nodes = nodeptr->successors(factory);
		for (auto& successor : successor_nodes)
		{
			cout << "  -> successor item: " << successor->current_item_type << ", ";
			cout << "nodes:";
			for (auto lvl : successor->conf.facility_levels)
				cout << " " << lvl;
			cout << ", edges:";
			for (auto lvl : successor->conf.transport_levels)
				cout << " " << lvl;
			if (successor->current_item_type == DONE)
			{
				// we've found a goal state! :)
				cout << endl << "success, cost = " << successor->total_cost << ", expanded " << closedlist.size() + openlist.size() << " nodes" << endl;
				return pair<Factory::FactoryConfiguration, double>(successor->conf, successor->total_cost);
			}

			bool found = false;
			for (auto& openlist_node : openlist)
				if (openlist_node->equals(*successor, factory))
				{
					openlist_node = move(successor);
					cout << "; already in openlist" << endl;
					found = true;
					break;
				}
			if (!found)
				for (auto& closedlist_node : closedlist)
					if (closedlist_node->equals(*successor, factory))
					{
						found = true;
						cout << "; already in closedlist" << endl;
						break;
					}

			if (!found)
			{
				openlist.push_back(move(successor));
				cout << "; not seen yet, adding to openlist" << endl;
			}
		}
	}

	cout << "could not find a solution :(" << endl;
	return pair<Factory::FactoryConfiguration, double>(initial_config, -1.);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r15
movq %rsi, 0x20(%rsp)
movq %rdi, %r14
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x91a3
movq (%rbx), %rdi
movq %r15, 0x58(%rsp)
movq %r15, %rsi
callq 0x91ca
leaq 0x50(%rsp), %rsi
movq (%rsi), %rax
movl $0xa, 0x30(%rax)
andq $0x0, 0x38(%rax)
xorpd %xmm0, %xmm0
movq %rsp, %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
callq 0x910e
movq 0xc3c4(%rip), %r12      # 0x14fc8
leaq 0x4c12(%rip), %rsi      # 0xd81d
leaq 0x49e0(%rip), %rbx      # 0xd5f2
movq 0x20(%rsp), %r15
movq %r14, 0x48(%rsp)
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x903e
movq %r12, %rdi
callq 0x3200
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x3180
movq %rax, %rdi
leaq 0x4bde(%rip), %rsi      # 0xd830
callq 0x3200
movq 0x8(%rsp), %rcx
subq (%rsp), %rcx
sarq $0x3, %rcx
movq 0x38(%rsp), %rsi
subq 0x30(%rsp), %rsi
sarq $0x3, %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x3180
movq %rax, %rdi
callq 0x3110
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rax, %r13
cmpq %rcx, %rax
je 0x8cb8
movq (%rax), %rdx
movq (%r13), %rsi
movsd 0x38(%rsi), %xmm0
ucomisd 0x38(%rdx), %xmm0
movq %rax, %rdx
ja 0x8caf
movq %r13, %rdx
addq $0x8, %rax
movq %rdx, %r13
jmp 0x8c91
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x910e
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %rsi
addq $-0x8, %rsi
movq %r13, %rdi
callq 0x9312
movq 0x8(%rsp), %rdi
addq $-0x8, %rdi
movq %rdi, 0x8(%rsp)
callq 0x914a
movq %r12, %rdi
leaq 0x4b4c(%rip), %rsi      # 0xd844
callq 0x3200
movq -0x8(%r14), %rcx
movl 0x30(%rcx), %esi
movq %rax, %rdi
callq 0x33b0
movq %rax, %rdi
leaq 0x48db(%rip), %rsi      # 0xd5f1
callq 0x3200
movq %r12, %rdi
leaq 0x4b31(%rip), %rsi      # 0xd856
callq 0x3200
movq %r14, 0x28(%rsp)
movq -0x8(%r14), %rax
movq (%rax), %rbp
movq 0x8(%rax), %r14
cmpq %r14, %rbp
je 0x8d5f
movq (%rbp), %r13
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3200
movq %rax, %rdi
movq %r13, %rsi
callq 0x3180
addq $0x8, %rbp
jmp 0x8d3a
movq %r12, %rdi
leaq 0x4af4(%rip), %rsi      # 0xd85d
callq 0x3200
movq 0x28(%rsp), %rax
movq -0x8(%rax), %rax
movq 0x18(%rax), %rbp
movq 0x20(%rax), %r14
cmpq %r14, %rbp
je 0x8da4
movq (%rbp), %r13
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3200
movq %rax, %rdi
movq %r13, %rsi
callq 0x3180
addq $0x8, %rbp
jmp 0x8d7f
movq %r12, %rdi
callq 0x3110
movq 0x28(%rsp), %rax
movq -0x8(%rax), %rsi
movq (%r15), %rdx
leaq 0x60(%rsp), %rdi
callq 0x8772
movq 0x60(%rsp), %r13
movq 0x68(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %r14
cmpq 0x28(%rsp), %r13
je 0x8f63
movq %r12, %rdi
leaq 0x4a7b(%rip), %rsi      # 0xd866
callq 0x3200
movq (%r13), %rcx
movl 0x30(%rcx), %esi
movq %rax, %rdi
callq 0x33b0
movq %rax, %rdi
leaq 0x47e8(%rip), %rsi      # 0xd5f1
callq 0x3200
movq %r12, %rdi
leaq 0x4a3e(%rip), %rsi      # 0xd856
callq 0x3200
movq (%r13), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r15, %r14
je 0x8e4c
movq (%r14), %rbp
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3200
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3180
addq $0x8, %r14
jmp 0x8e28
movq %r12, %rdi
leaq 0x4a07(%rip), %rsi      # 0xd85d
callq 0x3200
movq (%r13), %rax
movq 0x18(%rax), %r14
movq 0x20(%rax), %r15
cmpq %r15, %r14
je 0x8e8b
movq (%r14), %rbp
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3200
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3180
addq $0x8, %r14
jmp 0x8e67
movq (%r13), %rax
cmpl $-0x1, 0x30(%rax)
je 0x8f79
movq (%rsp), %rbp
movq 0x8(%rsp), %r14
movq 0x20(%rsp), %r15
cmpq %r14, %rbp
je 0x8ec6
movq (%rbp), %rdi
movq (%r13), %rsi
movq (%r15), %rdx
callq 0x8630
testb %al, %al
jne 0x8f0f
addq $0x8, %rbp
jmp 0x8ea7
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r15
cmpq %r15, %r14
je 0x8f33
movq (%r14), %rdi
movq (%r13), %rsi
movq 0x20(%rsp), %rax
movq (%rax), %rdx
callq 0x8630
addq $0x8, %r14
testb %al, %al
je 0x8ed0
movq %r12, %rdi
leaq 0x49bb(%rip), %rsi      # 0xd8b6
callq 0x3200
movq 0x20(%rsp), %r15
movq %rax, %rdi
callq 0x3110
jmp 0x8f5a
movq %rbp, %rdi
movq %r13, %rsi
callq 0x9312
movq %r12, %rdi
leaq 0x497c(%rip), %rsi      # 0xd8a0
callq 0x3200
movq %rax, %rdi
callq 0x3110
jmp 0x8f5a
movq %rsp, %rdi
movq %r13, %rsi
callq 0x910e
movq 0x20(%rsp), %r15
movq %r12, %rdi
leaq 0x4981(%rip), %rsi      # 0xd8ce
callq 0x3200
movq %rax, %rdi
callq 0x3110
addq $0x8, %r13
jmp 0x8dd1
leaq 0x60(%rsp), %rdi
callq 0x9182
leaq 0x48a9(%rip), %rsi      # 0xd81d
jmp 0x8c1c
movq 0xc048(%rip), %rdi      # 0x14fc8
callq 0x3110
movq 0x48(%rsp), %r14
leaq 0x48eb(%rip), %rsi      # 0xd87c
movq %rax, %rdi
callq 0x3200
movq (%r13), %rcx
movsd 0x38(%rcx), %xmm0
movq %rax, %rdi
callq 0x3340
leaq 0x48dc(%rip), %rsi      # 0xd88d
movq %rax, %rdi
callq 0x3200
movq 0x38(%rsp), %rcx
subq 0x30(%rsp), %rcx
sarq $0x3, %rcx
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x3180
leaq 0x48b3(%rip), %rsi      # 0xd899
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x3110
movq (%r13), %rsi
leaq 0x38(%rsi), %rdx
movq %r14, %rdi
callq 0x91f4
leaq 0x60(%rsp), %rdi
callq 0x9182
leaq 0x30(%rsp), %rdi
callq 0x9182
movq %rsp, %rdi
callq 0x9182
leaq 0x50(%rsp), %rdi
callq 0x914a
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xbf83(%rip), %rdi      # 0x14fc8
leaq 0x48a5(%rip), %rsi      # 0xd8f1
callq 0x3200
movq %rax, %rdi
callq 0x3110
movq %r14, %rdi
movq 0x58(%rsp), %rsi
callq 0x4672
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, 0x30(%r14)
jmp 0x9010
jmp 0x9091
movq %rax, %rbx
jmp 0x90b9
jmp 0x9091
jmp 0x9091
jmp 0x9091
jmp 0x909a
jmp 0x9091
jmp 0x9091
jmp 0x909a
jmp 0x909a
jmp 0x909a
jmp 0x9091
movq %rax, %rbx
jmp 0x90a7
jmp 0x909a
jmp 0x909a
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x9182
leaq 0x30(%rsp), %rdi
callq 0x9182
movq %rsp, %rdi
callq 0x9182
leaq 0x50(%rsp), %rdi
callq 0x914a
movq %rbx, %rdi
callq 0x33c0