Skip to content

Commit

Permalink
dstring module from the datrie
Browse files Browse the repository at this point in the history
  • Loading branch information
kmike committed Aug 2, 2012
1 parent 6085bbe commit 7b9d919
Show file tree
Hide file tree
Showing 11 changed files with 625 additions and 365 deletions.
2 changes: 2 additions & 0 deletions libdatrie/datrie/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ libdatrie_la_SOURCES = \
typedefs.h \
triedefs.h \
trie-private.h \
dstring.h \
dstring.c \
fileutils.h \
fileutils.c \
darray.h \
Expand Down
51 changes: 24 additions & 27 deletions libdatrie/datrie/darray.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,53 +538,51 @@ da_output_symbols (const DArray *d,
* @param d : the double-array structure
* @param from : the node to walk from
* @param to : the node to walk to
* @param res_key : the storage for storing the result
*
* @return the allocated key string
* @return boolean indicating success
*
* Get key for walking from state @a from to state @a to, assuming @a from
* is an ancester node of @a to.
* is an ancester node of @a to, and copy it to @a res_key dynamic string.
*
* The return string must be freed with free().
* The @a res_key is assumed to be constructed with
* char_size == sizeof (TrieChar).
*
* Available since: 0.2.6
*/
TrieChar *
Bool
da_get_transition_key (const DArray *d,
TrieIndex from,
TrieIndex to)
TrieIndex to,
DString *res_key)
{
TrieChar *key;
int key_size, key_length;
int key_length;
int i;

key_size = 20;
key_length = 0;
key = (TrieChar *) malloc (key_size);
dstring_clear (res_key);

/* trace back to root */
while (to != from) {
TrieIndex parent;

if (key_length + 1 >= key_size) {
key_size += 20;
key = (TrieChar *) realloc (key, key_size);
}
parent = da_get_check (d, to);
key[key_length++] = (TrieChar) (to - da_get_base (d, parent));
TrieIndex parent = da_get_check (d, to);
TrieChar tc = (TrieChar )(to - da_get_base (d, parent));
if (!dstring_append_char (res_key, &tc))
return FALSE;
to = parent;
}
key[key_length] = '\0';
if (!dstring_terminate (res_key))
return FALSE;

/* reverse the string */
key = dstring_get_val_rw (res_key);
key_length = dstring_length (res_key);
for (i = 0; i < --key_length; i++) {
TrieChar temp;

temp = key[i];
TrieChar temp = key[i];
key[i] = key[key_length];
key[key_length] = temp;
}

return key;
return TRUE;
}

static TrieIndex
Expand Down Expand Up @@ -835,11 +833,10 @@ da_enumerate_recursive (const DArray *d,
base = da_get_base (d, state);

if (base < 0) {
TrieChar *key;

key = da_get_transition_key (d, da_get_root (d), state);
ret = (*enum_func) (key, state, user_data);
free (key);
DString *key = dstring_new (sizeof (TrieChar), 20);
da_get_transition_key (d, da_get_root (d), state, key);
ret = (*enum_func) (dstring_get_val (key), state, user_data);
dstring_free (key);
} else {
Symbols *symbols;
int i;
Expand Down
6 changes: 4 additions & 2 deletions libdatrie/datrie/darray.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define __DARRAY_H

#include "triedefs.h"
#include "dstring.h"

/**
* @file darray.h
Expand Down Expand Up @@ -89,9 +90,10 @@ Bool da_walk (const DArray *d, TrieIndex *s, TrieChar c);

Symbols * da_output_symbols (const DArray *d, TrieIndex s);

TrieChar * da_get_transition_key (const DArray *d,
Bool da_get_transition_key (const DArray *d,
TrieIndex from,
TrieIndex to);
TrieIndex to,
DString *res_key);

/**
* @brief Test walkability in double-array structure
Expand Down
169 changes: 169 additions & 0 deletions libdatrie/datrie/dstring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* libdatrie - Double-Array Trie Library
* Copyright (C) 2006 Theppitak Karoonboonyanan <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
* dstring.c - Dynamic string type
* Created: 2012-08-01
* Author: Theppitak Karoonboonyanan <[email protected]>
*/

#include "dstring.h"

#include "trie-private.h"
#include <string.h>
#include <stdlib.h>

struct _DString {
int char_size;
int str_len;
int alloc_size;
void * val;
};


DString *
dstring_new (int char_size, int n_elm)
{
DString *ds;

ds = (DString *) malloc (sizeof (DString));
if (!ds)
return NULL;

ds->alloc_size = char_size * n_elm;
ds->val = malloc (ds->alloc_size);
if (!ds->val) {
free (ds);
return NULL;
}

ds->char_size = char_size;
ds->str_len = 0;

return ds;
}

void
dstring_free (DString *ds)
{
free (ds->val);
free (ds);
}

int
dstring_length (const DString *ds)
{
return ds->str_len;
}

const void *
dstring_get_val (const DString *ds)
{
return ds->val;
}

void *
dstring_get_val_rw (DString *ds)
{
return ds->val;
}

void
dstring_clear (DString *ds)
{
ds->str_len = 0;
}

static Bool
dstring_ensure_space (DString *ds, int size)
{
if (ds->alloc_size < size) {
int re_size = MAX_VAL (ds->alloc_size * 2, size);
void *re_ptr = realloc (ds->val, re_size);
if (!re_ptr)
return FALSE;
ds->val = re_ptr;
ds->alloc_size = re_size;
}

return TRUE;
}

Bool
dstring_copy (DString *dst, const DString *src)
{
if (!dstring_ensure_space (dst, (src->str_len + 1) * src->char_size))
return FALSE;

memcpy (dst->val, src->val, (src->str_len + 1) * src->char_size);

dst->char_size = src->char_size;
dst->str_len = src->str_len;

return TRUE;
}

Bool
dstring_append (DString *dst, const DString *src)
{
if (dst->char_size != src->char_size)
return FALSE;

if (!dstring_ensure_space (dst, (dst->str_len + src->str_len + 1)
* dst->char_size))
{
return FALSE;
}

memcpy (dst->val + (dst->char_size * dst->str_len), src->val,
(src->str_len + 1) * dst->char_size);

dst->str_len += src->str_len;

return TRUE;
}

Bool
dstring_append_char (DString *ds, const void *data)
{
if (!dstring_ensure_space (ds, (ds->str_len + 2) * ds->char_size))
return FALSE;

memcpy (ds->val + (ds->char_size * ds->str_len), data, ds->char_size);

ds->str_len++;

return TRUE;
}

Bool
dstring_terminate (DString *ds)
{
if (!dstring_ensure_space (ds, (ds->str_len + 2) * ds->char_size))
return FALSE;

memset (ds->val + (ds->char_size * ds->str_len), 0, ds->char_size);

return TRUE;
}

/*
vi:ts=4:ai:expandtab
*/
59 changes: 59 additions & 0 deletions libdatrie/datrie/dstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* libdatrie - Double-Array Trie Library
* Copyright (C) 2006 Theppitak Karoonboonyanan <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
* dstring.h - Dynamic string type
* Created: 2012-08-01
* Author: Theppitak Karoonboonyanan <[email protected]>
*/

#ifndef __DSTRING_H
#define __DSTRING_H

#include "typedefs.h"

typedef struct _DString DString;

DString * dstring_new (int char_size, int n_elm);

void dstring_free (DString *ds);

int dstring_length (const DString *ds);

const void * dstring_get_val (const DString *ds);

void * dstring_get_val_rw (DString *ds);

void dstring_clear (DString *ds);

Bool dstring_copy (DString *dst, const DString *src);

Bool dstring_append (DString *dst, const DString *src);

Bool dstring_append_char (DString *ds, const void *data);

Bool dstring_terminate (DString *ds);


#endif /* __DSTRING_H */

/*
vi:ts=4:ai:expandtab
*/
Loading

0 comments on commit 7b9d919

Please sign in to comment.