Commits

jame2981  committed 590dd7b

Reduction python Loop count

  • Participants
  • Parent commits dd3094a

Comments (0)

Files changed (2)

File simplebloom/__init__.py

 
     def generate(self, string):
         ascii = string.encode('utf-8')
-        
-        for salt in self.salts:
-            yield compute_indices(hash_ap(ascii, salt), self.size)
+
+        for value in hash_ap(ascii, self.salts):
+            yield compute_indices(value, self.size)
 
     def add(self, string):
         for idx in self.generate(string):
         return True
 
 if __name__ == "__main__":
-    salts = (
+    # salts must a list
+    salts = [
           0xAAAAAAAA, 0xBBBBBBBB, 0xCCCCCCCC, 0xDDDDDDDD,
           0xEEEEEEEE, 0xFFFFFFFF, 0xAABBCCDD, 0xABCDEFFF
-        )
+        ]
     
     idx = SimpleFilter(5000, salts);
     idx.add('if')

File simplebloom/_bloom.c

 static PyObject * 
 py_hash_ap(PyObject *self, PyObject *args) {
   const char *data;
-  unsigned int data_len;
-  unsigned int salt;
+  PyObject* salts;
 
-  if(!PyArg_ParseTuple(args, "sI", &data, &salt)) {
+  if(!PyArg_ParseTuple(args, "sO!", &data, &PyList_Type, &salts)) {
     return NULL;
   }
 
-  data_len = strlen(data);
+  unsigned int data_len = strlen(data);
+  unsigned int salts_len = (unsigned int)PyList_Size(salts);
+  PyObject* result = PyTuple_New(salts_len);
+  int i;
 
-  return Py_BuildValue("L", hash_ap(data, data_len, salt));
+  for (i = 0; i < salts_len; i++) {
+    PyObject* item = PyList_GetItem(salts, i);
+    PyTuple_SetItem(result, i, 
+		    PyInt_FromLong(hash_ap(data, data_len, PyLong_AsLong(item))));
+  }
+
+  return result;
 }
 
 static PyMethodDef _bllomMethods[] = {