Skip to content

Commit

Permalink
iterkeys, keys, iteritems, values, itervalues
Browse files Browse the repository at this point in the history
  • Loading branch information
isamaru committed Oct 17, 2017
1 parent 3a0add1 commit 6032dc7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bounter/count_min_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def __init__(self, size_mb=64, width=None, depth=None, log_counting=None):
If width is not provided, the algorithm chooses the maximum width to fill the available size.
The more, the better, should be very large, preferably in the same order of magnitude as the cardinality
of the counted set.
log_counting (int): Use logarithmic counter value for reduced bucket size:
log_counting (int): Use logarithmic approximate counter value for reduced bucket size:
- None (default): 4B, no counter error
- 1024: 2B, counter error ~2% for values larger than 1024
- 8: 1B, counter error ~30% for values larger than 8
- 1024: 2B, value approximation error ~2% for values larger than 2048
- 8: 1B, value approximation error ~30% for values larger than 16
"""

cell_size = CountMinSketch.cell_size(log_counting)
Expand Down
47 changes: 47 additions & 0 deletions bounter/tests/hashtable/test_htc_iteration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: Filip Stefanak <[email protected]>
# Copyright (C) 2017 Rare Technologies
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).

import unittest

from bounter import HashTable


class HashTableIterationTest(unittest.TestCase):
"""
Functional tests for additional HashTable iterations methods, which all produce an iterator
"""

def setUp(self):
self.ht = HashTable(buckets=64)
self.ht.update([u"foo", u"bar", u"foo", "foo"])
self.keys = {u"foo", u"bar"}
self.values = {3, 1}
self.pairs = {(u"foo", 3), (u"bar", 1)}

def test_keys(self):
self.assertEqual(set(self.ht.keys()), self.keys)

def test_iterkeys(self):
self.assertEqual(set(self.ht.iterkeys()), self.keys)

def test_values(self):
self.assertEqual(set(self.ht.values()), self.values)

def test_itervalues(self):
self.assertEqual(set(self.ht.itervalues()), self.values)

def test_items(self):
self.assertEqual(set(self.ht.items()), self.pairs)

def test_iteritems(self):
self.assertEqual(set(self.ht.iteritems()), self.pairs)


if __name__ == '__main__':
unittest.main()
26 changes: 26 additions & 0 deletions cbounter/ht_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,12 @@ PyObject* HT_VARIANT(_ITER_iternext)(HT_VARIANT(_ITER_TYPE) *self)

if (i < buckets)
{
if (self->result_type == ITER_RESULT_VALUES)
{
self->i = i + 1;
return Py_BuildValue("L", table[i].count);
}

PyObject * result;
char * current_key = table[i].key;
PyObject * pkey;
Expand Down Expand Up @@ -915,6 +921,11 @@ HT_VARIANT(_HT_iter_K)(HT_TYPE *self)
return HT_VARIANT(_make_iterator)(self, ITER_RESULT_KEYS);
}

static HT_VARIANT(_ITER_TYPE) *
HT_VARIANT(_HT_iter_V)(HT_TYPE *self)
{
return HT_VARIANT(_make_iterator)(self, ITER_RESULT_VALUES);
}

static PyMethodDef HT_VARIANT(_methods)[] = {
{"increment", (PyCFunction)HT_VARIANT(_increment), METH_VARARGS,
Expand All @@ -929,6 +940,21 @@ static PyMethodDef HT_VARIANT(_methods)[] = {
{"items", (PyCFunction)HT_VARIANT(_HT_iter_KV), METH_NOARGS,
"Iterates over all key-value pairs."
},
{"iteritems", (PyCFunction)HT_VARIANT(_HT_iter_KV), METH_NOARGS,
"Iterates over all key-value pairs."
},
{"keys", (PyCFunction)HT_VARIANT(_HT_iter_K), METH_NOARGS,
"Iterates over all keys."
},
{"iterkeys", (PyCFunction)HT_VARIANT(_HT_iter_K), METH_NOARGS,
"Iterates over all keys."
},
{"values", (PyCFunction)HT_VARIANT(_HT_iter_V), METH_NOARGS,
"Iterates over all non-zero counts."
},
{"itervalues", (PyCFunction)HT_VARIANT(_HT_iter_V), METH_NOARGS,
"Iterates over all non-zero counts."
},
{"update", (PyCFunction)HT_VARIANT(_update), METH_VARARGS,
"Adds all pairs from another counter, or adds all items from an iterable."
},
Expand Down

0 comments on commit 6032dc7

Please sign in to comment.