Python Boost无序地图打印程序正在多次打印值以及不需要的值
我正在尝试为boost::unordered_map添加一台漂亮的打印机,方法是在现有的boost漂亮打印机列表中添加一台新打印机,如下所示: 但是,当我尝试打印地图时,很多值会被打印多次,并且会出现一些不需要的字符。我不确定还应该对现有代码进行哪些修改,使其只打印一次值,而不打印任何不需要的字符。下面是我在myMap.cpp中更新myMap的循环:Python Boost无序地图打印程序正在多次打印值以及不需要的值,python,boost,gdb,pretty-print,unordered-map,Python,Boost,Gdb,Pretty Print,Unordered Map,我正在尝试为boost::unordered_map添加一台漂亮的打印机,方法是在现有的boost漂亮打印机列表中添加一台新打印机,如下所示: 但是,当我尝试打印地图时,很多值会被打印多次,并且会出现一些不需要的字符。我不确定还应该对现有代码进行哪些修改,使其只打印一次值,而不打印任何不需要的字符。下面是我在myMap.cpp中更新myMap的循环: for (int i=0; i<=10; i++) { string s = to_string(i); myMap[s] = i
for (int i=0; i<=10; i++)
{
string s = to_string(i);
myMap[s] = i;
}
我得到的结果如下:
$1 = boost::unordered::detail = {"1" = 1, "0" = 0, "2" = 2, "1" = 1, "0" = 0, "3" = 3,
"2" = 2, "1" = 1, "0" = 0, "4" = 4, "3" = 3, "2" = 2, "1" = 1, "0" = 0, "5" = 5, "4" = 4,
"3" = 3, "2" = 2, "1" = 1, "0" = 0, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2, "1" = 1,
"0" = 0, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2, "1" = 1,
"0" = 0, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2,
"1" = 1, "0" = 0, "9" = 9, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4,
"3" = 3, "2" = 2, "1" = 1, "0" = 0,
" \204`", '\000' <repeats 13 times>, "\321\n\002", '\000' <repeats 181 times>... = 0,
"9" = 9, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2,
"1" = 1, "0" = 0}
$1=boost::unordered::detail={“1”=1,“0”=0,“2”=2,“1”=1,“0”=0,“3”=3,
"2" = 2, "1" = 1, "0" = 0, "4" = 4, "3" = 3, "2" = 2, "1" = 1, "0" = 0, "5" = 5, "4" = 4,
"3" = 3, "2" = 2, "1" = 1, "0" = 0, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2, "1" = 1,
"0" = 0, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2, "1" = 1,
"0" = 0, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2,
"1" = 1, "0" = 0, "9" = 9, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4,
"3" = 3, "2" = 2, "1" = 1, "0" = 0,
“\204`”、“\000”、“\321\n\002”、“\000”…=0,
"9" = 9, "8" = 8, "7" = 7, "10" = 10, "6" = 6, "5" = 5, "4" = 4, "3" = 3, "2" = 2,
"1" = 1, "0" = 0}
我的pretty printer python代码如下所示:
@_register_printer
class BoostUnorderedMap:
"Pretty Printer for boost::unordered_map"
printer_name = 'boost::unordered_map'
version = '1.40'
type_name_re = '^boost::unordered::unordered_map<.*>$'
class _iterator:
def __init__(self,fields):
type_1 = fields.val.type.template_argument(0)
type_2 = fields.val.type.template_argument(1)
self.buckets = fields.val['table_']['buckets_']
self.bucket_count = fields.val['table_']['bucket_count_']
self.bucket_size = fields.val['table_']['size_']
self.current_bucket = 0
self.value = 0
self.iteration = 0
self.temp_node = 0
self.bucket_to_start = 0
pair = "std::pair<%s const, %s>" % (type_1, type_2)
self.pair_pointer = gdb.lookup_type(pair).pointer()
self.base_pointer = gdb.lookup_type("boost::unordered::detail::value_base< %s >" % pair).pointer()
self.node_pointer = gdb.lookup_type("boost::unordered::detail::ptr_node<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> >").pointer()
self.node = self.buckets[self.current_bucket]['next_']
def __iter__(self):
return self
def next(self):
while not self.node:
self.current_bucket = self.current_bucket + 1
if self.current_bucket >= self.bucket_count:
raise StopIteration
self.node = self.buckets[self.current_bucket]['next_']
iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()
self.node = self.node['next_']
return ('%s' % iterator['first'], iterator['second'])
def __init__(self, val):
self.val = val
def children(self):
return self._iterator(self)
def to_string(self):
return "boost::unordered::detail"
return ('%s' % iterator['first'], iterator['second'])
@\u寄存器\u打印机
类boostUnderedMap:
“用于boost::无序映射的漂亮打印机”
打印机名称='boost::无序\u映射'
版本='1.40'
键入“^boost::unordered::unordered\u map$”
类_迭代器:
定义初始化(自我,字段):
type_1=fields.val.type.template_参数(0)
type_2=fields.val.type.template_参数(1)
self.bucket=fields.val['table\']['bucket\']
self.bucket\u count=fields.val['table\u']['bucket\u count\u']
self.bucket\u size=fields.val['table\u']['size\u']
自当前_bucket=0
self.value=0
self.iteration=0
self.temp_节点=0
self.bucket\u to\u start=0
pair=“std::pair”%(类型1,类型2)
self.pair\u pointer=gdb.lookup\u类型(pair.pointer)()
self.base\u pointer=gdb.lookup\u type(“boost::unordered::detail::value\u base<%s>“%pair”)。指针()
self.node_pointer=gdb.lookup_type(“boost::unordered::detail::ptr_node”).pointer()
self.node=self.bucket[self.current\u bucket]['next\u']
定义(自我):
回归自我
def next(自我):
而不是self.node:
self.current\u bucket=self.current\u bucket+1
如果self.current\u bucket>=self.bucket\u count:
提出停止迭代
self.node=self.bucket[self.current\u bucket]['next\u']
迭代器=self.node.cast(self.node\u指针).cast(self.base\u指针).cast(self.pair\u指针).dereference()
self.node=self.node['next_u']
返回(“%s”%iterator['first'],iterator['second']))
定义初始值(self,val):
self.val=val
def儿童(自我):
返回self.\u迭代器(self)
def到_字符串(自):
返回“boost::unordered::detail”
如果您有任何帮助,我们将不胜感激。如果不进一步了解Boost的内部结构,就很难确切地知道出了什么问题。如果是我,我可能会破解“下一步”方法,添加一些调试打印,看看哪里出了问题 总之,在Python代码中有一件事很突出。现在返回的子元素如下所示:
@_register_printer
class BoostUnorderedMap:
"Pretty Printer for boost::unordered_map"
printer_name = 'boost::unordered_map'
version = '1.40'
type_name_re = '^boost::unordered::unordered_map<.*>$'
class _iterator:
def __init__(self,fields):
type_1 = fields.val.type.template_argument(0)
type_2 = fields.val.type.template_argument(1)
self.buckets = fields.val['table_']['buckets_']
self.bucket_count = fields.val['table_']['bucket_count_']
self.bucket_size = fields.val['table_']['size_']
self.current_bucket = 0
self.value = 0
self.iteration = 0
self.temp_node = 0
self.bucket_to_start = 0
pair = "std::pair<%s const, %s>" % (type_1, type_2)
self.pair_pointer = gdb.lookup_type(pair).pointer()
self.base_pointer = gdb.lookup_type("boost::unordered::detail::value_base< %s >" % pair).pointer()
self.node_pointer = gdb.lookup_type("boost::unordered::detail::ptr_node<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> >").pointer()
self.node = self.buckets[self.current_bucket]['next_']
def __iter__(self):
return self
def next(self):
while not self.node:
self.current_bucket = self.current_bucket + 1
if self.current_bucket >= self.bucket_count:
raise StopIteration
self.node = self.buckets[self.current_bucket]['next_']
iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()
self.node = self.node['next_']
return ('%s' % iterator['first'], iterator['second'])
def __init__(self, val):
self.val = val
def children(self):
return self._iterator(self)
def to_string(self):
return "boost::unordered::detail"
return ('%s' % iterator['first'], iterator['second'])
但是,使用此API的通常方法是将映射的键和值作为单独的子项返回,然后使用返回“map”的display\u hint方法。看
在下一种方法中,您可以很容易地使用“yield”实现这一点