The LUA implementation of data structure linked list realizes __ garbled problem in C + + list

Source: Internet
Author: User
Tags assert lua prev shallow copy
the LUA implementation of data structure linked list in C + + list implementation

write by nine Days Wild Goose Feather (jtianling)--Blog.csdn.net/vagrxie

Vector I didn't implement it in Lua, it was awkward to implement a list. In fact, it's too much like the C + + standard library list, so it may not be a good feature of Lua, and a bit of a point is that the assignment of the table in Lua is a shallow copy of the assignment, which plays a key role in implementing the list, Otherwise, Lua without pointers becomes impossible to implement a list:

The program also appends an iterator that conforms to the features of the Lua iterator, or if you always use Clistiterator, you may wonder if you're using LUA or not, huh?

The point here is that there is really no debug tool available in Lua, and note that my wording is not even one available, let alone useful. In contrast, C + + has vs+gdb,python with Pydb,bash Bashdb, while Lua has nothing. The LUA for Windows SciTE don't mention how bad it is, Clidebug,xdblua, remdebug and so on I have used, and even the official website mentioned several Ides I have tried, all of the can not use, Probably because of the LUA upgrade (remember SciTE is available), I'm depressed. As a 200-line list code, there is no debugging tool available, it is a nightmare (perhaps not so exaggerated, but it does waste a lot of my simple debugging can send you the problem), alas ... I didn't even want to find myself guilty until I had a good LUA debugging tool. Don't write too many LUA code anymore. At most, print and assert are barely written in small programs. Actually feel the LUA itself for debugging support is very in place, why there is no useful tools. Alas.......... This is the benefit of popularization ... Really have time, the man DIY one uses to forget.


-----In the LUA debugging tool no longer hope, adjust the Lua path settings, casually toss a bit of scite, the results can be normal debugging, faint ..... Sometimes it's so inexplicable and wonderful, I toss 3 days (accurate is 3 days of the night), tried n tools, the results have problems, the results today inexplicable wonderful good, who said, you make up the thought that no bugs in the software will always be in a day inexplicable collapse .... This point in the work I have been deep experience, the problem is that a bug, has not been able to run the normal software how in a day inexplicable good? Oh.

In short, or the Lua debugging tools dissatisfied, at least, there is no can let me use putty to hang on Linux to debug the tools: The total can not be written over there every time in the test back in Windows to try it .... (now it seems like it's the only way)

1 #!/usr/bin/env Lua
2
3--Require "STD"
4
5--Node prototype
6 Cnode ={}
7 function Cnode:new (data, prev, next)
8 Localo ={}
9 O.data = Data
Ten O.prev = prev
One o.next = Next
O.type = "Cnode"
Setmetatable (O, self)
Self.__index = Self
15 returnO
End
17
--Iterator of list prototype like in C + +
Clistiterator ={}
function Clistiterator:new (a)
ASSERT (a ~= nil and
Type (a) = = "Table" and
A.type ~= Nil and
((A.type = = "CList")or(A.type = = "Cnode")),
Argument to new A clistiterator must is a CList object or a Cnode object ")
26
27 Localo ={}
--Give it a type name
O.type = "Clistiterator"
30
--If A is a CList object then create a begin iterator for the object
--If A is a Cnode object then return a iterator point to the node
33ifA.type = "CList"then
O.pos = A.head.next
35ElseIfA.type = "Cnode"then
O.pos = A
37 End
38
Setmetatable (O, self)
Self.__index = Self
41 returnO
The end
43
The function clistiterator:isend ()
45 return notSelf.pos.data
End
47
Clistiterator:cur function ()
49 returnSelf.pos
End
51
function Clistiterator:movenext ()
Self.pos = Self.pos.next
54 returnSelf
End
56
function Clistiterator:moveprev ()
Self.pos = Self.pos.prev
59 returnSelf
The end
61
--List prototype
CList ={}
Clist:createiterator function ()
65 returnClistiterator:new (self)
The end
67
Clist:new function ()
69 Localo ={}
O.head = Cnode:new ()
O.head.prev = O.head
O.head.next = O.head
73
Give it a type Def
O.type = "CList"
Setmetatable (O, self)
Self.__index = Self
78 returnO
The end of
80
Bayi function Clist:insert (it, data)
Nil assert (it ~=, "must pointer where to Insert")
The type (IT) = = "Table", "fisrt Argument must is a clistiterator (now it even not a table)")
Nil assert (Type ~=, "fisrt Argument must be a clistiterator (now it.type = = nil)")
The assert (It.type = = "Clistiterator", "fisrt Argument must be a clistiterator")
86
87 LocalITER = Clistiterator:new (self)
88 Localnode = cnode:new (data, It.pos.prev, It.pos)
It.pos.prev.next = node
It.pos.prev = node
91 returnClistiterator:new (node)
The end
93
Clist:begin function ()
95 returnSelf:createiterator ()
End
97
Clist:end function ()
99 returnClistiterator:new (Self.head)
The end
101
102
Clist:pushfront function (data)
Self:insert (Self:begin (), data)
The end
106
Clist:pushback function (data)
108 Self:insert (Self:end (), data)
109 End
110
function Clist:isempty ()
112 returnSelf:begin (). pos = Self:end (). Pos
113 End
114
Clist:erase function (IT)
116 Assert ( notIt.data, "You can ' t erase")
117 It.pos.prev.next = It.pos.next
118 It.pos.next.prev = It.pos.prev
119 it = Nil
The end
121
122 function Clist:popfront ()
123 Assert ( notSelf:isempty (), "Can ' t popfront to a Empty list")
124 Self:erase (Self:begin ())
The end
126
127 function Clist:popback ()
128 Assert ( notSelf:isempty (), "Can ' t popback to a Empty list")
129 Self:erase (Self:end (): MovePrev ())
130 End
131
132 function Clist:clear ()
133 While notSelf:isempty ()Todo
134 Self:erase (Self:begin ())
135 End
136 End
137
138--Redefine global print to support the CList
139 P = _g.print
140 function print (o)
141ifo ~= Nil andType (o) = = "Table" and
O.type ~= Nil andO.type = "CList"then
143--Iterate like in C + + using CList and Clistiterator feature
144 Localit = O:createiterator ()
145 While notIt:isend ()Todo
146 Io.write (It:cur (). Data)
147 Io.write ("")
148 It:movenext ()
149 End
Io.write ("n")
151Else
152 P (o)
153 End
154 End
155
156--Test Pushfront
157 Print ("/ntest:test Pushfront and Popfront")
158 NewList = Clist:new ()
159 Newlist:pushfront (10)
160 Print (NewList)
161 Newlist:pushfront (20)
162 Print (NewList)
163 Newlist:pushfront (30)
164 print (NewList)
165 Newlist:popfront ()
166 Print (NewList)
167 it = Newlist:createiterator ()
Newlist:erase (IT)
169 Print (NewList)
170 Newlist:clear ()
171 Print (NewList)
172
173
174--Test pushback
175 print ("/ntest:test pushback and Popback")
176 newlist = Clist:new ()
177 Newlist:pushback (10)
178 Print (NewList)
179 Newlist:pushback (20)
180 Print (NewList)
181 Newlist:pushback (30)
NewList print ()
183 Newlist:popback ()
184 Print (NewList)
185 Newlist:popfront ()
186 Print (NewList)
187
188
189--Test:insert at begin
190 print ("/ntest:insert at Begin")
191 NewList = Clist:new ()
The IT = Newlist:createiterator ()
193 iter = Newlist:insert (it, 10);
194 io.write ("cur iterator:"). ToString (It.pos.data). "Return iterator:". ToString (Iter.pos.data). "/n")
195 Print (NewList)
196 iter = Newlist:insert (it, 20);
197 Io.write ("cur iterator:"). ToString (It.pos.data). "Return iterator:". ToString (Iter.pos.data). "/n")
198 Print (NewList)
199 iter = Newlist:insert (it, 30);
Io.write ("cur iterator:"). ToString (It.pos.data). "Return iterator:". ToString (Iter.pos.data). "/n")
201 Print (NewList)
202
203--Test:insert at the back
204 Print ("/ntest:insert at Back")
205 NewList = Clist:new ()
206 it = Newlist:createiterator ()
207 it = Newlist:insert (it, 10);
Io.write ("cur iterator:"). ToString (it.pos.data) ... " /n ")
209 it = Newlist:insert (it, 20);
210 Io.write ("cur iterator:"). &nb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.