Commit 46a361d0 authored by jkvis's avatar jkvis

Added minimal library interface

parent 0d099896
CC = gcc
CFLAGS = -O0 -march=native -Iinclude/ -Ilib/sais-lite-lcp/
CPPFLAGS = -DNDEBUG -Wall -Wextra
CFLAGS = -O0 -march=native
CPPFLAGS = -Wall -Wextra -Iinclude/ -Ilib/sais-lite-lcp/
SOURCES = $(shell find src/ -name '*.c')
OBJECTS = $(SOURCES:.c=.o)
DEPS = $(OBJECTS:.o=.d)
LIBS = sais-lite-lcp
.PHONY: all clean
.PHONY: clean
libgesa.a: lib/lib$(LIBS).a $(OBJECTS)
ar rcs $@ $<
clean:
rm -f $(OBJECTS) libgesa.a
rm -f $(OBJECTS) $(DEPS) libgesa.a
lib/lib$(LIBS).a:
$(MAKE) -wC lib/
-include $(DEPS)
%.o: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -MMD -o $@ -c $<
#if !defined(__gesa_h__)
#define __gesa_h__
#include <stddef.h>
#include <stdio.h>
#if defined(__cplusplus)
extern "C"
{
#endif
typedef unsigned char GESA_char_t; // base type for strings
typedef int GESA_index_t; // base type for indices (signed)
static int const GESA_KEEP_STRINGS = 0;
static int const GESA_DESTROY_STRINGS = 1;
typedef struct
{
size_t n; // the number of strings
GESA_char_t const** string; // an array of n pointers to strings
size_t* length; // an array of the lengths of the strings
size_t total; // the total length of the suffix array
GESA_index_t* sa; // the suffix array
GESA_index_t* lcp; // the longest common prefix array
GESA_index_t* da; // the document array: which suffix comes
// from which string
} GESA; // GESA
// Creates a GESA from a single string
// On success it returns 0
int GESA_create(GESA* const gesa,
GESA_char_t const* const string,
size_t const length);
// All created GESAs should be destroyed. Normally, the GESA does not
// have ownership of the strings. In case it does (e.g., creation via
// deserialization) supply the GESA_DESTROY_STRINGS flag.
void GESA_destroy(GESA* const gesa,
int const destroy_strings);
#if defined(__cplusplus)
}
#endif
#endif
CC = gcc
CFLAGS = -O3 -fomit-frame-pointer -funroll-loops -march=native -I.
CPPFLAGS = -DNDEBUG -Wall -Wextra
CFLAGS = -O3 -fomit-frame-pointer -funroll-loops -march=native
CPPFLAGS = -I. -DNDEBUG -Wall -Wextra
TARGETS = sais-lite-lcp
SOURCES = sais-lite-lcp/sais.c
OBJECTS = $(SOURCES:.c=.o)
......@@ -8,13 +8,13 @@ DEPS = $(OBJECTS:.o=.d)
.PHONY: clean
-include $(DEPS)
lib$(TARGETS:./%=%).a: $(OBJECTS)
ar rcs $@ $<
clean:
rm -f $(OBJECTS) $(DEPS) lib$(TARGETS:./%=%).a
-include $(DEPS)
%.o: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -MMD -o $@ -c $<
#include <stdio.h>
#include <stdlib.h>
#include "../include/gesa.h"
#include "../lib/sais-lite-lcp/sais.h"
int GESA_create(GESA* const gesa,
GESA_char_t const* const string,
size_t const length)
{
gesa->n = 1;
gesa->string = malloc(sizeof(*gesa->string));
gesa->length = malloc(sizeof(*gesa->length));
if (gesa->string == NULL || gesa->length == NULL)
{
free(gesa->string);
free(gesa->length);
return 1; // memory allocation failed
} // if
gesa->string[0] = string;
gesa->length[0] = length;
gesa->total = length;
gesa->sa = malloc((length + 1) * sizeof(*gesa->sa)); // sais-lite-lcp specific
gesa->lcp = malloc(length * sizeof(*gesa->lcp));
gesa->da = malloc(length * sizeof(*gesa->da));
if (gesa->sa == NULL || gesa->lcp == NULL || gesa->da == NULL)
{
GESA_destroy(gesa, GESA_KEEP_STRINGS);
return 1; // memory allocation failed
} // if
for (size_t i = 0; i < length; ++i)
{
gesa->da[i] = 0;
} // for
return sais((GESA_char_t*) string, gesa->sa, gesa->lcp, (int) length);
} // GESA_create
void GESA_destroy(GESA* const gesa,
int const destroy_strings)
{
if (destroy_strings == GESA_DESTROY_STRINGS)
{
for (size_t i = 0; i < gesa->n; ++i)
{
free((void*) gesa->string[i]); // ignore const qualifier
} // for
} // if
free(gesa->string);
free(gesa->length);
free(gesa->sa);
free(gesa->lcp);
free(gesa->da);
} // GESA_destroy
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