/* 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.
 */

#include <b.h>

static b_error_t b_iterator_type_pointer (b_iterator_type_t * iterator,
					  b_cache_t ** cache);
static b_error_t b_iterator_type_pointer_init (b_iterator_type_t *
					       iterator, b_cache_t ** cache);
static b_error_t b_iterator_type_pointer_right (b_iterator_type_t *
						iterator, b_cache_t ** cache);
static b_error_t b_iterator_type_pointer_up (b_iterator_type_t *
					     iterator, b_cache_t ** cache);
static b_error_t b_iterator_type_pointer_found (b_iterator_type_t *
						iterator,
						b_cache_t ** cache,
						b_map_t * map,
						b_uint64 pointer);

static b_error_t b_iterator_triple_pointer (b_iterator_triple_t *
					    iterator,
					    b_uint64 tmap_pointer,
					    unsigned char tmap_index,
					    b_tcache_t ** cache);

/**
 * b_iterator_type_new:
 * @data: The pointer to a b_t data struct.
 * @type: The type of the elements
 * @iterator: The return pointer.
 * @returns: The error code.
 *
 * Creates a new Iterator for a type of element. The iterator must be destoyed
 * with b_iterator_type_destroy(). You should use b_iterator_type_step() to get
 * the list of results.
 **/
b_error_t
b_iterator_type_new (b_t * data, b_type_t type, b_iterator_type_t ** iterator)
{
  b_io_t *io;
  b_error_t error;

  B_LOG3 ("b_iterator_type_new: %p %d %p", data, type, iterator);

  if (!data || !iterator)
    return B_ERROR_DATA;

  if ((error = b_get_type_internal (data, type, &io)) != B_OK)
    return error;

  if (!(*iterator = calloc (1, sizeof (b_iterator_type_t))))
    return B_ERROR_MEMORY;

  (*iterator)->io = io;
  (*iterator)->data = data;
  (*iterator)->type = type;

  (*iterator)->right = -1;

  return B_OK;
}

/**
 * b_iterator_type_destroy:
 * @iterator: The pointer of a b_iterator_t struct.
 * @returns: The error code.
 *
 * Destroies a b_iterator_t struct.
 **/
b_error_t
b_iterator_type_destroy (b_iterator_type_t * iterator)
{
  B_LOG1 ("b_iterator_type_destroy: %p", iterator);

  if (!iterator)
    return B_ERROR_DATA;

  free (iterator);

  return B_OK;
}

/**
 * b_iterator_type_step:
 * @iterator: The pointer of a b_iterator_t struct.
 * @ret: The return string for the next result.
 * @ret_size: the len of the ret string. It can be NULL.
 * @returns: The error code.
 *
 * Returns the next element of the storage iterator. If *ret will be NULL
 * no other results are into the storage. The new *ret string must be freed
 * with a normal free().
 **/
b_error_t
b_iterator_type_step (b_iterator_type_t * iterator, unsigned char **ret,
		      b_uint64 * ret_size)
{
  b_cache_t *cache;
  b_error_t error;

  B_LOG3 ("b_iterator_type_step: %p %p %p", iterator, ret, ret_size);

  if (!iterator || !ret)
    return B_ERROR_DATA;

  error = b_iterator_type_pointer (iterator, &cache);

  if (error != B_OK)
    return error;

  if (cache)
    {
      if (!(*ret = b_string_alloc (cache->what, cache->len)))
	return B_ERROR_MEMORY;

      if (ret_size)
	*ret_size = cache->len;

      b_cache_destroy (cache);
    }
  else
    {
      *ret = NULL;

      if (ret_size)
	*ret_size = 0;
    }

  return B_OK;
}

static b_error_t
b_iterator_type_pointer (b_iterator_type_t * iterator, b_cache_t ** cache)
{
  *cache = NULL;

  B_LOG2 ("b_iterator_type_pointer: %p %p", iterator, cache);

  if (!iterator->pointer)
    return b_iterator_type_pointer_init (iterator, cache);

  else if (iterator->right == 1)
    return b_iterator_type_pointer_right (iterator, cache);

  else if (iterator->right == 0)
    return b_iterator_type_pointer_up (iterator, cache);

  else
    return B_OK;
}

static b_error_t
b_iterator_type_pointer_init (b_iterator_type_t * iterator,
			      b_cache_t ** cache)
{
  b_map_t map;
  b_uint64 pointer;

  B_LOG2 ("b_iterator_type_pointer_init: %p %p", iterator, cache);

  pointer = b_io_seek (iterator->io, sizeof (b_uint64), SEEK_SET);

  /* Search the first noempty element: */
  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
    return B_OK;

  if (map.left)
    {
      while (map.left)
	{
	  pointer = b_io_seek (iterator->io, map.left, SEEK_SET);
	  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
	    return B_ERROR_INTERNAL;
	}

      if (map.right)
	iterator->right = 1;
      else
	iterator->right = 0;

      iterator->pointer = pointer;
      return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
    }

  if (map.right)
    {
      iterator->right = 1;
      iterator->pointer = pointer;
      return b_iterator_type_pointer (iterator, cache);
    }

  return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
}

static b_error_t
b_iterator_type_pointer_right (b_iterator_type_t * iterator,
			       b_cache_t ** cache)
{
  b_map_t map;
  b_uint64 pointer;

  B_LOG2 ("b_iterator_type_pointer_right: %p %p", iterator, cache);

  pointer = b_io_seek (iterator->io, iterator->pointer, SEEK_SET);
  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
    return B_ERROR_INTERNAL;

  pointer = b_io_seek (iterator->io, map.right, SEEK_SET);
  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
    return B_ERROR_INTERNAL;

  if (map.left)
    {
      while (map.left)
	{
	  pointer = b_io_seek (iterator->io, map.left, SEEK_SET);
	  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
	    return B_ERROR_INTERNAL;
	}

      if (map.right)
	iterator->right = 1;
      else
	iterator->right = 0;

      iterator->pointer = pointer;
      return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
    }

  if (map.right)
    iterator->right = 1;
  else
    iterator->right = 0;

  iterator->pointer = pointer;
  return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
}

static b_error_t
b_iterator_type_pointer_up (b_iterator_type_t * iterator, b_cache_t ** cache)
{
  b_map_t map;
  b_uint64 pointer;

  B_LOG2 ("b_iterator_type_pointer_up: %p %p", iterator, cache);

  pointer = b_io_seek (iterator->io, iterator->pointer, SEEK_SET);
  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
    return B_ERROR_INTERNAL;

  if (!map.parent)
    return B_OK;

  pointer = b_io_seek (iterator->io, map.parent, SEEK_SET);
  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
    return B_ERROR_INTERNAL;

  if (map.left == iterator->pointer)
    {
      if (map.right)
	iterator->right = 1;
      else
	iterator->right = 0;

      iterator->pointer = pointer;
      return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
    }

  iterator->right = 0;
  iterator->pointer = pointer;
  return b_iterator_type_pointer (iterator, cache);
}

static b_error_t
b_iterator_type_pointer_found (b_iterator_type_t * iterator,
			       b_cache_t ** cache, b_map_t * map,
			       b_uint64 pointer)
{
  b_error_t error;
  unsigned char *what;

  B_LOG4 ("b_iterator_type_pointer_found: %p %p %p %d", iterator, cache, map,
	  (int) pointer);

  if (pointer == sizeof (b_uint64))
    return b_iterator_type_pointer (iterator, cache);

  b_io_seek (iterator->io, pointer + sizeof (b_map_t), SEEK_SET);

  if (!(what = malloc (sizeof (unsigned char) * ((size_t) map->len + 1))))
    return B_ERROR_MEMORY;

  if (map->len && b_io_read (iterator->io, what, map->len) <= 0)
    {
      free (what);
      return B_ERROR_INTERNAL;
    }

  what[map->len] = 0;

  error = b_cache_new (map->hash, what, map->len, pointer, cache);

  free (what);
  return error;
}

/**
 * b_iterator_triple_new:
 * @data: The pointer to a b_t data struct.
 * @iterator: The return pointer for the new iterator.
 * @compare: If you want have only results from a template, this b_triple_t
 * describes the template. You can create it with b_triple_new_incomplete().
 * @returns: The error code.
 *
 * Creates a new iterator for triples. It must be destroied with 
 * b_iterator_triple_destroy(). You can get the list of results with 
 * b_iterator_triple_step().
 **/
b_error_t
b_iterator_triple_new (b_t * data, b_iterator_triple_t ** iterator,
		       b_triple_t * compare)
{
  B_LOG3 ("b_iterator_triple_new: %p %p %p", data, iterator, compare);

  if (!data || !iterator)
    return B_ERROR_DATA;

  b_sync (data);

  if (!(*iterator = calloc (1, sizeof (b_iterator_triple_t))))
    return B_ERROR_MEMORY;

  (*iterator)->data = data;
  (*iterator)->status = -1;

  if (compare)
    {
      if (!((*iterator)->compare = calloc (1, sizeof (b_iterator_compare_t))))
	{
	  free (*iterator);
	  return B_ERROR_MEMORY;
	}

#define B_ITERATOR_COMPARE( x , len , e ) \
      if (compare->x) \
        { \
  	  b_hash_new (data, compare->x, compare->len, \
                      (*iterator)->compare->x); \
          (*iterator)->compare->e = 1; \
        }

      B_ITERATOR_COMPARE (subject_uri, subject_uri_len, subject_uri_exists);
      B_ITERATOR_COMPARE (subject_bnode, subject_bnode_len,
			  subject_bnode_exists);

      B_ITERATOR_COMPARE (property, property_len, property_exists);

      B_ITERATOR_COMPARE (object_uri, object_uri_len, object_uri_exists);
      B_ITERATOR_COMPARE (object_bnode, object_bnode_len,
			  object_bnode_exists);
      B_ITERATOR_COMPARE (object_literal, object_literal_len,
			  object_literal_exists);

      B_ITERATOR_COMPARE (context, context_len, context_exists);

      B_ITERATOR_COMPARE (datatype, datatype_len, datatype_exists);
      B_ITERATOR_COMPARE (lang, lang_len, lang_exists);
    }

  return B_OK;
}

/**
 * b_iterator_triple_destroy:
 * @iterator: The triple iterator.
 * @returns: The error code.
 *
 * Destroies a b_iterator_triple_t data struct.
 **/
b_error_t
b_iterator_triple_destroy (b_iterator_triple_t * iterator)
{
  B_LOG1 ("b_iterator_triple_destroy: %p", iterator);

  if (!iterator)
    return B_ERROR_DATA;

  if (iterator->compare)
    free (iterator->compare);

  free (iterator);

  return B_OK;
}

/**
 * b_iterator_triple_step:
 * @iterator: The triple iterator.
 * @triple: The return pointer for the next b_triple_t. If it is NULL, no other
 * results are into the storage.
 * @returns: The error code.
 *
 * Gets the next results. The new b_triple_t must be freed with
 * b_triple_destroy().
 **/
b_error_t
b_iterator_triple_step (b_iterator_triple_t * iterator, b_triple_t ** ret)
{
  b_tcache_t *cache;
  b_error_t error;

  B_LOG2 ("b_iterator_triple_step: %p %p", iterator, ret);

  if (!iterator->file)
    error =
      b_iterator_triple_pointer (iterator, sizeof (b_uint64), 0, &cache);

  /* Previous results? */
  else if (iterator->results)
    {
      cache = b_el_first (&iterator->results);
      error = B_OK;
    }

  else if (!iterator->compare && iterator->file->tmap_index < B_TMAP_NUMB - 1)
    {
      error =
	b_iterator_triple_pointer (iterator, iterator->file->tmap_pointer,
				   iterator->file->tmap_index + 1, &cache);
    }
  else
    {
      error =
	b_iterator_triple_pointer (iterator,
				   iterator->file->tmap_pointer +
				   sizeof (b_tmap_t) +
				   B_TRIPLE_SIZE * B_TMAP_NUMB, 0, &cache);
    }

  if (error != B_OK)
    return error;

  if (cache)
    {
      if (iterator->file)
	b_tcache_destroy_single (&iterator->file, NULL, iterator->file);

      iterator->file = cache;

      if ((error = b_triple_new_cache (cache, ret)) != B_OK)
	return error;
    }
  else
    {
      if (iterator->file)
	b_tcache_destroy_single (&iterator->file, NULL, iterator->file);

      *ret = NULL;
    }

  return B_OK;
}

static b_error_t
b_iterator_triple_pointer (b_iterator_triple_t * iterator,
			   b_uint64 tmap_pointer, unsigned char tmap_index,
			   b_tcache_t ** cache)
{
  b_tcache_t *c;
  b_error_t error;
  b_tmap_t tmap;
  b_el_t *list;

  B_LOG4 ("b_iterator_triple_pointer: %p %d %d %p", iterator,
	  (int) tmap_pointer, tmap_index, cache);

  *cache = NULL;

  if (!iterator->compare)
    {
      b_tmap_item_t tmap_item;

      b_io_seek (iterator->data->triples_io, tmap_pointer, SEEK_SET);

      /* This index is full ? */
      if (b_io_read (iterator->data->triples_io, &tmap, sizeof (b_tmap_t)) <=
	  0)
	return B_OK;

      if ((error =
	   b_tempty_check (iterator->data, &tmap, tmap_pointer, 0)) != B_OK)
	return error;

      if (!tmap.map[tmap_index])
	{
	  for (; !tmap.map[tmap_index] && tmap_index < B_TMAP_NUMB;
	       tmap_index++);

	  if (tmap_index == B_TMAP_NUMB)
	    return b_iterator_triple_pointer (iterator,
					      tmap_pointer +
					      sizeof (b_tmap_t) +
					      B_TRIPLE_SIZE * B_TMAP_NUMB, 0,
					      cache);
	}

      b_io_seek (iterator->data->triples_io,
		 tmap_pointer + sizeof (b_tmap_t) +
		 tmap_index * B_TRIPLE_SIZE, SEEK_SET);

      if (!(c = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
	return B_ERROR_MEMORY;

#define B_TRIPLE_POINTER( x , t ) \
  if (b_io_read (iterator->data->triples_io, &tmap_item, \
		 sizeof (b_tmap_item_t)) <= 0) \
    { \
      b_tcache_destroy_single (NULL, NULL, c); \
      return B_ERROR_INTERNAL; \
    } \
  \
  if (tmap_item.pointer) \
    { \
      if ((error = b_get_type_pointer (iterator->data, t, \
				       tmap_item.pointer, &c->x)) \
          != B_OK) \
	{ \
          b_tcache_destroy_single (NULL, NULL, c); \
	  return error; \
	} \
      \
      if (!c->x) \
	{ \
          b_tcache_destroy_single (NULL, NULL, c); \
	  return B_ERROR_INTERNAL; \
	} \
    }

      B_TRIPLE_POINTER (subject_uri, B_TYPE_URI);
      B_TRIPLE_POINTER (subject_bnode, B_TYPE_BNODE);
      B_TRIPLE_POINTER (property, B_TYPE_PROPERTY);
      B_TRIPLE_POINTER (object_uri, B_TYPE_URI);
      B_TRIPLE_POINTER (object_bnode, B_TYPE_BNODE);
      B_TRIPLE_POINTER (object_literal, B_TYPE_LITERAL);
      B_TRIPLE_POINTER (context, B_TYPE_CONTEXT);
      B_TRIPLE_POINTER (datatype, B_TYPE_DATATYPE);
      B_TRIPLE_POINTER (lang, B_TYPE_LITERAL);

      c->tmap_index = tmap_index;
      c->tmap_pointer = tmap_pointer;
      *cache = c;
      return B_OK;
    }

  /* To compare: */
  else
    {
      int tmap_results[B_TMAP_NUMB];
      int i, j, elements;

      if (iterator->results)
	return B_ERROR_INTERNAL;

      b_io_seek (iterator->data->triples_io, tmap_pointer, SEEK_SET);

      if (b_io_read (iterator->data->triples_io, &tmap, sizeof (b_tmap_t)) <=
	  0)
	return B_OK;

      if ((error =
	   b_tempty_check (iterator->data, &tmap, tmap_pointer, 0)) != B_OK)
	return error;

      memset (&tmap_results, 0, sizeof (tmap_results));

      /* Disactive no compatible results: */
      for (i = elements = 0; i < B_TMAP_NUMB; i++)
	{
	  if (!tmap.map[i])
	    tmap_results[i] = 1;
	  else
	    elements++;
	}

      /* Disactive no compatible results: */
      for (i = 0; i < elements; i++)
	{
	  if (iterator->compare->subject_uri_exists
	      && B_TMAP_VALUE (tmap.subjects_uri[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.subjects_uri[i])] = 1;

	  if (iterator->compare->subject_bnode_exists
	      && B_TMAP_VALUE (tmap.subjects_bnode[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.subjects_bnode[i])] = 1;

	  if (iterator->compare->object_uri_exists
	      && B_TMAP_VALUE (tmap.objects_uri[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.objects_uri[i])] = 1;

	  if (iterator->compare->object_bnode_exists
	      && B_TMAP_VALUE (tmap.objects_bnode[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.objects_bnode[i])] = 1;

	  if (iterator->compare->object_literal_exists
	      && B_TMAP_VALUE (tmap.objects_literal[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.objects_literal[i])] = 1;

	  if (iterator->compare->context_exists
	      && B_TMAP_VALUE (tmap.contexts[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.contexts[i])] = 1;

	  if (iterator->compare->datatype_exists
	      && B_TMAP_VALUE (tmap.datatypes[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.datatypes[i])] = 1;

	  if (iterator->compare->lang_exists
	      && B_TMAP_VALUE (tmap.langs[i]) == B_TMAP_VALUE_EMPTY)
	    tmap_results[B_TMAP_DATA (tmap.langs[i])] = 1;
	}

#define B_GET_FILE( x , y , n , e ) \
  if (tmap_results[i]) \
    continue; \
  \
  if (iterator->compare->e) \
    { \
      b_io_seek (iterator->data->triples_io, tmap_pointer + \
                 sizeof (b_tmap_t) + i * B_TRIPLE_SIZE + \
		 sizeof(b_tmap_item_t) * n, SEEK_SET); \
      if (b_io_read (iterator->data->triples_io, &x, \
		     sizeof (b_tmap_item_t)) <= 0) \
        return B_ERROR_INTERNAL; \
        \
      switch (b_hash_compare (x.hash, iterator->compare->x)) \
	{ \
          case -1: \
            for (j = 0; j < B_TMAP_NUMB; j++) \
              { \
                if (B_TMAP_VALUE (tmap.y[j]) != B_TMAP_VALUE_EMPTY) \
                  { \
                    tmap_results[B_TMAP_DATA(tmap.y[j])] = 1; \
		    if (B_TMAP_DATA(tmap.y[j]) == i) \
		      break; \
                  } \
              } \
            break; \
          \
          case 1: \
            for (j = B_TMAP_NUMB-1; j >=0; j--) \
              { \
                if (B_TMAP_VALUE (tmap.y[j]) != B_TMAP_VALUE_EMPTY) \
                  { \
                    tmap_results[B_TMAP_DATA(tmap.y[j])] = 1; \
		    if (B_TMAP_DATA(tmap.y[j]) == i) \
		     break; \
                  } \
              } \
            break; \
          \
          case 0: \
          default: \
            break; \
	} \
    }

      for (i = 0; i < B_TMAP_NUMB; i++)
	{
	  b_tmap_item_t subject_uri, subject_bnode, property, object_uri,
	    object_bnode, object_literal, context, datatype, lang;

	  B_GET_FILE (subject_uri, subjects_uri, 0, subject_uri_exists);
	  B_GET_FILE (subject_bnode, subjects_bnode, 1, subject_bnode_exists);

	  B_GET_FILE (property, properties, 2, property_exists);

	  B_GET_FILE (object_uri, objects_uri, 3, object_uri_exists);
	  B_GET_FILE (object_bnode, objects_bnode, 4, object_bnode_exists);
	  B_GET_FILE (object_literal, objects_literal, 5,
		      object_literal_exists);
	  B_GET_FILE (context, contexts, 6, context_exists);
	  B_GET_FILE (datatype, datatypes, 7, datatype_exists);
	  B_GET_FILE (lang, langs, 8, lang_exists);

	  if (tmap_results[i])
	    continue;

	  if (!(c = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
	    {
	      b_el_free (&iterator->results);
	      return B_ERROR_MEMORY;
	    }

	  b_io_seek (iterator->data->triples_io,
		     tmap_pointer + sizeof (b_tmap_t) + i * B_TRIPLE_SIZE,
		     SEEK_SET);

#define B_TRIPLE_POINTER2( x , t ) \
  if (b_io_read (iterator->data->triples_io, &x, \
		 sizeof (b_tmap_item_t)) <= 0) \
    { \
      b_tcache_destroy_single (NULL, NULL, c); \
      b_el_free (&iterator->results); \
      return B_ERROR_INTERNAL; \
    } \
  \
  if (x.pointer) \
    { \
      if ((error = b_get_type_pointer (iterator->data, t, \
			               x.pointer, &c->x)) \
          != B_OK) \
	{ \
          b_tcache_destroy_single (NULL, NULL, c); \
	  b_el_free (&iterator->results); \
	  return error; \
	} \
      \
      if (!c->x) \
	{ \
          b_tcache_destroy_single (NULL, NULL, c); \
	  b_el_free (&iterator->results); \
	  return B_ERROR_INTERNAL; \
	} \
    }

	  B_TRIPLE_POINTER2 (subject_uri, B_TYPE_URI);
	  B_TRIPLE_POINTER2 (subject_bnode, B_TYPE_BNODE);
	  B_TRIPLE_POINTER2 (property, B_TYPE_PROPERTY);
	  B_TRIPLE_POINTER2 (object_uri, B_TYPE_URI);
	  B_TRIPLE_POINTER2 (object_bnode, B_TYPE_BNODE);
	  B_TRIPLE_POINTER2 (object_literal, B_TYPE_LITERAL);
	  B_TRIPLE_POINTER2 (context, B_TYPE_CONTEXT);
	  B_TRIPLE_POINTER2 (datatype, B_TYPE_DATATYPE);
	  B_TRIPLE_POINTER2 (lang, B_TYPE_LITERAL);

	  c->tmap_index = i;
	  c->tmap_pointer = tmap_pointer;

	  if ((error = b_el_new (c, &iterator->results)) != B_OK)
	    {
	      b_tcache_destroy_single (NULL, NULL, c);
	      b_el_free (&iterator->results);
	      return error;
	    }
	}

      if (iterator->results)
	{
	  list = iterator->results;

	  *cache = list->cache;
	  iterator->results = list->next;
	  free (list);
	}
      else
	{
	  return b_iterator_triple_pointer (iterator,
					    tmap_pointer + sizeof (b_tmap_t) +
					    B_TRIPLE_SIZE * B_TMAP_NUMB, 0,
					    cache);
	}

      return B_OK;
    }
}

/* EOF */
