summaryrefslogtreecommitdiff
path: root/Source/Library/Common/Heap-index.class.cpp
blob: 32807360390a43211cbea2bea0b0e184c095a73a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "Heap.class.h"

/*
 * Implementation of the functions for managing heap index
 */

void Heap::insertIntoIndex(heap_header_t *e) {
	//If index is full, return
	if ((m_index.size * sizeof(heap_header_t*) + (u32int)m_index.data) >= m_start) return;

	u32int iterator = 0;
	while (iterator < m_index.size && m_index.data[iterator]->size < e->size) {
		if (m_index.data[iterator] == e) return;
		iterator++;
	}
	if (iterator == m_index.size) {
		m_index.data[m_index.size++] = e;
	} else {
		u32int pos = iterator;
		iterator = m_index.size;
		while (iterator > pos) {
			m_index.data[iterator] = m_index.data[iterator - 1];
			iterator--;
		}
		m_index.size++;
		m_index.data[pos] = e;
	}
}

u32int Heap::findIndexEntry(heap_header_t *e) {
	for (u32int  i = 0; i < m_index.size; i++) {
		if (m_index.data[i] == e)
			return i;
	}
	return (u32int) - 1;
}

void Heap::removeFromIndex(u32int idx) {
	m_index.size--;
	while (idx < m_index.size) {
		m_index.data[idx] = m_index.data[idx + 1];
		idx++;
	}
}

void Heap::removeFromIndex(heap_header_t *e) {
	u32int i = findIndexEntry(e);
	if (i != (u32int) - 1) {
		removeFromIndex(i);
	}
}