1 #ifndef JAWS_CACHE_HEAP_T_CPP
2 #define JAWS_CACHE_HEAP_T_CPP
4 #include "JAWS/Cache_Heap_T.h"
5 #include "JAWS/Cache_Manager_T.h"
7 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
>
8 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::JAWS_Cache_Heap (ACE_Allocator
*alloc
,
14 if (this->allocator_
== 0)
15 this->allocator_
= ACE_Allocator::instance ();
18 = this->maxsize_
* sizeof (Cache_Heap_Item
*);
20 this->heap_
= (Cache_Heap_Item
**) this->allocator_
->malloc (memsize
);
23 for (size_t i
= 0; i
< this->maxsize_
; i
++)
29 // should indicate something
33 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
>
34 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::~JAWS_Cache_Heap (void)
38 for (size_t i
= 0; i
< this->maxsize_
; i
++)
42 ACE_DES_FREE_TEMPLATE4(this->heap_
[i
], this->allocator_
->free
,
44 EXT_ID
, FACT
, H_FN
, E_FN
);
49 this->allocator_
->free (this->heap_
);
56 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> int
57 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::is_empty (void) const
59 return (this->size_
== 0);
62 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> int
63 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::is_full (void) const
65 return (this->size_
== this->maxsize_
);
68 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> size_t
69 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::size (void) const
74 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> size_t
75 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::maxsize (void) const
77 return this->maxsize_
;
80 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> int
81 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::maxsize (Cache_Manager
*cm
,
87 = new_maxsize
* sizeof (Cache_Heap_Item
*);
89 Cache_Heap_Item
**new_heap
90 = (Cache_Heap_Item
**) this->allocator_
->malloc (memsize
);
93 while (new_maxsize
< this->size_
)
96 for (size_t i
= 0; i
< new_maxsize
; i
++)
98 new_heap
[i
] = this->heap_
[i
];
102 Cache_Heap_Item
** volatile temp
= this->heap_
;
103 this->heap_
= new_heap
;
104 this->maxsize_
= new_maxsize
;
105 this->allocator_
->free (temp
);
112 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> void
113 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::insert_i (Cache_Heap_Item
*item
)
115 /* ASSERT: this->size_ < this->maxsize_ */
119 for (i
= this->size_
+ 1; i
> 1; i
/= 2)
121 if (item
->priority () > this->heap_
[i
/2 - 1]->priority ())
124 this->heap_
[i
-1] = this->heap_
[i
/2 - 1];
125 this->heap_
[i
-1]->heap_idx_
= i
-1;
128 this->heap_
[i
-1] = item
;
129 this->heap_
[i
-1]->heap_idx_
= i
-1;
133 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> int
134 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::insert (const EXT_ID
&ext_id
,
135 JAWS_Cache_Object
*const &int_id
)
137 if (this->is_full ())
140 Cache_Heap_Item
*item
;
141 ACE_NEW_MALLOC_RETURN (item
,
143 this->allocator_
->malloc (sizeof (Cache_Heap_Item
)),
144 Cache_Heap_Item (ext_id
, int_id
), -1);
146 this->insert_i (item
);
151 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> void
152 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::remove_i (void)
154 /* ASSERT: this->size_ > 0 */
156 Cache_Heap_Item
*temp
= this->heap_
[this->size_
];
157 this->heap_
[this->size_
] = 0;
160 while (2*i
<= this->size_
)
163 if ((child
< this->size_
)
164 && (this->heap_
[2*i
]->priority ()
165 < this->heap_
[2*i
- 1]->priority ()))
168 if (temp
->priority () < this->heap_
[child
-1]->priority ())
171 this->heap_
[i
-1] = this->heap_
[child
-1];
172 this->heap_
[i
-1]->heap_idx_
= i
-1;
178 this->heap_
[i
-1] = temp
;
179 this->heap_
[i
-1]->heap_idx_
= i
-1;
183 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> void
184 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::remove_i (size_t pos
)
186 Cache_Heap_Item
*item
= this->heap_
[pos
];
193 this->heap_
[i
-1] = this->heap_
[i
/2 - 1];
194 this->heap_
[i
-1]->heap_idx_
= i
-1;
200 this->heap_
[0] = item
;
205 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> int
206 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::remove (EXT_ID
&ext_id
,
207 JAWS_Cache_Object
*&int_id
)
209 if (this->is_empty ())
212 Cache_Heap_Item
*item
= this->heap_
[0];
213 item
->int_id_
->heap_item (0);
217 ext_id
= item
->ext_id_
;
218 int_id
= item
->int_id_
;
220 ACE_DES_FREE_TEMPLATE4(item
, this->allocator_
->free
,
221 JAWS_Cache_Heap_Item
,
222 EXT_ID
, FACT
, H_FN
, E_FN
);
228 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> int
229 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::remove (void *item
)
234 Cache_Heap_Item
*real_item
= (Cache_Heap_Item
*) item
;
236 // Make sure the item is where it thinks it is.
237 if (this->heap_
[real_item
->heap_idx_
] != real_item
)
240 real_item
->int_id_
->heap_item (0);
241 this->remove_i (real_item
->heap_idx_
);
243 ACE_DES_FREE_TEMPLATE4(real_item
, this->allocator_
->free
,
244 JAWS_Cache_Heap_Item
,
245 EXT_ID
, FACT
, H_FN
, E_FN
);
252 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> int
253 JAWS_Cache_Heap
<EXT_ID
,FACT
,H_FN
,E_FN
>::adjust (void *item
)
258 Cache_Heap_Item
*real_item
= (Cache_Heap_Item
*) item
;
260 // Make sure the item is where it thinks it is.
261 if (this->heap_
[real_item
->heap_idx_
] != real_item
)
264 this->remove_i (real_item
->heap_idx_
);
265 this->insert_i (real_item
);
271 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
>
272 JAWS_Cache_Heap_Item
<EXT_ID
,FACT
,H_FN
,E_FN
>::
273 JAWS_Cache_Heap_Item (const EXT_ID
&ext_id
, JAWS_Cache_Object
*const &int_id
)
278 this->int_id_
->heap_item (this);
281 template <class EXT_ID
, class FACT
, class H_FN
, class E_FN
> unsigned int
282 JAWS_Cache_Heap_Item
<EXT_ID
,FACT
,H_FN
,E_FN
>::priority (void)
284 return this->int_id_
->priority ();
288 #endif /* JAWS_CACHE_HEAP_T_CPP */