golang 可以推送和弹出的intlist

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 可以推送和弹出的intlist相关的知识,希望对你有一定的参考价值。

package main
//file to implement expanding/shrinking intlist with a map
import (
	"fmt"
	"strconv"
)
type IntList struct {
	list map[int]int
	//for keeping track of placement indices and insertion spots
	record map[string]int
}

//creates an IntList and returns it
func createIntList() IntList {
	lst := IntList{list:make(map[int]int), record:make(map[string]int)}
	lst.record["insert"] = 0
	lst.record["last"] = -1
	return lst
}
//appends a new element to the end of the list, and updates the insertion and last element positions
func (il IntList) push(elem int) {
	il.list[il.record["insert"]] = elem
	il.record["insert"] += 1
	il.record["last"] += 1
}
//removes the last element from the intlist and returns it
func (il IntList) pop() int {
	popped, has := il.list[il.record["last"]]
	if has {
		delete(il.list, il.record["last"])
		il.record["last"] -= 1
		il.record["insert"] -= 1
		return popped
	}
	return 0
}
//gets an element in the inlist by the given int key
func (il IntList) get(key int) int {
	got, has := il.list[key]
	if has {
		return got
	}
	return 0
}
//appends a new integer to the left side of the list, by moving all current elements up by one
func (il IntList) pushleft(elem int) {
	for i:=il.record["last"] ;i>=0;i-- {
		il.list[i+1] = il.list[i]
	}
	il.list[0] = elem
	il.record["last"] += 1
	il.record["insert"] += 1
}

//gets a pretty string form of the IntList
func (il IntList) tostring() string {
	lststr := "[ "
	for key := range il.list {
		lststr += strconv.Itoa(il.list[key]) + " "
	}
	lststr += "]"
	return lststr
}


func main() {
  tester := createIntList()
  tester.push(5)
  tester.push(8)
  tester.pushleft(1)
  fmt.Println(tester)
  fmt.Println(tester.tostring())
}

UIViewController 推送和弹出中的问题

【中文标题】UIViewController 推送和弹出中的问题【英文标题】:Issue in UIViewController push & pop 【发布时间】:2012-06-29 06:01:31 【问题描述】:

我遇到了推送和弹出 UIViewController 的问题。我有两个 UIViewController 说 AB

我写了一个IBAction,其中我从A 推送到B 喜欢,

B *bView=[[B alloc] initWithNibName:@"B" bundle:nil];
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:0.7];
[self.navigationController pushViewController:bView animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

[bView release]; //Problem is here, If I comment this line, it works fine! else it crashes after my some transitions from A [push] B, B [pop] A.

现在在B 我已经写了一个IBAction 来弹出到A,如下所示

[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:0.7];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[self.navigationController popViewControllerAnimated:NO];
[UIView commitAnimations];

我也从B 打电话给[super dealloc]

-(void) dealloc

   [super dealloc];

如果我不释放bView,它将不会调用dealloc,并且可能会产生内存问题。

这是因为我应用了动画造成的吗?

我尝试了所有其他方式,例如autorelease、设置bView=nil; 等,但这些对我不起作用!

任何帮助,建议!

【问题讨论】:

【bView release】没有问题;您的 B 类代码中还有其他问题,您可以使用 NSZombie 来跟踪问题的确切位置。或者您可以粘贴 B 类的代码。 NSZombie 已启用,它显示消息message sent to deallocated reference 它会显示一些内存地址,在控制台上你可以使用“po memory-address”查看是哪个实例导致了这个问题。 【参考方案1】:

尝试在动画完成块内释放bView

您可以使用旧样式+ setAnimationDidStopSelector: 或者如果不需要支持iOS4之前的设备,可以使用动画块animateWithDuration:delay:options:animations:completion:

【讨论】:

以上是关于golang 可以推送和弹出的intlist的主要内容,如果未能解决你的问题,请参考以下文章

如何设置推送和弹出视图控制器流程

以编程方式切换自定义视图(推送和弹出)

UIViewController 推送和弹出中的问题

java 堆栈 - 推送和弹出操作

TabBarController + NavigationController:推送和弹出问题

C-字符推送和弹出操作[关闭]