2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief invertible bloom filter
24 * @author Florian Dold
28 #include "gnunet_common.h"
30 #include "strata_estimator.h"
33 strata_estimator_write (const struct StrataEstimator *se, void *buf)
37 GNUNET_assert (NULL != se);
38 for (i = 0; i < se->strata_count; i++)
40 ibf_write_slice (se->strata[i], 0, se->ibf_size, buf);
41 buf += se->ibf_size * IBF_BUCKET_SIZE;
46 strata_estimator_read (const void *buf, struct StrataEstimator *se)
49 for (i = 0; i < se->strata_count; i++)
51 ibf_read_slice (buf, 0, se->ibf_size, se->strata[i]);
52 buf += se->ibf_size * IBF_BUCKET_SIZE;
58 strata_estimator_insert (struct StrataEstimator *se, struct IBF_Key key)
63 /* count trailing '1'-bits of v */
64 for (i = 0; v & 1; v>>=1, i++)
66 ibf_insert (se->strata[i], key);
71 struct StrataEstimator *
72 strata_estimator_create (unsigned int strata_count, uint32_t ibf_size, uint8_t ibf_hashnum)
74 struct StrataEstimator *se;
77 /* fixme: allocate everything in one chunk */
79 se = GNUNET_malloc (sizeof (struct StrataEstimator));
80 se->strata_count = strata_count;
81 se->ibf_size = ibf_size;
82 se->strata = GNUNET_malloc (sizeof (struct InvertibleBloomFilter *) * strata_count);
83 for (i = 0; i < strata_count; i++)
84 se->strata[i] = ibf_create (ibf_size, ibf_hashnum);
90 * Estimate set difference with two strata estimators,
91 * i.e. arrays of IBFs.
92 * Does not not modify its arguments.
94 * @param se1 first strata estimator
95 * @param se2 second strata estimator
96 * @return the estimated difference
99 strata_estimator_difference (const struct StrataEstimator *se1,
100 const struct StrataEstimator *se2)
105 GNUNET_assert (se1->strata_count == se2->strata_count);
107 for (i = se1->strata_count - 1; i >= 0; i--)
109 struct InvertibleBloomFilter *diff;
110 /* number of keys decoded from the ibf */
114 /* FIXME: implement this without always allocating new IBFs */
115 diff = ibf_dup (se1->strata[i]);
116 ibf_subtract (diff, se2->strata[i]);
119 more = ibf_decode (diff, NULL, NULL);
120 if (GNUNET_NO == more)
125 if (GNUNET_SYSERR == more)
128 return count * (1 << (i + 1));
138 * Make a copy of a strata estimator.
140 * @param se the strata estimator to copy
143 struct StrataEstimator *
144 strata_estimator_dup (struct StrataEstimator *se)
146 struct StrataEstimator *c;
149 c = GNUNET_malloc (sizeof (struct StrataEstimator));
150 c->strata_count = se->strata_count;
151 c->ibf_size = se->ibf_size;
152 c->strata = GNUNET_malloc (sizeof (struct InvertibleBloomFilter *) * se->strata_count);
153 for (i = 0; i < se->strata_count; i++)
154 c->strata[i] = ibf_dup (se->strata[i]);
160 strata_estimator_destroy (struct StrataEstimator *se)
163 for (i = 0; i < se->strata_count; i++)
164 ibf_destroy (se->strata[i]);
165 GNUNET_free (se->strata);