source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Heap.h @ 1600

Revision 1600, 1.6 KB checked in by gumbau, 18 years ago (diff)

Fixed simplification bug when simplifying to extremely a low factor

Line 
1#ifndef GFXTOOLS_HEAP_INCLUDED // -*- C++ -*-
2#define GFXTOOLS_HEAP_INCLUDED
3
4#include <gfx/tools/Array.h>
5
6#define NOT_IN_HEAP -47
7
8//
9//
10// This file extracted from Terra
11//
12//
13
14namespace simplif
15{
16        class Heapable
17        {
18                private:
19
20                        int token;
21
22                public:
23
24                        Heapable() { notInHeap(); }
25
26                        inline int isInHeap() { return token!=NOT_IN_HEAP; }
27                        inline void notInHeap() { token = NOT_IN_HEAP; }
28                        inline int getHeapPos() { return token; }
29                        inline void setHeapPos(int t) { token=t; }
30        };
31
32        class heap_node
33        {
34                public:
35                        float import;
36                        Heapable *obj;
37
38                        heap_node() { obj=NULL; import=0.0; }
39                        heap_node(Heapable *t, float i=0.0) { obj=t; import=i; }
40                        heap_node(const heap_node& h) { import=h.import; obj=h.obj; }
41        };
42
43        class Heap : public array<heap_node>
44        {
45                //
46                // The actual size of the heap.  array::length()
47                // simply returns the amount of allocated space
48                int size;
49
50                void swap(int i, int j);
51
52                int parent(int i) { return (i-1)/2; }
53                int left(int i) { return 2*i+1; }
54                int right(int i) { return 2*i+2; }
55
56                void upheap(int i);
57                void downheap(int i);
58
59                public:
60
61                Heap() { size=0; }
62                Heap(int s) : array<heap_node>(s) { size=0; }
63
64                void insert(Heapable *, float);
65                void update(Heapable *, float);
66
67                heap_node *extract();
68
69                heap_node *top() { return size<1 ? (heap_node *)NULL : &ref(0); }
70                heap_node *kill(int i);
71
72                //      Gets heap size.
73                int     getSize(){return        size;}
74
75                //      Gets element at position i.
76                heap_node       *getElement(int i){return       &ref(i);}
77
78        };
79
80}
81
82// GFXTOOLS_HEAP_INCLUDED
83#endif
84
Note: See TracBrowser for help on using the repository browser.