aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmSet.c
blob: 61a3b96e9f13b771c84cfbaea8d6d2a0619310d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*=============================================================================*\
 * File: gfsmSet.c
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library
 *
 * Copyright (c) 2004-2008 Bryan Jurish.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *=============================================================================*/

#include <gfsmSet.h>
#include <gfsmCommon.h>

//-- no-inline definitions
#ifndef GFSM_INLINE_ENABLED
# include <gfsmSet.hi>
#endif

/*======================================================================
 * Constructors etc.
 */

/*--------------------------------------------------------------
 * copy_foreach_func()
 */
gboolean gfsm_set_copy_foreach_func(gpointer key, gpointer value, gfsmSet *data)
{
  if (data) g_tree_insert(data,key,value);
  return FALSE; // don't stop iterating
}

/*--------------------------------------------------------------
 * clear()
 */
void gfsm_set_clear(gfsmSet *set)
{
  guint i;
  GPtrArray *keys = g_ptr_array_sized_new(gfsm_set_size(set));
  gfsm_set_to_ptr_array(set,keys);
  for (i=0; i < keys->len; i++) {
    g_tree_remove(set, g_ptr_array_index(keys,i));
  }
  g_ptr_array_free(keys,TRUE);
}


/*======================================================================
 * Algebra
 */

/*--------------------------------------------------------------
 * union_func()
 */
gboolean gfsm_set_union_func(gpointer key, GFSM_UNUSED gpointer value, gfsmSetUnionData *data)
{
  if (!data->dupfunc) {
    //-- no memory hairiness: just insert
    gfsm_set_insert(data->dst, key);
  } else {
    if (!gfsm_set_contains(data->dst, key))
      gfsm_set_insert(data->dst, (*data->dupfunc)(key));
  }
  return FALSE;
}

/*--------------------------------------------------------------
 * difference_func()
 */
gboolean gfsm_set_difference_func(gpointer key, GFSM_UNUSED gpointer value, gfsmSet *set1)
{
  gfsm_set_remove(set1,key);
  return FALSE;
}

/*--------------------------------------------------------------
 * intersection()
 */
gfsmSet *gfsm_set_intersection(gfsmSet *set1, gfsmSet *set2)
{
  guint i;
  GPtrArray *elts1 = g_ptr_array_sized_new(gfsm_set_size(set1));
  gfsm_set_to_ptr_array(set1,elts1);
  for (i=0; i < elts1->len; i++) {
    gpointer elt = g_ptr_array_index(elts1,i);
    if (!gfsm_set_contains(set2,elt)) gfsm_set_remove(set1,elt);
  }
  g_ptr_array_free(elts1,TRUE);
  return set1;
}


/*======================================================================
 * Converters
 */

/*--------------------------------------------------------------
 * to_slist_foreach_func()
 */
gboolean gfsm_set_to_slist_foreach_func(gpointer key, GFSM_UNUSED gpointer value, GSList **dst)
{
  *dst = g_slist_prepend(*dst, key);
  return FALSE; //-- don't stop iterating
}

/*--------------------------------------------------------------
 * to_ptr_array_foreach_func()
 */
gboolean gfsm_set_to_ptr_array_foreach_func(gpointer key, GFSM_UNUSED gpointer value, GPtrArray *dst)
{
  g_ptr_array_add(dst,key);
  return FALSE;
}

/*======================================================================
 * Debugging
 */
#ifdef GFSM_DEBUG_ENABLED

gboolean gfsm_set_print_foreach_func(gpointer key, gpointer data, FILE *f)
{
  fprintf(f, " %u", GPOINTER_TO_UINT(key));
  return FALSE;
}

void gfsm_set_print_uint(gfsmSet *set, FILE *f)
{
  fputc('{',f);
  g_tree_foreach(set, (GTraverseFunc)gfsm_set_print_foreach_func, f);
  fputs(" }", f);
}

#endif /*GFSM_DEBUG_ENABLED*/