// File: /workspace/github_cpp_makefile_star2_O2/AbraaoHonorio[P]Page-Replacement-Algorithms/PageReplacement.cpp
// Function: PageReplacement::lru()
void PageReplacement::lru(void)
{
	/* initializing */
	pageFaults_m = 0;
	myQueue.clear();


	/*inserting the value to queue*/
	for (int i = (sizeRam_m-1); i>=0; i--)
	{
	    if (std::find(myQueue.begin(), myQueue.end(), myPages[i]) != myQueue.end())
				;
        else
        {
            pageFaults_m++;
            myQueue.push_back(myPages[i]);
        }
	}

	// deleting Erase the first sizeRam_m elements
	myPages.erase(myPages.begin(), myPages.begin() + sizeRam_m);

	if (!myPages.empty() && !myQueue.empty()) //verify exist elemen t
	{
		for (unsigned int i = 0; i <myPages.size(); i++)
		{
				auto it = std::find(myQueue.begin(), myQueue.end(), myPages[i]);
				if (it != myQueue.end())
				{
					auto index = std::distance(myQueue.begin(), it); // calculating the index of the element
					myQueue.erase(myQueue.begin() + index); // deleting the element
					myQueue.insert(myQueue.begin(), myPages[i]); //Inserting the element at the top( highest priority )

				}
				else
				{
					myQueue.insert(myQueue.begin(), myPages[i]); //Inserting the element at the top( highest  priority )
					myQueue.erase(myQueue.end()-1); //delete to lower priority
					pageFaults_m++;
				}
		}
	}
	else
	{
		std::cout << " The Queue have empty or sizeRam is null " << std::endl;
	}

	std::cout << " LRU  " << pageFaults_m << " " << std::endl; //output
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
andl $0x0, 0x8(%rdi)
movq 0x10(%rdi), %rax
cmpq %rax, 0x18(%rdi)
je 0x1736
movq %rax, 0x18(%rbx)
leaq 0x10(%rbx), %r14
movl 0xc(%rbx), %r12d
leaq 0x28(%rbx), %r15
leaq -0x4(,%r12,4), %r13
testl %r12d, %r12d
jle 0x1784
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %r13, %rdx
callq 0x1ae3
cmpq 0x18(%rbx), %rax
jne 0x177b
incl 0x8(%rbx)
movq 0x28(%rbx), %rsi
addq %r13, %rsi
movq %r14, %rdi
callq 0x196a
decl %r12d
addq $-0x4, %r13
jmp 0x174a
movq 0x28(%rbx), %rsi
movslq 0xc(%rbx), %rax
leaq (%rsi,%rax,4), %rdx
movq %r15, %rdi
callq 0x19c2
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
cmpq %rcx, %rax
je 0x1842
movq 0x10(%rbx), %rdx
cmpq 0x18(%rbx), %rdx
je 0x1842
xorl %r12d, %r12d
movl %r12d, %r12d
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r12, %rcx
jbe 0x185d
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
leaq (%rax,%r12,4), %rdx
callq 0x1ae3
cmpq 0x18(%rbx), %rax
je 0x1807
movq %r14, %rdi
movq %rax, %rsi
callq 0x1988
movq (%r14), %rsi
leaq (,%r12,4), %rdx
addq (%r15), %rdx
movq %r14, %rdi
callq 0x1a6c
jmp 0x1832
movq 0x10(%rbx), %rsi
leaq (,%r12,4), %rdx
addq 0x28(%rbx), %rdx
movq %r14, %rdi
callq 0x1a6c
movq 0x18(%rbx), %rsi
addq $-0x4, %rsi
movq %r14, %rdi
callq 0x1988
incl 0x8(%rbx)
incl %r12d
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
jmp 0x17ba
movq 0x278f(%rip), %rdi      # 0x3fd8
leaq 0x7b4(%rip), %rsi       # 0x2004
callq 0x10f0
movq %rax, %rdi
callq 0x1090
movq 0x2774(%rip), %rdi      # 0x3fd8
leaq 0x7d3(%rip), %rsi       # 0x203e
callq 0x10f0
movl 0x8(%rbx), %esi
movq %rax, %rdi
callq 0x1140
leaq 0x7ba(%rip), %rsi       # 0x203c
movq %rax, %rdi
callq 0x10f0
movq %rax, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x1090
nop