summaryrefslogtreecommitdiff
path: root/components/rtgui/utils/perfect_hash/example-Python/PerfHash.py
diff options
context:
space:
mode:
authorGrissiom <[email protected]>2013-02-01 10:27:10 +0800
committerGrissiom <[email protected]>2013-02-01 10:27:10 +0800
commitf06c70feed0941b5bceedcecf89e9aa22034aca8 (patch)
tree89fd3a989b4d88fc72b39eaad6567de3a3d23874 /components/rtgui/utils/perfect_hash/example-Python/PerfHash.py
parent4f02e67bef7eacee5102d7730356663d9c889e15 (diff)
sync with rtgui-0.6
Diffstat (limited to 'components/rtgui/utils/perfect_hash/example-Python/PerfHash.py')
-rw-r--r--components/rtgui/utils/perfect_hash/example-Python/PerfHash.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/components/rtgui/utils/perfect_hash/example-Python/PerfHash.py b/components/rtgui/utils/perfect_hash/example-Python/PerfHash.py
new file mode 100644
index 0000000000..49131c2b63
--- /dev/null
+++ b/components/rtgui/utils/perfect_hash/example-Python/PerfHash.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+"""
+This example shows how to use the class PerfHash.
+
+This class is designed for creating perfect hash tables at run time,
+which should be avoided, in particulat inserting new keys is
+prohibitively expensive since a new perfect hash table needs to be
+constructed. However, this class can be usefull for testing.
+
+For practical programming purposes in Python the class PerfHash
+should never be used because Python's built-in dictionary is very
+efficient and always faster than PerfHash.
+"""
+
+import sys
+
+sys.path.append('..')
+from perfect_hash import PerfHash
+
+
+month = dict(zip('jan feb mar apr may jun jul aug sep oct mov dec'.split(),
+ range(1, 13)))
+
+d = PerfHash(month)
+
+
+for m in month:
+ assert month[m] == d[m]
+
+d[True] = False
+
+assert d[True] == False
+
+for i in xrange(10): # very expensive
+ d[i] = 2*i*i + 3*i -7
+
+assert d[4] == 37
+
+
+print 'OK'