Commit 43cc18c5 authored by Vicențiu Ciorbaru's avatar Vicențiu Ciorbaru Committed by Sergei Golubchik

cleanup: simplify Queue<>, add const

also add const to methods in List<> and Hash_set<>
while we're at it
parent d284d93f
...@@ -52,7 +52,7 @@ class Bounded_queue ...@@ -52,7 +52,7 @@ class Bounded_queue
uchar **m_sort_keys; uchar **m_sort_keys;
size_t m_compare_length; size_t m_compare_length;
Sort_param *m_sort_param; Sort_param *m_sort_param;
Queue<uchar*,uchar*,size_t> m_queue; Queue<uchar*, size_t> m_queue;
}; };
...@@ -68,7 +68,7 @@ int Bounded_queue::init(ha_rows max_elements, size_t cmplen, ...@@ -68,7 +68,7 @@ int Bounded_queue::init(ha_rows max_elements, size_t cmplen,
if (max_elements >= UINT_MAX - 1) if (max_elements >= UINT_MAX - 1)
return 1; return 1;
// We allocate space for one extra element, for replace when queue is full. // We allocate space for one extra element, for replace when queue is full.
return m_queue.init((uint)max_elements + 1, 0, true, return m_queue.init((uint)max_elements + 1, true,
(decltype(m_queue)::Queue_compare)get_ptr_compare(cmplen), (decltype(m_queue)::Queue_compare)get_ptr_compare(cmplen),
&m_compare_length); &m_compare_length);
} }
......
...@@ -6654,7 +6654,7 @@ subselect_rowid_merge_engine::init(MY_BITMAP *non_null_key_parts, ...@@ -6654,7 +6654,7 @@ subselect_rowid_merge_engine::init(MY_BITMAP *non_null_key_parts,
if (merge_keys[i]->sort_keys()) if (merge_keys[i]->sort_keys())
return TRUE; return TRUE;
if (pq.init(merge_keys_count, 0, false, if (pq.init(merge_keys_count, false,
subselect_rowid_merge_engine::cmp_keys_by_cur_rownum)) subselect_rowid_merge_engine::cmp_keys_by_cur_rownum))
return TRUE; return TRUE;
...@@ -6713,8 +6713,9 @@ subselect_rowid_merge_engine::cmp_keys_by_null_selectivity(Ordered_key **k1, ...@@ -6713,8 +6713,9 @@ subselect_rowid_merge_engine::cmp_keys_by_null_selectivity(Ordered_key **k1,
*/ */
int int
subselect_rowid_merge_engine::cmp_keys_by_cur_rownum(void *, Ordered_key *k1, subselect_rowid_merge_engine::cmp_keys_by_cur_rownum(void *,
Ordered_key *k2) const Ordered_key *k1,
const Ordered_key *k2)
{ {
rownum_t r1= k1->current(); rownum_t r1= k1->current();
rownum_t r2= k2->current(); rownum_t r2= k2->current();
......
...@@ -1368,7 +1368,7 @@ class Ordered_key : public Sql_alloc ...@@ -1368,7 +1368,7 @@ class Ordered_key : public Sql_alloc
return FALSE; return FALSE;
}; };
/* Return the current index element. */ /* Return the current index element. */
rownum_t current() rownum_t current() const
{ {
DBUG_ASSERT(key_buff_elements && cur_key_idx < key_buff_elements); DBUG_ASSERT(key_buff_elements && cur_key_idx < key_buff_elements);
return key_buff[cur_key_idx]; return key_buff[cur_key_idx];
...@@ -1507,7 +1507,7 @@ class subselect_rowid_merge_engine: public subselect_partial_match_engine ...@@ -1507,7 +1507,7 @@ class subselect_rowid_merge_engine: public subselect_partial_match_engine
Priority queue of Ordered_key indexes, one per NULLable column. Priority queue of Ordered_key indexes, one per NULLable column.
This queue is used by the partial match algorithm in method exec(). This queue is used by the partial match algorithm in method exec().
*/ */
Queue<Ordered_key, Ordered_key> pq; Queue<Ordered_key> pq;
protected: protected:
/* /*
Comparison function to compare keys in order of decreasing bitmap Comparison function to compare keys in order of decreasing bitmap
...@@ -1518,7 +1518,9 @@ class subselect_rowid_merge_engine: public subselect_partial_match_engine ...@@ -1518,7 +1518,9 @@ class subselect_rowid_merge_engine: public subselect_partial_match_engine
Comparison function used by the priority queue pq, the 'smaller' key Comparison function used by the priority queue pq, the 'smaller' key
is the one with the smaller current row number. is the one with the smaller current row number.
*/ */
static int cmp_keys_by_cur_rownum(void *arg, Ordered_key *k1, Ordered_key *k2); static int cmp_keys_by_cur_rownum(void *arg,
const Ordered_key *k1,
const Ordered_key *k2);
bool test_null_row(rownum_t row_num); bool test_null_row(rownum_t row_num);
bool exists_complementing_null_row(MY_BITMAP *keys_to_complement); bool exists_complementing_null_row(MY_BITMAP *keys_to_complement);
......
...@@ -62,18 +62,28 @@ class Hash_set ...@@ -62,18 +62,28 @@ class Hash_set
@retval FALSE OK. The value either was inserted or existed @retval FALSE OK. The value either was inserted or existed
in the hash. in the hash.
*/ */
bool insert(T *value) bool insert(const T *value)
{ {
return my_hash_insert(&m_hash, reinterpret_cast<const uchar*>(value)); return my_hash_insert(&m_hash, reinterpret_cast<const uchar*>(value));
} }
bool remove(T *value) bool remove(const T *value)
{ {
return my_hash_delete(&m_hash, reinterpret_cast<uchar*>(value)); return my_hash_delete(&m_hash,
reinterpret_cast<uchar*>(const_cast<T*>(value)));
} }
T *find(const void *key, size_t klen) const T *find(const void *key, size_t klen) const
{ {
return (T*)my_hash_search(&m_hash, reinterpret_cast<const uchar *>(key), klen); return (T*)my_hash_search(&m_hash, reinterpret_cast<const uchar *>(key), klen);
} }
T *find(const T *other) const
{
DBUG_ASSERT(m_hash.get_key);
size_t klen;
uchar *key= m_hash.get_key(reinterpret_cast<const uchar *>(other),
&klen, false);
return find(key, klen);
}
/** Is this hash set empty? */ /** Is this hash set empty? */
bool is_empty() const { return m_hash.records == 0; } bool is_empty() const { return m_hash.records == 0; }
/** Returns the number of unique elements. */ /** Returns the number of unique elements. */
......
...@@ -496,8 +496,8 @@ template <class T> class List :public base_list ...@@ -496,8 +496,8 @@ template <class T> class List :public base_list
inline List() :base_list() {} inline List() :base_list() {}
inline List(const List<T> &tmp, MEM_ROOT *mem_root) : inline List(const List<T> &tmp, MEM_ROOT *mem_root) :
base_list(tmp, mem_root) {} base_list(tmp, mem_root) {}
inline bool push_back(T *a) { return base_list::push_back(a); } inline bool push_back(const T *a) { return base_list::push_back((void *)a); }
inline bool push_back(T *a, MEM_ROOT *mem_root) inline bool push_back(const T *a, MEM_ROOT *mem_root)
{ return base_list::push_back((void*) a, mem_root); } { return base_list::push_back((void*) a, mem_root); }
inline bool push_front(const T *a) { return base_list::push_front(a); } inline bool push_front(const T *a) { return base_list::push_front(a); }
inline bool push_front(const T *a, MEM_ROOT *mem_root) inline bool push_front(const T *a, MEM_ROOT *mem_root)
......
...@@ -16,24 +16,25 @@ ...@@ -16,24 +16,25 @@
#ifndef QUEUE_INCLUDED #ifndef QUEUE_INCLUDED
#define QUEUE_INCLUDED #define QUEUE_INCLUDED
#include <my_global.h>
#include "queues.h" #include "queues.h"
/** /**
A typesafe wrapper of QUEUE, a priority heap A typesafe wrapper of QUEUE, a priority heap
*/ */
template<typename Element, typename Key, typename Param=void> template<typename Element, typename Param=void>
class Queue class Queue
{ {
public: public:
typedef int (*Queue_compare)(Param *, Key *, Key *); typedef int (*Queue_compare)(Param *, const Element *, const Element *);
Queue() { m_queue.root= 0; } Queue() { m_queue.root= 0; }
~Queue() { delete_queue(&m_queue); } ~Queue() { delete_queue(&m_queue); }
int init(uint max_elements, uint offset_to_key, bool max_at_top, int init(uint max_elements, bool max_at_top, Queue_compare compare,
Queue_compare compare, Param *param= 0) Param *param= 0)
{ {
return init_queue(&m_queue, max_elements, offset_to_key, max_at_top, return init_queue(&m_queue, max_elements, 0, max_at_top,
(queue_compare)compare, param, 0, 0); (queue_compare)compare, (void *)param, 0, 0);
} }
size_t elements() const { return m_queue.elements; } size_t elements() const { return m_queue.elements; }
...@@ -42,11 +43,11 @@ class Queue ...@@ -42,11 +43,11 @@ class Queue
bool is_empty() const { return elements() == 0; } bool is_empty() const { return elements() == 0; }
Element *top() const { return (Element*)queue_top(&m_queue); } Element *top() const { return (Element*)queue_top(&m_queue); }
void push(Element *element) { queue_insert(&m_queue, (uchar*)element); } void push(const Element *element) { queue_insert(&m_queue, (uchar*)element); }
Element *pop() { return (Element *)queue_remove_top(&m_queue); } Element *pop() { return (Element *)queue_remove_top(&m_queue); }
void clear() { queue_remove_all(&m_queue); } void clear() { queue_remove_all(&m_queue); }
void propagate_top() { queue_replace_top(&m_queue); } void propagate_top() { queue_replace_top(&m_queue); }
void replace_top(Element *element) void replace_top(const Element *element)
{ {
queue_top(&m_queue)= (uchar*)element; queue_top(&m_queue)= (uchar*)element;
propagate_top(); propagate_top();
......
...@@ -78,21 +78,21 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo) ...@@ -78,21 +78,21 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo)
return err == HA_ERR_END_OF_FILE ? 0 : err; return err == HA_ERR_END_OF_FILE ? 0 : err;
} }
static int cmp_float(void *, float *a, float *b)
{
return *a < *b ? -1 : *a == *b ? 0 : 1;
}
struct Node struct Node
{ {
float distance; float distance;
uchar ref[1000]; uchar ref[1000];
}; };
static int cmp_float(void *, const Node *a, const Node *b)
{
return a->distance < b->distance ? -1 : a->distance == b->distance ? 0 : 1;
}
int mhnsw_first(TABLE *table, Item *dist, ulonglong limit) int mhnsw_first(TABLE *table, Item *dist, ulonglong limit)
{ {
TABLE *graph= table->hlindex; TABLE *graph= table->hlindex;
Queue<Node, float> todo, result; Queue<Node> todo, result;
Node *cur; Node *cur;
String *str, strbuf; String *str, strbuf;
const size_t ref_length= table->file->ref_length; const size_t ref_length= table->file->ref_length;
...@@ -105,10 +105,10 @@ int mhnsw_first(TABLE *table, Item *dist, ulonglong limit) ...@@ -105,10 +105,10 @@ int mhnsw_first(TABLE *table, Item *dist, ulonglong limit)
DBUG_ASSERT(graph); DBUG_ASSERT(graph);
if (todo.init(1000, 0, 0, cmp_float)) // XXX + autoextent if (todo.init(1000, 0, cmp_float)) // XXX + autoextent
return HA_ERR_OUT_OF_MEM; return HA_ERR_OUT_OF_MEM;
if (result.init(limit, 0, 1, cmp_float)) if (result.init(limit, 1, cmp_float))
return HA_ERR_OUT_OF_MEM; return HA_ERR_OUT_OF_MEM;
if ((err= graph->file->ha_index_init(0, 1))) if ((err= graph->file->ha_index_init(0, 1)))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment