#include "keyword-list.h"
#include <stddef.h>
Keyword_List::Keyword_List (Keyword *car)
: _cdr (NULL), _car (car)
{
}
KeywordExt_List::KeywordExt_List (KeywordExt *car)
: Keyword_List (car)
{
}
Keyword_List *
copy_list (Keyword_List *list)
{
Keyword_List *result;
Keyword_List **lastp = &result;
while (list != NULL)
{
Keyword_List *new_cons = new Keyword_List (list->first());
*lastp = new_cons;
lastp = &new_cons->rest();
list = list->rest();
}
*lastp = NULL;
return result;
}
KeywordExt_List *
copy_list (KeywordExt_List *list)
{
return static_cast<KeywordExt_List *> (copy_list (static_cast<Keyword_List *> (list)));
}
void
delete_list (Keyword_List *list)
{
while (list != NULL)
{
Keyword_List *rest = list->rest();
delete list;
list = rest;
}
}
typedef bool (*Keyword_Comparison) (Keyword *keyword1, Keyword *keyword2);
static Keyword_List *
merge (Keyword_List *list1, Keyword_List *list2, Keyword_Comparison less)
{
Keyword_List *result;
Keyword_List **resultp = &result;
for (;;)
{
if (!list1)
{
*resultp = list2;
break;
}
if (!list2)
{
*resultp = list1;
break;
}
if (less (list2->first(), list1->first()))
{
*resultp = list2;
resultp = &list2->rest();
list2 = list1; list1 = *resultp;
}
else
{
*resultp = list1;
resultp = &list1->rest();
list1 = *resultp;
}
}
return result;
}
Keyword_List *
mergesort_list (Keyword_List *list, Keyword_Comparison less)
{
if (list == NULL || list->rest() == NULL)
return list;
else
{
Keyword_List *middle = list;
for (Keyword_List *temp = list->rest();;)
{
temp = temp->rest();
if (temp == NULL)
break;
temp = temp->rest();
middle = middle->rest();
if (temp == NULL)
break;
}
Keyword_List *right_half = middle->rest();
middle->rest() = NULL;
return merge (mergesort_list (list, less),
mergesort_list (right_half, less),
less);
}
}
KeywordExt_List *
mergesort_list (KeywordExt_List *list,
bool (*less) (KeywordExt *keyword1, KeywordExt *keyword2))
{
return
static_cast<KeywordExt_List *>
(mergesort_list (static_cast<Keyword_List *> (list),
reinterpret_cast<Keyword_Comparison> (less)));
}
#ifndef __OPTIMIZE__
#define INLINE
#include "keyword-list.icc"
#undef INLINE
#endif