// File: /workspace/github_cpp_makefile_star2_O2/Slexis-Works[P]texture-packing/algoBFDH.cpp
// Function: algoBFDH(std::vector<Rectangle, std::allocator<Rectangle>> const&, std::vector<Bin, std::allocator<Bin>>&, unsigned int, unsigned int)
void algoBFDH(const std::vector<Rectangle> &rects, std::vector<Bin> &bins, coord bw, coord by) {
	// Tri des données
	std::vector<Rectangle> sortedRects;
	for (const Rectangle &rect : rects) {
		sortedRects.push_back(rect);
	}
	std::sort(sortedRects.begin(), sortedRects.end(), goesBeforeByHeight);

	// Création de tous les étages
	std::vector<Shelf> shelves;

	// Insertion du premier rectangle afin d'initialiser un premier étage
	shelves.push_back(Shelf(bw, 0, sortedRects[0]));

	int i = 0;
	int currSize = 0;
	int minShelf = 0;
	int minShelfSize = std::numeric_limits<int>::max();

	// On cherche, pour chaque élément, l'étage qui laissera le moins d'espace horizontal lorsqu'on l'ajoutera
	for (const Rectangle &rect : sortedRects) {
		for (auto & shelf : shelves) {
			// On calcul l'espace horizontal restant de l'étage courant après insertion de l'élément
			currSize = shelf.getEmptySpace() - rect.getW();

			// Si l'espace horizontal est insuffisant, on avance dans le for
			if (currSize < 0) {
				i++;
				continue;
			}

			// Sinon, on regarde si l'espace de l'étage courant et inférieur à l'espace trouvé plus tôt dans la boucle
			if (minShelfSize > currSize) {
				minShelf = i;
				minShelfSize = currSize;
			}

			i++;
		}

		// Si pas de place, on insère un nouvel étage
		if (shelves.at(minShelf).getEmptySpace() < rect.getW()) {
			shelves.push_back(Shelf(bw, 0, rect));
		}
		// Sinon insertion de l'élément
		else {
			shelves.at(minShelf).putRect(rect);
		}

		// Réinitialisation de variable
		i = 0;
		minShelf = 0;
		minShelfSize = std::numeric_limits<int>::max();
	}

	// Insertion de tous les étages de manière naïve
	coord curY = by;
	for (auto & shelf : shelves) {
		if (curY + shelf.getHeight() > by) {
			bins.push_back(Bin(bw, by));
			curY = 0;
		}
		shelf.setY(curY);
		curY += shelf.getHeight();
		bins.back().importShelf(shelf);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, %ebx
movl %edx, 0x4(%rsp)
movq %rsi, 0x8(%rsp)
leaq 0x30(%rsp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
movq (%rdi), %r12
movq 0x8(%rdi), %r14
cmpq %r14, %r12
je 0x4773
movq %r15, %rdi
movq %r12, %rsi
callq 0x3998
addq $0x14, %r12
jmp 0x475d
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq -0xdf8(%rip), %rdx      # 0x398c
callq 0x3a92
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x30(%rsp), %rcx
leaq 0x50(%rsp), %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
callq 0x2e3c
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x33ac
leaq 0x60(%rsp), %rdi
callq 0x2774
movq 0x30(%rsp), %r12
movq 0x38(%rsp), %rbp
leaq 0x10(%rsp), %r15
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r14
cmpq %rbp, %r12
je 0x488a
xorl %eax, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
cmpq %r14, %r13
je 0x4823
movl 0x8(%r13), %esi
movl 0xc(%r13), %edi
addl 0x8(%r12), %esi
subl %esi, %edi
cmpl %edi, %ecx
movl %edi, %esi
cmovll %ecx, %esi
movl %eax, %r8d
cmovgl %edx, %r8d
testl %edi, %edi
cmovnsl %esi, %ecx
cmovnsl %r8d, %eax
incl %edx
addq $0x28, %r13
jmp 0x47f0
movslq %eax, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x46e4
leaq 0x50(%rsp), %r14
movl 0xc(%rax), %ecx
subl 0x8(%rax), %ecx
cmpl 0x8(%r12), %ecx
jae 0x486b
movq %r14, %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
movq %r12, %rcx
callq 0x2e3c
movq %r15, %rdi
movq %r14, %rsi
callq 0x33ac
leaq 0x60(%rsp), %rdi
callq 0x2774
jmp 0x4881
movq %r15, %rdi
movq %r13, %rsi
callq 0x46e4
movq %rax, %rdi
movq %r12, %rsi
callq 0x2ea0
addq $0x14, %r12
jmp 0x47d4
leaq 0x50(%rsp), %r12
movl %ebx, %ebp
cmpq %r14, %r13
je 0x48f3
movl (%r13), %eax
addl %ebp, %eax
cmpl %ebx, %eax
jbe 0x48c7
movq %r12, %rdi
movl 0x4(%rsp), %esi
movl %ebx, %edx
callq 0x3140
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x35e0
leaq 0x58(%rsp), %rdi
callq 0x2774
xorl %ebp, %ebp
movq %r13, %rdi
movl %ebp, %esi
callq 0x2f4e
movl (%r13), %r15d
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
addq $-0x20, %rdi
movq %r13, %rsi
callq 0x3180
addl %r15d, %ebp
addq $0x28, %r13
jmp 0x4891
leaq 0x10(%rsp), %rdi
callq 0x3328
leaq 0x30(%rsp), %rdi
callq 0x2774
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x492a
jmp 0x4940
jmp 0x4939
jmp 0x4940
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0x4932
jmp 0x4940
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x2774
jmp 0x4943
movq %rax, %rbx
jmp 0x494d
jmp 0x4940
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3328
leaq 0x30(%rsp), %rdi
callq 0x2774
movq %rbx, %rdi
callq 0x21e0