Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ lib/ts/test_Map
lib/ts/test_Vec
lib/ts/test_geometry
lib/ts/test_Regex
lib/ts/test_PriorityQueue
lib/ts/test_X509HostnameValidator
lib/perl/lib/Apache/TS.pm

Expand Down
7 changes: 6 additions & 1 deletion lib/ts/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ library_includedir=$(includedir)/ts
library_include_HEADERS = apidefs.h

noinst_PROGRAMS = mkdfa CompileParseRules
check_PROGRAMS = test_arena test_atomic test_freelist test_geometry test_List test_Map test_Regex test_Vec test_X509HostnameValidator
check_PROGRAMS = test_arena test_atomic test_freelist test_geometry test_List test_Map test_Regex test_PriorityQueue test_Vec test_X509HostnameValidator
TESTS = $(check_PROGRAMS)

AM_CPPFLAGS = -I$(top_srcdir)/lib
Expand Down Expand Up @@ -84,6 +84,7 @@ libtsutil_la_SOURCES = \
MatcherUtils.h \
ParseRules.cc \
ParseRules.h \
PriorityQueue.h \
Ptr.h \
RawHashTable.cc \
RawHashTable.h \
Expand Down Expand Up @@ -216,6 +217,10 @@ test_Regex_SOURCES = test_Regex.cc
test_Regex_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@
test_Regex_LDFLAGS = @EXTRA_CXX_LDFLAGS@ @LIBTOOL_LINK_FLAGS@

test_PriorityQueue_SOURCES = test_PriorityQueue.cc
test_PriorityQueue_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@
test_PriorityQueue_LDFLAGS = @EXTRA_CXX_LDFLAGS@ @LIBTOOL_LINK_FLAGS@

test_Vec_SOURCES = test_Vec.cc
test_Vec_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@
test_Vec_LDFLAGS = @EXTRA_CXX_LDFLAGS@ @LIBTOOL_LINK_FLAGS@
Expand Down
214 changes: 214 additions & 0 deletions lib/ts/PriorityQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/** @file

Priority Queue Implementation using Binary Heap.

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef __PRIORITY_QUEUE_H__
#define __PRIORITY_QUEUE_H__

#include "ts/ink_assert.h"
#include "ts/Vec.h"

template <typename T> struct PriorityQueueEntry {
PriorityQueueEntry(T n) : index(0), node(n) {}

uint32_t index;
T node;
};

template <typename T> struct PriorityQueueLess {
bool operator()(const T &a, const T &b) { return *a < *b; }
};

template <typename T, class Comp = PriorityQueueLess<T> > class PriorityQueue
{
public:
PriorityQueue() {}
~PriorityQueue() {}

const bool empty();
PriorityQueueEntry<T> *top();
void pop();
void push(PriorityQueueEntry<T> *);
void update(PriorityQueueEntry<T> *);
void update(PriorityQueueEntry<T> *, bool);
const Vec<PriorityQueueEntry<T> *> &dump() const;

private:
Vec<PriorityQueueEntry<T> *> _v;

void _swap(uint32_t, uint32_t);
void _bubble_up(uint32_t);
void _bubble_down(uint32_t);
};

template <typename T, typename Comp>
const Vec<PriorityQueueEntry<T> *> &
PriorityQueue<T, Comp>::dump() const
{
return _v;
}

template <typename T, typename Comp>
const bool
PriorityQueue<T, Comp>::empty()
{
return _v.length() == 0;
}

template <typename T, typename Comp>
void
PriorityQueue<T, Comp>::push(PriorityQueueEntry<T> *entry)
{
ink_release_assert(entry != NULL);

int len = _v.length();
_v.push_back(entry);
entry->index = len;

_bubble_up(len);
}

template <typename T, typename Comp>
PriorityQueueEntry<T> *
PriorityQueue<T, Comp>::top()
{
if (empty()) {
return NULL;
} else {
return _v[0];
}
}

template <typename T, typename Comp>
void
PriorityQueue<T, Comp>::pop()
{
if (empty()) {
return;
}

_v[0] = _v[_v.length() - 1];
_v.pop();
_bubble_down(0);
}

template <typename T, typename Comp>
void
PriorityQueue<T, Comp>::update(PriorityQueueEntry<T> *entry)
{
ink_release_assert(entry != NULL);

if (empty()) {
return;
}

// One of them will works whether weight is increased or decreased
_bubble_down(entry->index);
_bubble_up(entry->index);
}

template <typename T, typename Comp>
void
PriorityQueue<T, Comp>::update(PriorityQueueEntry<T> *entry, bool increased)
{
ink_release_assert(entry != NULL);

if (empty()) {
return;
}

if (increased) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simply call both the up and down. Otherwise, caller must know whether the value is increased.
I suggest that make the flag completely optional and use it as a hint only if a caller explicitly specifies it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, if both of them are called, one of them works and other does nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

enum {
  PQ_WEIGHT_NO_HINT = 0,
  PQ_WEIGHT_INCREASED = 1,
  PQ_WEIGHT_DECREASED = 2
};

template <typename T>
void
PriorityQueue<T>::update(PriorityQueueEntry<T> *entry, uint8_t up_down_hint = PQ_WEIGHT_NO_HINT)
{
  ink_release_assert(entry != NULL);

  if (empty()) {
    return;
  }

  switch (up_down_hint) {
  case PQ_WEIGHT_INCREASED:
    _bubble_down(entry->index);
    break;
  case PQ_WEIGHT_DECREASED:
    _bubble_up(entry->index);
    break;
  default:
    // One of them will works whether weight is increased or decreased
    _bubble_down(entry->index);
    _bubble_up(entry->index);
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, something like that. I was expecting overloading, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overloading sounds good. I'll add a function to call both of up and down.

_bubble_down(entry->index);
} else {
_bubble_up(entry->index);
}
}

template <typename T, typename Comp>
void
PriorityQueue<T, Comp>::_swap(uint32_t i, uint32_t j)
{
PriorityQueueEntry<T> *tmp = _v[i];
_v[i] = _v[j];
_v[j] = tmp;

_v[i]->index = i;
_v[j]->index = j;
}


template <typename T, typename Comp>
void
PriorityQueue<T, Comp>::_bubble_up(uint32_t index)
{
if (empty()) {
ink_release_assert(false);
}

Comp comp;

uint32_t parent;
while (index != 0) {
parent = (index - 1) / 2;
if (comp(_v[index]->node, _v[parent]->node)) {
_swap(parent, index);
index = parent;
continue;
}

break;
}
}

template <typename T, typename Comp>
void
PriorityQueue<T, Comp>::_bubble_down(uint32_t index)
{
if (empty()) {
// Do nothing
return;
}

uint32_t left, right, smaller;

Comp comp;

while (true) {
if ((left = index * 2 + 1) >= _v.length()) {
break;
} else if ((right = index * 2 + 2) >= _v.length()) {
smaller = left;
} else {
smaller = comp(_v[left]->node, _v[right]->node) ? left : right;
}

if (comp(_v[smaller]->node, _v[index]->node)) {
_swap(smaller, index);
index = smaller;
continue;
}

break;
}
}

#endif // __PRIORITY_QUEUE_H__
2 changes: 1 addition & 1 deletion lib/ts/Regression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ start_test(RegressionTest *t)
}

int
RegressionTest::run(char *atest)
RegressionTest::run(const char *atest)
{
if (atest)
dfa.compile(atest);
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/Regression.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct RegressionTest {
static int ran_tests;
static DFA dfa;
static RegressionTest *current;
static int run(char *name = NULL);
static int run(const char *name = NULL);
static int run_some();
static int check_status();
};
Expand Down
1 change: 1 addition & 0 deletions lib/ts/TestBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <stdarg.h>
#include "ts/ink_apidefs.h"
#include "ts/Regression.h"

namespace
Expand Down
Loading