Module to handle spacemaps

Check-in [886158fbf4]
anonymous

Check-in [886158fbf4]

Overview
Comment:python 2.7 support, simpler conditionals
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256: 886158fbf48c034ee4e82fc554a1313ade434e845100bfdff41654291d6f4180
User & Date: c.kworr@f767742a-4495-3fff-757e-17af2457c78a on 2011-12-19 09:27:51.000
Other Links: branch diff | manifest | tags
Context
2011-12-21
09:18
__ne__ method check-in: 213edd15a6 user: c.kworr@f767742a-4495-3fff-757e-17af2457c78a tags: master, trunk
2011-12-19
09:27
python 2.7 support, simpler conditionals check-in: 886158fbf4 user: c.kworr@f767742a-4495-3fff-757e-17af2457c78a tags: master, trunk
2010-10-14
14:23
first version check-in: bfe3da6658 user: c.kworr@f767742a-4495-3fff-757e-17af2457c78a tags: master, trunk
Changes
1
2
3
4
5
6
7
8
class SpaceMap:
	''' Here we have:
	__bottom - minimum space address possible;
	__top - maximum space address possible;
	__pairs - internal list of ranges;
	__walk_list - internal list of keys.'''

	__slots__ = frozenset(('__bottom', '__pairs', '__top', '__walk_list'))
|







1
2
3
4
5
6
7
8
class SpaceMap(object):
	''' Here we have:
	__bottom - minimum space address possible;
	__top - maximum space address possible;
	__pairs - internal list of ranges;
	__walk_list - internal list of keys.'''

	__slots__ = frozenset(('__bottom', '__pairs', '__top', '__walk_list'))
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
		else:
			return(None)

	def __set(self, name, value):
		'''Internal function to add ranges without folding. Also checks for range correctness.'''
		if type(name) != int or type(value) != int or name >= value:
			return(False)
		if not self.__bottom == None:
			if name < self.__bottom or value < self.__bottom:
				return(False)
		if not self.__top == None:
			if name >= self.__top or value >= self.__top:
				return(False)
		self.__pairs[name] = value
		return True

	def __setitem__(self, name, value):
		'''Adds one more range and tries to fold a space map.'''







|


|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
		else:
			return(None)

	def __set(self, name, value):
		'''Internal function to add ranges without folding. Also checks for range correctness.'''
		if type(name) != int or type(value) != int or name >= value:
			return(False)
		if self.__bottom != None:
			if name < self.__bottom or value < self.__bottom:
				return(False)
		if self.__top != None:
			if name >= self.__top or value >= self.__top:
				return(False)
		self.__pairs[name] = value
		return True

	def __setitem__(self, name, value):
		'''Adds one more range and tries to fold a space map.'''
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

	def __repr__(self):
		'''Returns the inner dict with ranges.'''
		return(repr(self.__pairs))

	def __and__(self, other):
		'''Returns new space map with ranges that appears both in A and B.'''
		if not type(other) == SpaceMap:
			return(False)
		new = {}
		self.rewind()
		other.rewind()
		my_pair = self.pop()
		other_pair = other.pop()
		# if there are no pairs in B - we have nothing to do







|







80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

	def __repr__(self):
		'''Returns the inner dict with ranges.'''
		return(repr(self.__pairs))

	def __and__(self, other):
		'''Returns new space map with ranges that appears both in A and B.'''
		if type(other) != SpaceMap:
			return(False)
		new = {}
		self.rewind()
		other.rewind()
		my_pair = self.pop()
		other_pair = other.pop()
		# if there are no pairs in B - we have nothing to do
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
						all_parts_found = True
		self.clear()
		other.clear()
		return(SpaceMap(new, self.__bottom, self.__top))

	def __sub__(self, other):
		'''Returns new space map with ranges in A not covered by B.'''
		if not type(other) == SpaceMap:
			return(False)
		new = {}
		self.rewind()
		other.rewind()
		my_pair = self.pop()
		other_pair = other.pop()
		# if there are no pairs in B - copy this one to new SpaceMap







|







177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
						all_parts_found = True
		self.clear()
		other.clear()
		return(SpaceMap(new, self.__bottom, self.__top))

	def __sub__(self, other):
		'''Returns new space map with ranges in A not covered by B.'''
		if type(other) != SpaceMap:
			return(False)
		new = {}
		self.rewind()
		other.rewind()
		my_pair = self.pop()
		other_pair = other.pop()
		# if there are no pairs in B - copy this one to new SpaceMap
Modified test.py from [5baa66cc41] to [9cc8316bf6].




1
2
3
4
5
6
7




def test():
	def check(desc, correct, result):
		if correct == result:
			print('PASSED:', desc, '==' , result, '==', correct)
		else:
			print('FAILED:', desc, '==' , result, '!=', correct)

>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
from __future__ import print_function, unicode_literals

from spacemap import SpaceMap

def test():
	def check(desc, correct, result):
		if correct == result:
			print('PASSED:', desc, '==' , result, '==', correct)
		else:
			print('FAILED:', desc, '==' , result, '!=', correct)