Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/thirdparty/odict/ordereddict.py
2992 views
1
# Copyright (c) 2009 Raymond Hettinger
2
#
3
# Permission is hereby granted, free of charge, to any person
4
# obtaining a copy of this software and associated documentation files
5
# (the "Software"), to deal in the Software without restriction,
6
# including without limitation the rights to use, copy, modify, merge,
7
# publish, distribute, sublicense, and/or sell copies of the Software,
8
# and to permit persons to whom the Software is furnished to do so,
9
# subject to the following conditions:
10
#
11
# The above copyright notice and this permission notice shall be
12
# included in all copies or substantial portions of the Software.
13
#
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
# OTHER DEALINGS IN THE SOFTWARE.
22
23
try:
24
from UserDict import DictMixin
25
except ImportError:
26
try:
27
from collections.abc import MutableMapping as DictMixin
28
except ImportError:
29
from collections import MutableMapping as DictMixin
30
31
class OrderedDict(dict, DictMixin):
32
33
def __init__(self, *args, **kwds):
34
if len(args) > 1:
35
raise TypeError('expected at most 1 arguments, got %d' % len(args))
36
try:
37
self.__end
38
except AttributeError:
39
self.clear()
40
self.update(*args, **kwds)
41
42
def clear(self):
43
self.__end = end = []
44
end += [None, end, end] # sentinel node for doubly linked list
45
self.__map = {} # key --> [key, prev, next]
46
dict.clear(self)
47
48
def __setitem__(self, key, value):
49
if key not in self:
50
end = self.__end
51
curr = end[1]
52
curr[2] = end[1] = self.__map[key] = [key, curr, end]
53
dict.__setitem__(self, key, value)
54
55
def __delitem__(self, key):
56
dict.__delitem__(self, key)
57
key, prev, next = self.__map.pop(key)
58
prev[2] = next
59
next[1] = prev
60
61
def __iter__(self):
62
end = self.__end
63
curr = end[2]
64
while curr is not end:
65
yield curr[0]
66
curr = curr[2]
67
68
def __reversed__(self):
69
end = self.__end
70
curr = end[1]
71
while curr is not end:
72
yield curr[0]
73
curr = curr[1]
74
75
def popitem(self, last=True):
76
if not self:
77
raise KeyError('dictionary is empty')
78
if last:
79
key = next(reversed(self))
80
else:
81
key = next(iter(self))
82
value = self.pop(key)
83
return key, value
84
85
def __reduce__(self):
86
items = [[k, self[k]] for k in self]
87
tmp = self.__map, self.__end
88
del self.__map, self.__end
89
inst_dict = vars(self).copy()
90
self.__map, self.__end = tmp
91
if inst_dict:
92
return (self.__class__, (items,), inst_dict)
93
return self.__class__, (items,)
94
95
def keys(self):
96
return list(self)
97
98
setdefault = DictMixin.setdefault
99
update = DictMixin.update
100
pop = DictMixin.pop
101
values = DictMixin.values
102
items = DictMixin.items
103
iterkeys = DictMixin.iterkeys
104
itervalues = DictMixin.itervalues
105
iteritems = DictMixin.iteritems
106
107
def __repr__(self):
108
if not self:
109
return '%s()' % (self.__class__.__name__,)
110
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
111
112
def copy(self):
113
return self.__class__(self)
114
115
@classmethod
116
def fromkeys(cls, iterable, value=None):
117
d = cls()
118
for key in iterable:
119
d[key] = value
120
return d
121
122
def __eq__(self, other):
123
if isinstance(other, OrderedDict):
124
if len(self) != len(other):
125
return False
126
for p, q in zip(self.items(), other.items()):
127
if p != q:
128
return False
129
return True
130
return dict.__eq__(self, other)
131
132
def __ne__(self, other):
133
return not self == other
134
135