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
|
#/***************************************************************************
# flisthash.h
# -----------
# begin : sam nov 2 2002
# copyright : (C) 1992-2004 by Fabian Padilla
# email : fp@bridgethink.com
# ***************************************************************************/
# /***************************************************************************
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the Foundation Public License as published by *
# * bridgethink sarl; either version 2 of the License, or *
# * (at your option) any later version. *
# * *
# ***************************************************************************/
#
# from C++ to python by Jeremy Zurcher <jeremy@asynk.ch>
#
# TODO it could maybe be a D={}, sounds faster and easier....
"""/** Special list that manage the link between Hash value and the evenData pointer.
function : maintain a list of SINGLE FstringHash ( single is define by HashValue)
description :
- search : search for an existing FstringHash with the same hashValue
- addOrGet and addOrRemove only add methods available. Then only single hashValue exist.
*@author Fabian Padilla
*/"""
__all__ = ["FlistHash"]
from flist import Flist
from fstringhash import FstringHash
class FlistHash(Flist):
""" This class is a wrapper arounf the flist class, providing some search capabilities"""
def __init__(self):
Flist.__init__(self) # force constructor
def __str__(self):
return "\t"+Flist.__str__(self)+\
"FlistHash - (null)\n"
#def SearchPos(self,fstringHash):
# """/** Search the position of the stringhash name.
# Return the position in the list or None if not found. */"""
# ret = None
# print self.L
# for I in self.L:
# if I.equals(fstringHash):
# return self.L.index(I)
# return ret
def Search(self,fstringHash):
"""/** Search if same name (stringhash) exist */"""
for I in self.L:
if I.equals(fstringHash):
return I
return None
def addOrGet(self,fstringHash):
"""/** If the same name exist in the list then get it from the list.
If not, add stringhash in the list then return None. */"""
pos = self.Search(fstringHash)
if pos <> None:
return pos
self.add(fstringHash)
return None
def addOrRemove(self,fstringHash):
"""/** If the same name exist in the list, then removes and return it.
If not, then add to the list and return None. */"""
pos = self.Search(fstringHash)
if pos <> None:
self.L.remove(pos)
return pos
self.add(fstringHash)
return None
if __name__ == '__main__':
import unittest
from OSconfig import START_ELEMENTS
List = FlistHash()
str=[FstringHash(),FstringHash(),FstringHash(),FstringHash(),FstringHash()]
STR=["TEST", "TEST1", "TEST", "TEST1", "TEST2"]
str[0].setString(STR[0])
str[1].setString(STR[1])
str[2].setString(STR[2])
str[3].setString(STR[3])
str[4].setString(STR[4])
class FlistHashTestCase(unittest.TestCase):
def test1HashAddOrGet(self):
self.assertEquals(List.addOrGet(str[0]),None)
self.assertEquals(List.addOrGet(str[1]),None)
self.assertEquals(List.addOrGet(str[2]).equals(str[2]),True)
self.assertEquals(List.addOrGet(str[3]).equals(str[3]),True)
self.assertEquals(List.addOrGet(str[4]),None)
def test2HashAddOrRemove(self):
self.assertEquals(List.addOrRemove(str[0]).equals(str[0]),True)
self.assertEquals(List.addOrRemove(str[1]).equals(str[1]),True)
self.assertEquals(List.addOrRemove(str[2]),None)
self.assertEquals(List.addOrRemove(str[3]),None)
self.assertEquals(List.addOrRemove(str[4]).equals(str[4]),True)
self.assertEquals(List.addOrRemove(str[2]).equals(str[2]),True)
self.assertEquals(List.addOrRemove(str[3]).equals(str[3]),True)
def test3HashSearch(self):
for I in range (START_ELEMENTS+10):
buffer = "TEST%d"%I
str1 = FstringHash()
str1.setString(buffer)
self.assertEquals(List.addOrGet( str1),None)
buffer = "TEST%d"%(START_ELEMENTS-10)
str1 = FstringHash()
str1.setString(buffer)
self.assertEquals(List.Search(str1).getString(),"TEST%d"%(START_ELEMENTS-10))
unittest.main()
|