/* -------------------------------------------------------------------------
 *  This file is part of the oGRAC project.
 * Copyright (c) 2024 Huawei Technologies Co.,Ltd.
 *
 * oGRAC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *          http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * kwlookup.h
 *
 *
 * IDENTIFICATION
 * src/ogsql/parser/kwlookup.h
 *
 * -------------------------------------------------------------------------
 */
#ifndef KWLOOKUP_H
#define KWLOOKUP_H

#include <stddef.h>
#include "cm_types.h"

/* Hash function used by ScanKeywordLookup */
typedef int (*ScanKeywordHashFunc) (const void *key, size_t keylen);

/*
 * This struct contains the data needed by ScanKeywordLookup to perform a
 * search within a set of keywords.  The contents are typically generated by
 * src/tools/gen_keywordlist.pl from a header containing PG_KEYWORD macros.
 */
typedef struct ScanKeywordList {
    const char *kw_string;      /* all keywords in order, separated by \0 */
    const uint16 *kw_offsets;   /* offsets to the start of each keyword */
    ScanKeywordHashFunc hash;   /* perfect hash function for keywords */
    int         num_keywords;   /* number of keywords */
    int         max_kw_len;     /* length of longest keyword */
} ScanKeywordList;


extern int	ScanKeywordLookup(const char *text, const ScanKeywordList *keywords);

/* Code that wants to retrieve the text of the N'th keyword should use this. */
static inline const char *GetScanKeyword(int n, const ScanKeywordList *keywords)
{
    return keywords->kw_string + keywords->kw_offsets[n];
}

#endif /* KWLOOKUP_H */