WEBVTT 00:00:00.000 --> 00:00:10.000 00:00:10.000 --> 00:00:11.000 00:00:11.000 --> 00:00:15.000 This presentation is delivered by the Stanford Center for Professional 00:00:15.000 --> 00:00:23.000 Development. 00:00:23.000 --> 00:00:25.000 Hey there! Oh, 00:00:25.000 --> 00:00:29.000 we're back. We're back and now we're getting down and dirty. We'll do more pointers, and link 00:00:29.000 --> 00:00:33.000 list, and implementations. It's actually going to be like our life for the next 00:00:33.000 --> 00:00:36.000 week and half. There's just even more implementation, trying to think about how those 00:00:36.000 --> 00:00:37.000 things work on the backside. So 00:00:37.000 --> 00:00:41.000 I will do another alternate implementation of the stack today. 00:00:41.000 --> 00:00:44.000 So we did one based on vector and we're going to do one based on a link list. 00:00:44.000 --> 00:00:47.000 And then I'm going to do a cube based on link list, as well, 00:00:47.000 --> 00:00:48.000 to kind of just mix 00:00:48.000 --> 00:00:51.000 and match a little bit. And then, we're still at the end of the case study, which the 00:00:51.000 --> 00:00:55.000 material is covered in Chapter 9. I probably won't get thorough all of that today. What we 00:00:55.000 --> 00:00:57.000 don't finish today we'll be talking about on Friday, 00:00:57.000 --> 00:01:00.000 and then we'll go on to start talking about map implementation and look at 00:01:00.000 --> 00:01:02.000 binary search trees. How many 00:01:02.000 --> 00:01:05.000 people started [inaudible]? How many of 00:01:05.000 --> 00:01:08.000 you got somewhere feeling good? 00:01:08.000 --> 00:01:09.000 Not just yet. One thing I will 00:01:09.000 --> 00:01:12.000 tell you is that there are two implementations you're working on 00:01:12.000 --> 00:01:16.000 and I listed them in the order of the one that the material we've seen the most, right. 00:01:16.000 --> 00:01:19.000 We've talked about link list more than we have talked about things that are tree and 00:01:19.000 --> 00:01:22.000 heat based. So I listed that one first. But, in fact, actually, in terms of difficulty of coding, 00:01:22.000 --> 00:01:27.000 I think, the second one is little bit more attractable than the first. So 00:01:27.000 --> 00:01:30.000 there's no reason you can't do them in either order if it feels good to you to start on the second one 00:01:30.000 --> 00:01:32.000 before you come back to the first one. 00:01:32.000 --> 00:01:35.000 Or just trade off between working on them if one of them is giving you fits just look 00:01:35.000 --> 00:01:37.000 at the other one to clear your head, 00:01:37.000 --> 00:01:41.000 so just a thought about how to approach it. Okay. 00:01:41.000 --> 00:01:46.000 So I am going to do some coding. I'm going to come back to my slides in a little bit here. 00:01:46.000 --> 00:01:50.000 But to pick up where we left off last time, was we had written a 00:01:50.000 --> 00:01:55.000 vector based implementation of the stack 00:01:55.000 --> 00:01:56.000 abstraction 00:01:56.000 --> 00:02:01.000 and so looking at the main operations here for a stack or push and pop array 00:02:01.000 --> 00:02:04.000 that its mechanism here is just to add to the end of the vector that we've 00:02:04.000 --> 00:02:08.000 got. So I'm letting the vector handle all the growing, and resizing, 00:02:08.000 --> 00:02:11.000 and memory, and manipulations that are needed for that. 00:02:11.000 --> 00:02:14.000 And then, when asked to pop one, we just take the one that was last most added, 00:02:14.000 --> 00:02:16.000 which will be at the far end of the array. 00:02:16.000 --> 00:02:19.000 And so given the way vector behaves, knowing that we know it's a continuous 00:02:19.000 --> 00:02:21.000 array based 00:02:21.000 --> 00:02:25.000 backing behind it, then we're getting this O of one access to that last element 00:02:25.000 --> 00:02:27.000 to either add a new one down there 00:02:27.000 --> 00:02:30.000 or to retrieve one and remove it from the end 00:02:30.000 --> 00:02:33.000 so that push and pop operations will both being in constant time no matter how many elements are in the 00:02:33.000 --> 00:02:36.000 stack, 10, 15, 6 million, 00:02:36.000 --> 00:02:40.000 the little bit of work we're doing is affecting only the far end of the array 00:02:40.000 --> 00:02:44.000 and not causing the rest of the configured storage to be jostled whatsoever so 00:02:44.000 --> 00:02:46.000 pretty easy to get up and running. 00:02:46.000 --> 00:02:50.000 So in general, once you have one abstraction you can start leveraging it into 00:02:50.000 --> 00:02:52.000 building other attractions and the vector manages a lot of the things that 00:02:52.000 --> 00:02:56.000 any array based implementation would need. So rather than using a raw array, 00:02:56.000 --> 00:02:57.000 there's very little reason to kind of, 00:02:57.000 --> 00:03:00.000 you know, ditch vector and go the straight raw array in this case because all it basically does 00:03:00.000 --> 00:03:03.000 it open up opportunities for error and kind of management and 00:03:03.000 --> 00:03:08.000 the kind of efficiency you can gain back by removing vectors intermediate 00:03:08.000 --> 00:03:11.000 stuff is really not very profound so not worth, 00:03:11.000 --> 00:03:12.000 probably, 00:03:12.000 --> 00:03:13.000 taking on. 00:03:13.000 --> 00:03:16.000 What we do is consider the only link limitation. We had talked about a link 00:03:16.000 --> 00:03:20.000 list for a vector and we kind of discarded it as likely not to lead anywhere 00:03:20.000 --> 00:03:21.000 interesting. 00:03:21.000 --> 00:03:24.000 I actually, am going to fully pursue it for this stack because it actually turns out that a link list 00:03:24.000 --> 00:03:28.000 is a good fit for what the behavior that a stack needs in terms of the 00:03:28.000 --> 00:03:29.000 manipulations and flexibility. 00:03:29.000 --> 00:03:33.000 So if I have a stack of enterers where I push 10, 20, and 30. I'm going to 00:03:33.000 --> 00:03:37.000 do that same diagram we did for the vector one as I did for the stack is 00:03:37.000 --> 00:03:38.000 00:03:38.000 --> 00:03:40.000 one possibility 00:03:40.000 --> 00:03:42.000 is that I put them in the list 00:03:42.000 --> 00:03:47.000 in this order. 00:03:47.000 --> 00:03:50.000 Another possibility 00:03:50.000 --> 00:03:51.000 is that I put them in, 00:03:51.000 --> 00:03:58.000 in this order. We'll call this Strategy A; we'll call this Strategy B. 00:03:58.000 --> 00:04:01.000 They seem, you know, fairly symmetric, right. 00:04:01.000 --> 00:04:05.000 If they're going to be a strong reason that 00:04:05.000 --> 00:04:09.000 leaves us to prefer Strategy A over B, or are they just equally 00:04:09.000 --> 00:04:12.000 easy to get the things done that we need to do? 00:04:12.000 --> 00:04:16.000 Anybody want to make an argument for me. 00:04:16.000 --> 00:04:18.000 Help me out. Last in, first out. 00:04:18.000 --> 00:04:22.000 Last in, first out. 00:04:22.000 --> 00:04:25.000 [Inaudible]. Be careful here. So 10, 20, 30 means the way the stack works is like 00:04:25.000 --> 00:04:26.000 this, 10, 00:04:26.000 --> 00:04:28.000 20, 30 00:04:28.000 --> 00:04:31.000 and so it's at the top that we're interested in, right? Um hm. 00:04:31.000 --> 00:04:34.000 That the top is where activity is taking place. Oh. 00:04:34.000 --> 00:04:37.000 So we want to be adding things and losing from the top. So do we want to put the top at 00:04:37.000 --> 00:04:41.000 the end of our list or at the front of our list? 00:04:41.000 --> 00:04:46.000 Which is the easier to access in the link list design? [Inaudible]. 00:04:46.000 --> 00:04:48.000 The front, right? 00:04:48.000 --> 00:04:52.000 So in the array format we prefer this, putting the top of the vector on the far end 00:04:52.000 --> 00:04:54.000 because then there is the only jostling of 00:04:54.000 --> 00:04:58.000 that. But needing access to the top here in the front is actually going to be the better way to get the link 00:04:58.000 --> 00:05:00.000 list strategy 00:05:00.000 --> 00:05:04.000 you work in. Let me actually go through both the push and the pop to convince 00:05:04.000 --> 00:05:05.000 you why this is true. 00:05:05.000 --> 00:05:09.000 So if I have access in the 10, 20, 30 case I could actually keep a separate 00:05:09.000 --> 00:05:12.000 additional pointer, one to the front and one to the back. So it could actually 00:05:12.000 --> 00:05:13.000 become maintaining at 00:05:13.000 --> 00:05:15.000 all times as pointing to the back. If I 00:05:15.000 --> 00:05:19.000 did that, right, then subsequent push up to a 40, it's pretty easy to tack on a 40. 00:05:19.000 --> 00:05:20.000 So it turns out it would be that 00:05:20.000 --> 00:05:24.000 the adding it's the push operation that actually isn't really a problem on this 00:05:24.000 --> 00:05:26.000 Strategy A. We can still kind of easily, and over 00:05:26.000 --> 00:05:30.000 time, if we have that tail pointer just tack one on to the end. As 00:05:30.000 --> 00:05:32.000 for the pop operation, 00:05:32.000 --> 00:05:33.000 00:05:33.000 --> 00:05:34.000 if I have to pop that 30, 00:05:34.000 --> 00:05:39.000 I need to adjust that tail pointer and move it back a cell. And that's where we 00:05:39.000 --> 00:05:41.000 start to get into trouble. If I have a pointer to 00:05:41.000 --> 00:05:45.000 the tail cell there is no mechanism in the singly linked list to back 00:05:45.000 --> 00:05:46.000 up. If I 00:05:46.000 --> 00:05:48.000 say it's time to pop 30, 00:05:48.000 --> 00:05:49.000 then I would actually need 00:05:49.000 --> 00:05:52.000 to delete the 30, get the value out, but then I need to update this tail pointer to 00:05:52.000 --> 00:05:55.000 point to 20, and 30 doesn't know 00:05:55.000 --> 00:05:57.000 where 20 is, 30's oblivious about these things. 00:05:57.000 --> 00:06:00.000 And so the only way to find 20 in singly linked list would be go back to the very 00:06:00.000 --> 00:06:03.000 beginning, walk your way down, and find the one that pointed to the cell you just took out of 00:06:03.000 --> 00:06:05.000 the list 00:06:05.000 --> 00:06:08.000 and that's going to be an operation we want to avoid. 00:06:08.000 --> 00:06:11.000 In the Strategy B case, right, adding a cell, this 00:06:11.000 --> 00:06:15.000 means putting a 40 in allocating a new cell and then attaching it, 00:06:15.000 --> 00:06:18.000 splicing it in, right between wiring in two pointers, and we can do these links in 00:06:18.000 --> 00:06:19.000 constant time, 00:06:19.000 --> 00:06:24.000 and then popping one is taking that front row cell off. Easy to get to, easy to update, and 00:06:24.000 --> 00:06:25.000 splice out 00:06:25.000 --> 00:06:29.000 so no traversal, no extra pointers needed. 00:06:29.000 --> 00:06:31.000 The idea that there are, 00:06:31.000 --> 00:06:34.000 you know, 100 or 1,000 elements following the one that's on 00:06:34.000 --> 00:06:36.000 top is irrelevant. It 00:06:36.000 --> 00:06:38.000 doesn't have any impact, whatsoever, on the running time 00:06:38.000 --> 00:06:42.000 to access the front, or stuff another front, or taking it on or off the front. 00:06:42.000 --> 00:06:45.000 So it is Strategy B 00:06:45.000 --> 00:06:47.000 that gives us the best setup when it's 00:06:47.000 --> 00:06:50.000 in the back of the linked list. 00:06:50.000 --> 00:06:55.000 So let me go ahead and do it. I think it's kind of 00:06:55.000 --> 00:06:58.000 good to see. Oh, like, what is it like to just write code and make stuff work. 00:06:58.000 --> 00:07:04.000 And so I'm kind of fond of this and you can tell me. 00:07:04.000 --> 00:07:07.000 I make a cell C 00:07:07.000 --> 00:07:08.000 it has an L type 00:07:08.000 --> 00:07:12.000 value and it has a soft T next 00:07:12.000 --> 00:07:15.000 right there. 00:07:15.000 --> 00:07:19.000 And then I have a pointer to the front linked list cell. 00:07:19.000 --> 00:07:21.000 So in addition, you know, 00:07:21.000 --> 00:07:25.000 it might that in order to make something like size operate 00:07:25.000 --> 00:07:26.000 efficiently, 00:07:26.000 --> 00:07:27.000 I might also want to catch the 00:07:27.000 --> 00:07:30.000 number of cells that update, or added or renew. I can also decide to 00:07:30.000 --> 00:07:33.000 just leave it out for now. Maybe 00:07:33.000 --> 00:07:36.000 I'll actually even change this to be the easier function, right, which 00:07:36.000 --> 00:07:38.000 is empty form. 00:07:38.000 --> 00:07:40.000 That way I don't even have to go down that road 00:07:40.000 --> 00:07:44.000 for now. 00:07:44.000 --> 00:07:46.000 Go back over here to 00:07:46.000 --> 00:07:49.000 this side and I've got to make everything now consistent with what's going on 00:07:49.000 --> 00:07:50.000 there. 00:07:50.000 --> 00:07:51.000 Okay. 00:07:51.000 --> 00:07:54.000 Let's start off with our constructor, which is a good place to make sure you get 00:07:54.000 --> 00:07:56.000 into a valid state. 00:07:56.000 --> 00:07:59.000 In this case, we're going to use the empty list so the head pointer points and tells us 00:07:59.000 --> 00:08:03.000 we have no cells whatsoever in the list. So we don't pre allocate anything, 00:08:03.000 --> 00:08:04.000 we don't get ready. 00:08:04.000 --> 00:08:07.000 What's kind of nice with this linked list is to allocate on demand 00:08:07.000 --> 00:08:10.000 each individual cell that's asked to be added to the list. 00:08:10.000 --> 00:08:13.000 The delete operation actually does show we need to do some work. I'm actually not 00:08:13.000 --> 00:08:15.000 going to implement it but I'm going to mention here that I would need to delete the 00:08:15.000 --> 00:08:16.000 entire list, 00:08:16.000 --> 00:08:19.000 which would involve, either iterates or recursion my way down to kind of 00:08:19.000 --> 00:08:21.000 do all the work. 00:08:21.000 --> 00:08:25.000 And then I changed this from size to iterate. 00:08:25.000 --> 00:08:28.000 I'd like to know if my list is empty. 00:08:28.000 --> 00:08:30.000 So I can just test for head 00:08:30.000 --> 00:08:33.000 is equal, equal 00:08:33.000 --> 00:08:36.000 to null. If it's null we have no list. Let 00:08:36.000 --> 00:08:41.000 me work on push before I work on pop. I'll 00:08:41.000 --> 00:08:45.000 turn them around. The push operation is, make myself a new cell. All 00:08:45.000 --> 00:08:47.000 right, let's go through the steps for that. New 00:08:47.000 --> 00:08:50.000 cell equals new cell 00:08:50.000 --> 00:08:54.000 T. [Inaudible] the size to hold a value 00:08:54.000 --> 00:08:56.000 and an X link. I'm going to set 00:08:56.000 --> 00:08:59.000 the new styles val to be the 00:08:59.000 --> 00:09:01.000 perimeter that came in. 00:09:01.000 --> 00:09:05.000 And now, I'm going to splice this guy onto the front of my list. 00:09:05.000 --> 00:09:10.000 So the outgoing pointer I'm doing first to the new cell is allocated and 00:09:10.000 --> 00:09:15.000 leads to what was previously the front row cell. 00:09:15.000 --> 00:09:19.000 And then it is now updated to point to this 00:09:19.000 --> 00:09:21.000 one. We've got pointer wired, one value 00:09:21.000 --> 00:09:23.000 assigned, and we've got a new 00:09:23.000 --> 00:09:28.000 cell on the front. The 00:09:28.000 --> 00:09:29.000 inverse of that, 00:09:29.000 --> 00:09:32.000 taking something off my list, I'm going to need 00:09:32.000 --> 00:09:35.000 to kind of detach that front row cell, delete its memory, get its value, things like 00:09:35.000 --> 00:09:38.000 that. So the error checking is still the same at the very beginning. Like, if I've 00:09:38.000 --> 00:09:41.000 got an empty staff I want to be sure I don't just kind of do reference no 00:09:41.000 --> 00:09:42.000 and get into 00:09:42.000 --> 00:09:48.000 some bad situations. So I report that error back using error, which will halt the program 00:09:48.000 --> 00:09:49.000 there. 00:09:49.000 --> 00:09:51.000 Otherwise, right, 00:09:51.000 --> 00:09:52.000 the element on 00:09:52.000 --> 00:09:57.000 top is the one that's in the value field of the head cell. So knowing that head's 00:09:57.000 --> 00:09:59.000 null is a safety reference to do there. 00:09:59.000 --> 00:10:03.000 And then I'm going to go ahead and 00:10:03.000 --> 00:10:06.000 get a hold of what that thing is and I'm 00:10:06.000 --> 00:10:09.000 going update 00:10:09.000 --> 00:10:13.000 the head to point to the cell after it. So splicing it out 00:10:13.000 --> 00:10:14.000 00:10:14.000 --> 00:10:17.000 and then deleting the 00:10:17.000 --> 00:10:20.000 old cell. 00:10:20.000 --> 00:10:23.000 Just take a look at these. Once we start doing 00:10:23.000 --> 00:10:25.000 pointers, right, there's just opportunities for error. 00:10:25.000 --> 00:10:28.000 I feel kind of good about what I did here but I am going to just do a little bit of 00:10:28.000 --> 00:10:30.000 tracing to kind of 00:10:30.000 --> 00:10:33.000 confirm for myself that the most likely situations that get you in trouble with link list is you'll 00:10:33.000 --> 00:10:36.000 handle the mainstream case but somehow forget one of the edge 00:10:36.000 --> 00:10:40.000 cases. Such as, what if the list was totally empty or just had a single cell? 00:10:40.000 --> 00:10:43.000 Is there some special case handling that needs to be dealt with? 00:10:43.000 --> 00:10:45.000 So if I think about it in terms of the push case, 00:10:45.000 --> 00:10:48.000 you know, I might say, well, if there's an existing set of cells, 00:10:48.000 --> 00:10:50.000 so we're using Strategy B here, let me 00:10:50.000 --> 00:10:54.000 erase A so I know what I'm looking at, 00:10:54.000 --> 00:10:58.000 that its head is currently pointing to a set of cells, right, that my 00:10:58.000 --> 00:11:00.000 allocation of the new cell out here, 00:11:00.000 --> 00:11:02.000 right, assigning its value, 00:11:02.000 --> 00:11:07.000 assigning its next field to be where head points two, and then sending head to 00:11:07.000 --> 00:11:08.000 this new guy, it 00:11:08.000 --> 00:11:10.000 looks to be good. 00:11:10.000 --> 00:11:14.000 If I also do that tracing on the - what if the list that we were looking at was 00:11:14.000 --> 00:11:17.000 just null to begin with. 00:11:17.000 --> 00:11:20.000 So there were no cells in there. This is the very first cell. It isn't going to have any kind of trouble 00:11:20.000 --> 00:11:24.000 stumbling over this case. So I would allocate that new cell 40, assign 00:11:24.000 --> 00:11:25.000 its value. 00:11:25.000 --> 00:11:28.000 Set the next field to be what head is, so that just basically sets the 00:11:28.000 --> 00:11:32.000 trail coming out of 40 to be null and then updates the head there. So we've 00:11:32.000 --> 00:11:35.000 produced a single linked list, right, with one cell. 00:11:35.000 --> 00:11:37.000 It looks like 00:11:37.000 --> 00:11:39.000 we're doing okay on the push behaviors. 00:11:39.000 --> 00:11:43.000 Now, the pop behavior, in this sort of case may be relevant to think about, if it's 00:11:43.000 --> 00:11:46.000 totally empty. All right, we come in and we've got an empty cell, 00:11:46.000 --> 00:11:49.000 then the empty test should cause it to error and get down to the rest of the 00:11:49.000 --> 00:11:50.000 code. 00:11:50.000 --> 00:11:52.000 Let's say that B points to 00:11:52.000 --> 00:11:55.000 some sequence of things, 30, 20, 00:11:55.000 --> 00:11:56.000 10, 00:11:56.000 --> 00:12:02.000 that 00:12:02.000 --> 00:12:05.000 taking the head value off and kind of writing it down somewhere, saying, okay, 00:12:05.000 --> 00:12:07.000 30 was the top value. 00:12:07.000 --> 00:12:10.000 The old cell is currently the head so we're 00:12:10.000 --> 00:12:13.000 keeping a temporary on this thing. 00:12:13.000 --> 00:12:17.000 And then head equals head error next, 00:12:17.000 --> 00:12:19.000 which splices out 00:12:19.000 --> 00:12:23.000 the cell around so we no longer have a pointer into this 30. The only place 00:12:23.000 --> 00:12:27.000 we can get back to it is with this temporary variable we assigned right here of old 00:12:27.000 --> 00:12:30.000 and then we delete that 00:12:30.000 --> 00:12:31.000 old to reclaim that storage 00:12:31.000 --> 00:12:33.000 and then our list now is 00:12:33.000 --> 00:12:37.000 the values 20 and 10, which followed it later in the list. To, also, 00:12:37.000 --> 00:12:41.000 do a little check what if it was the very last cell 00:12:41.000 --> 00:12:44.000 in the list that we're trying to pop? Does that 00:12:44.000 --> 00:12:46.000 have any special handling that we have overlooked 00:12:46.000 --> 00:12:50.000 if we just have a ten here with a null after it? I'll go 00:12:50.000 --> 00:12:55.000 through the process of writing down the ten. 00:12:55.000 --> 00:12:59.000 Assigning old to where head is, 00:12:59.000 --> 00:13:05.000 and assigning head-to-head error next, where head error of next cell is null 00:13:05.000 --> 00:13:08.000 but pointing to that, and then we set our list to null, and then we delete this cell. And so after 00:13:08.000 --> 00:13:11.000 we're done, right, we have the empty list again, the 00:13:11.000 --> 00:13:12.000 head pointer back to null. 00:13:12.000 --> 00:13:16.000 So it seems like we're doing pretty good job. Have a bunch of cells. Have one cell. 00:13:16.000 --> 00:13:25.000 No cells. Different things kind of seem to be going through here doing the right thing. Let's do it. A little bit 00:13:25.000 --> 00:13:34.000 of code on this side. [Inaudible]. Okay. Pop empty cell. I've 00:13:34.000 --> 00:13:37.000 got that. Oh, 00:13:37.000 --> 00:13:40.000 I think I may have proved, oh, it deliberately makes the error, in 00:13:40.000 --> 00:13:41.000 fact. 00:13:41.000 --> 00:13:43.000 That it 00:13:43.000 --> 00:13:48.000 was designed to test on that. [Inaudible] up 00:13:48.000 --> 00:13:52.000 to the end. So let's 00:13:52.000 --> 00:13:54.000 00:13:54.000 --> 00:13:57.000 see if 00:13:57.000 --> 00:14:03.000 we get back a three, two, one out of this guy. Okay. So 00:14:03.000 --> 00:14:08.000 we manage to do okay even on that. 00:14:08.000 --> 00:14:10.000 So looking at this piece of code, all 00:14:10.000 --> 00:14:14.000 right, the two operations we're most interested in, push and pop, right, 00:14:14.000 --> 00:14:19.000 are each oh of one, right. The number of elements out there, right, aren't 00:14:19.000 --> 00:14:21.000 changing anything about its performance. It does a little bit of 00:14:21.000 --> 00:14:23.000 imagination and a little bit of pointer arrangement up here to the front to 00:14:23.000 --> 00:14:24.000 the end 00:14:24.000 --> 00:14:28.000 and so it has a very nice tight allocation strategy, too, which is appealing relative 00:14:28.000 --> 00:14:29.000 to what 00:14:29.000 --> 00:14:33.000 the vector-based strategy was doing. It's like, now, it's doing things in advance on our behalf like 00:14:33.000 --> 00:14:36.000 extending our capacity so that every now and then we had some 00:14:36.000 --> 00:14:39.000 little glitch right when you were doing that big resize operation that happened infrequently, 00:14:39.000 --> 00:14:41.000 you won't have that same 00:14:41.000 --> 00:14:44.000 concern with this one because it does exactly what it needs at any given 00:14:44.000 --> 00:14:47.000 time, which is allocate a single cell, deletes a single cell, 00:14:47.000 --> 00:14:51.000 and also keeping the allocation very tidy in that way. So they'll both 00:14:51.000 --> 00:14:54.000 end of being oh of one. And both of these are 00:14:54.000 --> 00:14:58.000 commonly used viable strategies for how a stack is implemented. Right. Depending on the, 00:14:58.000 --> 00:14:59.000 00:14:59.000 --> 00:15:02.000 you know, just the inclination of the program or they may have decided 00:15:02.000 --> 00:15:05.000 one way was the way to go versus another 00:15:05.000 --> 00:15:07.000 that 00:15:07.000 --> 00:15:09.000 you will see both used in common practice because there really isn't 00:15:09.000 --> 00:15:12.000 one of them that's obviously better or obviously worse, right. 00:15:12.000 --> 00:15:16.000 Now, this uses a little bit more memory per cell, but then you have excess capacity 00:15:16.000 --> 00:15:21.000 and vector has excess capacity but no overhead per cell. 00:15:21.000 --> 00:15:23.000 There's a few other things to kind of think about trading off. You say, well, the code, 00:15:23.000 --> 00:15:27.000 itself, is a little harder to write if you're using only [inaudible]. 00:15:27.000 --> 00:15:30.000 That means it's a little bit more error prone 00:15:30.000 --> 00:15:33.000 and more likely, you'll make a mistake and have to do debug your way through it and the way with the 00:15:33.000 --> 00:15:35.000 vector it was pretty easy to get without tearing out your 00:15:35.000 --> 00:15:36.000 hair. Now, if 00:15:36.000 --> 00:15:40.000 we can 00:15:40.000 --> 00:15:42.000 do 00:15:42.000 --> 00:15:44.000 stack and queue that fast - 00:15:44.000 --> 00:15:45.000 I mean, stack that fast, then you figure queue 00:15:45.000 --> 00:15:47.000 must be 00:15:47.000 --> 00:15:51.000 not so crazy either. Let me 00:15:51.000 --> 00:15:59.000 draw you a picture of queue. 00:15:59.000 --> 00:16:02.000 Okay. 00:16:02.000 --> 00:16:08.000 I've got my 00:16:08.000 --> 00:16:12.000 queue and I can use the 00:16:12.000 --> 00:16:14.000 End queue operation on it to put in a 10, to put 00:16:14.000 --> 00:16:16.000 in a 20, to put in a 30. 00:16:16.000 --> 00:16:18.000 And 00:16:18.000 --> 00:16:20.000 I'm going to try and get back out what was 00:16:20.000 --> 00:16:23.000 front most. 00:16:23.000 --> 00:16:25.000 Okay. So queue is FIFO, first in 00:16:25.000 --> 00:16:27.000 first out, 00:16:27.000 --> 00:16:30.000 and waiting in line, whoever's been in the queue longest is the first one to come 00:16:30.000 --> 00:16:31.000 out, 00:16:31.000 --> 00:16:33.000 very fair handling of things. 00:16:33.000 --> 00:16:37.000 And so if were to think about this in terms of let's go straight across the 00:16:37.000 --> 00:16:39.000 linked list. We just got our head around the link list with the stack. Let's see if there's a link list for the 00:16:39.000 --> 00:16:40.000 queue 00:16:40.000 --> 00:16:44.000 provide the same kind of easy access to the things we need to do the work. 00:16:44.000 --> 00:16:48.000 So it seems like the 00:16:48.000 --> 00:16:51.000 two strategies that we looked at for stack 00:16:51.000 --> 00:16:55.000 are probably the same two to look at for this one, right, which we go in the front 00:16:55.000 --> 00:16:57.000 ways orders. I mean the front of the queue accessible 00:16:57.000 --> 00:17:01.000 in there and reversing our way down to the tail 00:17:01.000 --> 00:17:05.000 or the other way around. Right. I mean, the tail of the queue up 00:17:05.000 --> 00:17:09.000 here in the front 00:17:09.000 --> 00:17:15.000 leading the way to the head of the queue. So this is head - I'll call it 00:17:15.000 --> 00:17:17.000 front of queue, 00:17:17.000 --> 00:17:20.000 and this is back, 00:17:20.000 --> 00:17:24.000 this is the back 00:17:24.000 --> 00:17:27.000 and that's the front. Okay. So that will be GA that will be GB. But first off, 00:17:27.000 --> 00:17:29.000 ask yourself, 00:17:29.000 --> 00:17:30.000 where does the queue do its work? 00:17:30.000 --> 00:17:38.000 Does it do it at the front or does it do it at the back? 00:17:38.000 --> 00:17:41.000 It does it at both. 00:17:41.000 --> 00:17:44.000 But when you're end queuing, right, you're manipulating the back of the queue, adding 00:17:44.000 --> 00:17:47.000 something to the tail end of the line. When 00:17:47.000 --> 00:17:51.000 you are de queuing, you're adding something to the front of the queue. So like unlike the stack where both the 00:17:51.000 --> 00:17:53.000 actions were happening in the same place, and that kind of unified your thinking 00:17:53.000 --> 00:17:56.000 about where to 00:17:56.000 --> 00:17:57.000 allow for that, 00:17:57.000 --> 00:18:00.000 you know, easy access to the top, the one thing the stack cared 00:18:00.000 --> 00:18:03.000 about, but the bottom of the stack, right, was, you know, 00:18:03.000 --> 00:18:04.000 buried and it didn't matter. 00:18:04.000 --> 00:18:06.000 The queue actually needs accesses to both. 00:18:06.000 --> 00:18:07.000 So that 00:18:07.000 --> 00:18:10.000 sort of sounds like that both these strategies, right, have the potential to be 00:18:10.000 --> 00:18:14.000 trouble for us. Because it's like in the link list form, right, you know the idea that 00:18:14.000 --> 00:18:17.000 access to the head is easy, access to the tail that is a little bit more tricky. 00:18:17.000 --> 00:18:20.000 Now, I said we could add a tail pointer. And maybe that's actually going to be part of the 00:18:20.000 --> 00:18:22.000 answer to this, right, 00:18:22.000 --> 00:18:26.000 but as it is, with no head pointers in here, that adding an item to A would 00:18:26.000 --> 00:18:30.000 involve traversing that queue to find the end to put a new one on. 00:18:30.000 --> 00:18:34.000 Similarly, if indeed, the inverse operation is going to be our trouble, which 00:18:34.000 --> 00:18:38.000 is de queuing, we have to kind of walk its way down to find the last element 00:18:38.000 --> 00:18:41.000 in the link list ordering, which is [inaudible] queue. 00:18:41.000 --> 00:18:43.000 So what say we add a 00:18:43.000 --> 00:18:44.000 tail pointer in both cases? So we 00:18:44.000 --> 00:18:46.000 have a head that 00:18:46.000 --> 00:18:48.000 points to here and a tail that points to there, it's 00:18:48.000 --> 00:18:50.000 like that will make our 00:18:50.000 --> 00:18:53.000 problem a little bit easier to deal with. So 00:18:53.000 --> 00:18:55.000 let me look at B first. 00:18:55.000 --> 00:18:58.000 That adding to the back of the queue is easy. We saw how we did that with the stack, 00:18:58.000 --> 00:18:59.000 right, so that 00:18:59.000 --> 00:19:01.000 the End queue 00:19:01.000 --> 00:19:05.000 operation doesn't need that tail pointer and it already kind of wasn't a problem for us. 00:19:05.000 --> 00:19:08.000 Now, the D queue operation would mean, okay, using our tail pointer to know where the 00:19:08.000 --> 00:19:11.000 last close value is tells us it's ten. 00:19:11.000 --> 00:19:14.000 But we have the same problem I had pointed out with the stack, which is, 00:19:14.000 --> 00:19:18.000 we can get to this value, we can return this value, you know, we can delete this 00:19:18.000 --> 00:19:23.000 cell, but what we need to do, also, in the operation, update our tail pointer so a 00:19:23.000 --> 00:19:25.000 subsequent D queue can do its work efficiently. 00:19:25.000 --> 00:19:28.000 And it's that backing up of the tail pointer that 00:19:28.000 --> 00:19:29.000 is the sticky point 00:19:29.000 --> 00:19:33.000 that given that ten doesn't know who points into it, the single link list if very 00:19:33.000 --> 00:19:35.000 asymmetric that way. 00:19:35.000 --> 00:19:37.000 But you only know what's follows you, not what proceeds, 00:19:37.000 --> 00:19:40.000 but backing up this pointer is not easy. It 00:19:40.000 --> 00:19:44.000 involves the reversal of going back to the beginning, walking your way down to find somebody who 00:19:44.000 --> 00:19:46.000 points to the last [inaudible]. 00:19:46.000 --> 00:19:50.000 So it seems like this tail pointer didn't buy us a lot. It helped a little bit, you know, 00:19:50.000 --> 00:19:53.000 to get some of the operation up and running, but in the end, keeping and maintaining 00:19:53.000 --> 00:19:56.000 that tail pointer sort of came back to bite us. 00:19:56.000 --> 00:19:58.000 In the case of the Strategy A, 00:19:58.000 --> 00:20:02.000 the tail pointer gives us immediate access to the back, which we're going to need for 00:20:02.000 --> 00:20:05.000 the end queue. If I go to end queue of 40, in this case, 00:20:05.000 --> 00:20:10.000 then what I'm going to need to do is make a new cell, attach it off of the 00:20:10.000 --> 00:20:11.000 current cell, 00:20:11.000 --> 00:20:13.000 and then update the tail, 00:20:13.000 --> 00:20:17.000 right, to point to that cell while that update's moving forward down the list, 00:20:17.000 --> 00:20:20.000 right. If you're on the third cell and you add a fourth cell, you need to move the tail 00:20:20.000 --> 00:20:24.000 from the third to the fourth. Moving that direction's easy. It's the backing 00:20:24.000 --> 00:20:27.000 up where we got into trouble. So in fact, this 00:20:27.000 --> 00:20:31.000 suggests that Strategy A is the one we can make both these operations 00:20:31.000 --> 00:20:33.000 manipulate in constant time in a way 00:20:33.000 --> 00:20:35.000 that this one got 00:20:35.000 --> 00:20:37.000 us into trouble. 00:20:37.000 --> 00:20:41.000 I think we can do it. 00:20:41.000 --> 00:20:44.000 Let's switch it over. I've got an 00:20:44.000 --> 00:20:49.000 empty queue here. 00:20:49.000 --> 00:20:53.000 Also, it is empty in size. This is the same problem with size, which is either you have to go count them 00:20:53.000 --> 00:20:55.000 or you have to 00:20:55.000 --> 00:20:55.000 00:20:55.000 --> 00:20:57.000 cache the size. 00:20:57.000 --> 00:21:01.000 I will build the same exactly structure, in fact. 00:21:01.000 --> 00:21:11.000 Now, I'll type 00:21:11.000 --> 00:21:12.000 next. It's going to have both a 00:21:12.000 --> 00:21:14.000 head and a tail pointer 00:21:14.000 --> 00:21:18.000 so we can keep track of the two ends we've got going. I think I'm 00:21:18.000 --> 00:21:24.000 missing my [inaudible]. Slip over. 00:21:24.000 --> 00:21:29.000 Okay. And 00:21:29.000 --> 00:21:32.000 then I will make the same comment here about, yeah, you need to delete all cells. 00:21:32.000 --> 00:21:34.000 So I set the head and the tail to null. I'm 00:21:34.000 --> 00:21:41.000 now in my empty state that tells me I've got nothing in the queue, nothing at all, so far. And 00:21:41.000 --> 00:21:48.000 my Y is empty. Oh. 00:21:48.000 --> 00:21:50.000 We'll check that 00:21:50.000 --> 00:21:53.000 return equals, equals null, just like 00:21:53.000 --> 00:21:54.000 it did. In fact, it looks a lot like 00:21:54.000 --> 00:21:55.000 the stack, actually. 00:21:55.000 --> 00:21:59.000 And here I'm going to show you something kind of funny. If I go take 00:21:59.000 --> 00:22:04.000 my stack CCPs pop. 00:22:04.000 --> 00:22:08.000 So if you remember what pop did over here, is it took the front most cell off the 00:22:08.000 --> 00:22:11.000 list. And 00:22:11.000 --> 00:22:12.000 in the case of the queue 00:22:12.000 --> 00:22:16.000 that was the 00:22:16.000 --> 00:22:18.000 popping operation. And 00:22:18.000 --> 00:22:21.000 it turns out the D queue 00:22:21.000 --> 00:22:25.000 operation is exactly the same. 00:22:25.000 --> 00:22:29.000 If we're empty, right, then we want to raise there. Otherwise, we take the head's 00:22:29.000 --> 00:22:29.000 value. 00:22:29.000 --> 00:22:33.000 We move around the head, we delete the old memory. So it's not 00:22:33.000 --> 00:22:36.000 actually the top element. I should, actually, be a little bit more 00:22:36.000 --> 00:22:39.000 careful about my copying and pasting here. I could really call that 00:22:39.000 --> 00:22:41.000 front. It's the front-most element on the top. 00:22:41.000 --> 00:22:43.000 But it's like the exact same mechanics work 00:22:43.000 --> 00:22:48.000 to take a cell off the front in that way. Well, that's sort of 00:22:48.000 --> 00:22:48.000 nice. 00:22:48.000 --> 00:22:51.000 And it's like, yeah, well, since I have some code around I want to go try it. 00:22:51.000 --> 00:22:55.000 My stack, also, kind of does some things useful in push that I might be able to use. It's not 00:22:55.000 --> 00:22:58.000 exactly the same because it's adding it to the front, but it is 00:22:58.000 --> 00:23:00.000 setting up a new cell. So I'm going to 00:23:00.000 --> 00:23:05.000 make a new cell and I set its value. Then, it's attaching 00:23:05.000 --> 00:23:07.000 looks a little different. Let's take a look. 00:23:07.000 --> 00:23:11.000 We omitted a cell, copied the value in, 00:23:11.000 --> 00:23:16.000 now the goal is giving our pointer to the tail, we want to attach onto the tail. 00:23:16.000 --> 00:23:22.000 So we know that it's always going to have a next of null, the new cell. So no 00:23:22.000 --> 00:23:23.000 matter what, 00:23:23.000 --> 00:23:26.000 it will be the new last cell and it defiantly needs that value that tells 00:23:26.000 --> 00:23:28.000 us we're at the end of the list. 00:23:28.000 --> 00:23:32.000 And then we have to attach it to our tail. I'm going to write this code first and it's going to be 00:23:32.000 --> 00:23:35.000 wrong. So just go along with me in this case. 00:23:35.000 --> 00:23:37.000 If the tail's next is the new cell. 00:23:37.000 --> 00:23:40.000 So if I'm wiring in the pointer from the tail onto the cell we have 00:23:40.000 --> 00:23:41.000 there, 00:23:41.000 --> 00:23:48.000 and then we want to update the tail to point to the cell. So 00:23:48.000 --> 00:23:48.000 almost, 00:23:48.000 --> 00:23:53.000 but not quite, everything I need to do. 00:23:53.000 --> 00:23:56.000 Someone want to point out to me what it is I have failed 00:23:56.000 --> 00:24:05.000 to consider in this situation. [Inaudible]. 00:24:05.000 --> 00:24:09.000 It's the what? [Inaudible]. Yeah. ? trying to make null point to something [inaudible]. Exactly. So if my queue is totally empty. So in these cases like 00:24:09.000 --> 00:24:12.000 - one thing you can often think about whenever you see yourself with this 00:24:12.000 --> 00:24:15.000 error, and you're dereferencing something, you have to considered, well, is there a possibility 00:24:15.000 --> 00:24:20.000 that that value is null. And if so I'd better do something 00:24:20.000 --> 00:24:23.000 to protect against it. So in the case of the new cell here, I'm setting aside and 00:24:23.000 --> 00:24:27.000 not clearing that cell. We know it's valid, [inaudible], we've got good memory. 00:24:27.000 --> 00:24:30.000 That part's good but the access to the tail, and that's assuming 00:24:30.000 --> 00:24:34.000 there is a tail, that there is at least one cell already in the queue that the tail 00:24:34.000 --> 00:24:35.000 is pointing to. 00:24:35.000 --> 00:24:37.000 When that's not true, 00:24:37.000 --> 00:24:39.000 this is going to blow up. It's going to try to dereference it. 00:24:39.000 --> 00:24:43.000 So what we can do here is we can just check for that. We can say 00:24:43.000 --> 00:24:45.000 if the queue is empty 00:24:45.000 --> 00:24:48.000 then 00:24:48.000 --> 00:24:50.000 what we want to do is say that head 00:24:50.000 --> 00:24:53.000 equals tail equals new 00:24:53.000 --> 00:24:54.000 cell. 00:24:54.000 --> 00:25:00.000 Otherwise, we've got some existing tail and we 00:25:00.000 --> 00:25:02.000 just need to attach to it. 00:25:02.000 --> 00:25:05.000 Now, this is what I meant about that often there are little bit 00:25:05.000 --> 00:25:10.000 of special cases for certain 00:25:10.000 --> 00:25:12.000 different inputs. In particular, the most common ones are 00:25:12.000 --> 00:25:16.000 an empty list or a single link list being distinguished from a list that has 00:25:16.000 --> 00:25:20.000 two or more elements. So sometimes it's all about - a single link list has problems and 00:25:20.000 --> 00:25:23.000 so does the empty list. In this case, it's exactly the empty list. Like a 00:25:23.000 --> 00:25:27.000 single cell is actually fine, you know, one, ten, 6,000 00:25:27.000 --> 00:25:31.000 all work equally well. It's that empty case that we have to work on 00:25:31.000 --> 00:25:36.000 setting our head and tail to that. So with 00:25:36.000 --> 00:25:38.000 this plan we go back to my 00:25:38.000 --> 00:25:40.000 use of this. And 00:25:40.000 --> 00:25:47.000 stop talking about my stack and change into my queue, 00:25:47.000 --> 00:25:56.000 end queuing instead 00:25:56.000 --> 00:25:58.000 of pushing. [Inaudible]. Oh, 591 errors. 00:25:58.000 --> 00:26:01.000 Well, 00:26:01.000 --> 00:26:15.000 that's really - 591, what is that? [Inaudible]. 00:26:15.000 --> 00:26:17.000 Oh, yeah, this is the year I was going 00:26:17.000 --> 00:26:20.000 to talk about but I 00:26:20.000 --> 00:26:23.000 failed to do. 00:26:23.000 --> 00:26:26.000 So this is the kind of thing you'll get 00:26:26.000 --> 00:26:30.000 from failing to protect your [inaudible] from 00:26:30.000 --> 00:26:31.000 being revisited. 00:26:31.000 --> 00:26:33.000 And so I'll 00:26:33.000 --> 00:26:36.000 be - if I haven't deft this 00:26:36.000 --> 00:26:39.000 funny little symbol I just made up then I want to find it in the 00:26:39.000 --> 00:26:42.000 end deft. So if it came back around and it saw this same header file, it was 00:26:42.000 --> 00:26:45.000 supposed to say I already seen that symbol and I won't do it. But, in fact, I had 00:26:45.000 --> 00:26:47.000 accidentally named one of them 00:26:47.000 --> 00:26:50.000 with a capital H and one with a lowercase H. It says if this symbol's not been 00:26:50.000 --> 00:26:53.000 identified then define this other symbol and keep going. And so the next time it saw 00:26:53.000 --> 00:26:57.000 the header files it said if that this lowercase H hasn't been 00:26:57.000 --> 00:27:00.000 defined, well it hadn't so it made it again and then it kept doing that until it got really 00:27:00.000 --> 00:27:03.000 totally twisted up about it. So I will 00:27:03.000 --> 00:27:06.000 make them match, with any 00:27:06.000 --> 00:27:07.000 luck, 00:27:07.000 --> 00:27:11.000 without the caps lock on. Three times is a charm. 00:27:11.000 --> 00:27:12.000 And then one, two, three 00:27:12.000 --> 00:27:21.000 pop out the other side of queue, as I was hoping. Okay. 00:27:21.000 --> 00:27:24.000 So you know what I'm going to do, actually? I'm actually not going 00:27:24.000 --> 00:27:27.000 to go through the alternate implementation of queue. I'm just saying 00:27:27.000 --> 00:27:30.000 like given that it works so well for stack, right, to have this kind of 00:27:30.000 --> 00:27:34.000 single link list, you know, efficient allocation, it's not surprising that queue, 00:27:34.000 --> 00:27:35.000 like, given to us. And what 00:27:35.000 --> 00:27:37.000 this is, like I said earlier, I said, well, 00:27:37.000 --> 00:27:41.000 you know you can do things with vector, you can do things with 00:27:41.000 --> 00:27:44.000 stack, anything you do with stack you can do with vector if you were just being 00:27:44.000 --> 00:27:48.000 careful. Right. The pushing, and popping, and the queue D queue, are operations that you could express 00:27:48.000 --> 00:27:52.000 in other terms of vector adds and removes and inserts that things. 00:27:52.000 --> 00:27:55.000 But one of the nice things about providing a structure like a stack or queue is it says 00:27:55.000 --> 00:27:58.000 these are the only things you're allowed to add at this end, remove from that end, 00:27:58.000 --> 00:28:02.000 it gives you options as a implementer that don't make sense for the general 00:28:02.000 --> 00:28:06.000 purpose case. That the vector as a link list had some compromises 00:28:06.000 --> 00:28:10.000 that probably weren't worth making. But in the case of stack and queue it actually came out 00:28:10.000 --> 00:28:13.000 very cleanly and very nicely, like this tidy little access to one end, or the 00:28:13.000 --> 00:28:15.000 other end, or both, right, 00:28:15.000 --> 00:28:17.000 was easily managed with a link list and then it didn't have any of the constraints of 00:28:17.000 --> 00:28:20.000 the continuous memory to fight with. 00:28:20.000 --> 00:28:21.000 And so often having a structure 00:28:21.000 --> 00:28:25.000 that actually offers less gives you some different options as an 00:28:25.000 --> 00:28:28.000 implementer because you know you don't have to support access into the middle in 00:28:28.000 --> 00:28:31.000 an efficient way because 00:28:31.000 --> 00:28:33.000 the stack and queue don't let you. They don't let you riffle through the 00:28:33.000 --> 00:28:35.000 middle of the stack or the queue to do things 00:28:35.000 --> 00:28:38.000 and that gives you some freedom as an implementer, which is neat to take advantage 00:28:38.000 --> 00:28:43.000 of. Yes, sir. [Inaudible] in Java 00:28:43.000 --> 00:28:55.000 that, you know, we supposed to use an Array whenever we could because Array uses more 00:28:55.000 --> 00:29:00.000 memory ? Um hm. Does the queue 00:29:00.000 --> 00:29:03.000 and stack use more memory than an Array, for example? So typically they'll be about the same, because of exactly this one thing, which is it allocates tightly so it asks for one cell per entry that's in there, right. And that cell has this overhead in the form of this extra pointer. 00:29:03.000 --> 00:29:06.000 Right. So in effect it means that everything got a little bit bigger because 00:29:06.000 --> 00:29:08.000 everything's carrying a little overhead. 00:29:08.000 --> 00:29:11.000 Okay. So there's a little bit more memory. The thing the Array is doing is it tends to be 00:29:11.000 --> 00:29:15.000 allocating extra slots that aren't being used. So it's not usually tightly allocated 00:29:15.000 --> 00:29:18.000 either. So in any given situation it could be as much as twice as big 00:29:18.000 --> 00:29:19.000 because it had that extra space. 00:29:19.000 --> 00:29:22.000 So kind of in the tradeoff they tend to be in the same 00:29:22.000 --> 00:29:25.000 general range. This one has about twice the space, I think it's about four 00:29:25.000 --> 00:29:28.000 bites it has a four-byte pointer. The thing's much bigger 00:29:28.000 --> 00:29:30.000 and it turns out actually this four bite pointer might be a smaller percentage of 00:29:30.000 --> 00:29:35.000 the overhead and will be a larger amount of the capacity in the excess case. 00:29:35.000 --> 00:29:37.000 So for a lot of things they're going to be about the same range. And then there's 00:29:37.000 --> 00:29:40.000 a few isolated situations where, 00:29:40.000 --> 00:29:43.000 yeah, if you have very big things being stored having a lot of excess capacity 00:29:43.000 --> 00:29:45.000 is likely to be a more expensive 00:29:45.000 --> 00:29:48.000 part of the vector strategy than the link list. But the 00:29:48.000 --> 00:29:51.000 link list does have this built in overhead for every element. 00:29:51.000 --> 00:29:54.000 And so it's not the case where you would say right off the bat, 00:29:54.000 --> 00:29:55.000 well, 00:29:55.000 --> 00:29:58.000 definitely an array over link list because that the allocation space. 00:29:58.000 --> 00:30:01.000 It's like, hey, you have to think about the whole picture. 00:30:01.000 --> 00:30:05.000 That is going to be the whole next lectures. Well, you 00:30:05.000 --> 00:30:06.000 know, it's about tradeoffs. 00:30:06.000 --> 00:30:09.000 It's not that one strategy's always going to be 00:30:09.000 --> 00:30:12.000 the clearly better one. If it is, then we don't need to think about 00:30:12.000 --> 00:30:14.000 anything else. There are times when 00:30:14.000 --> 00:30:16.000 you know 00:30:16.000 --> 00:30:16.000 00:30:16.000 --> 00:30:19.000 that one is just great and there's no reason to think about anything else. And then 00:30:19.000 --> 00:30:20.000 there's other times when 00:30:20.000 --> 00:30:24.000 there are reasons to think about different ways to solve the same problem. 00:30:24.000 --> 00:30:26.000 So let me 00:30:26.000 --> 00:30:27.000 propose a case study 00:30:27.000 --> 00:30:31.000 that has some 00:30:31.000 --> 00:30:34.000 meat. You may think along the way, but you're going to really have to pretend that 00:30:34.000 --> 00:30:38.000 this is going to be relevant at first because it's going to seem like you've been 00:30:38.000 --> 00:30:41.000 transferred back to 00:30:41.000 --> 00:30:44.000 1970, which was a very bad year, long before you born. 00:30:44.000 --> 00:30:49.000 I want you to think about the idea of a text setter. So Microsoft Word, 00:30:49.000 --> 00:30:50.000 or 00:30:50.000 --> 00:30:53.000 you know your email send window, or 00:30:53.000 --> 00:30:57.000 BB edit, or X code. All these things, right, have some backing of 00:30:57.000 --> 00:31:00.000 what usually is called a buffer, 00:31:00.000 --> 00:31:02.000 a buffer, sort of a funny word, right, 00:31:02.000 --> 00:31:06.000 but a buffer of characters behind it. 00:31:06.000 --> 00:31:07.000 Okay. And that buffer 00:31:07.000 --> 00:31:11.000 is used as the kind of document storage for all the characters 00:31:11.000 --> 00:31:12.000 00:31:12.000 --> 00:31:15.000 that you're editing and moving around. As you kind of bop around and move around there's 00:31:15.000 --> 00:31:19.000 something often called the cursor where you type characters 00:31:19.000 --> 00:31:21.000 and characters get moved as you insert. 00:31:21.000 --> 00:31:25.000 You can select things, and delete them, and cut, copy, and paste, and all that sort of stuff. 00:31:25.000 --> 00:31:28.000 So what does that data structure look like? What is the kind of thing that wants to 00:31:28.000 --> 00:31:29.000 back 00:31:29.000 --> 00:31:30.000 a buffer? 00:31:30.000 --> 00:31:33.000 What kinds of things are important in that 00:31:33.000 --> 00:31:35.000 context to be able to support 00:31:35.000 --> 00:31:40.000 and what operations needs to be optimized for that tool to field [inaudible]? 00:31:40.000 --> 00:31:44.000 So what we're going to look at is a real simplification kind of taking that problem, kind of 00:31:44.000 --> 00:31:48.000 distilling it down to its essences and looking at just six operations that are kind 00:31:48.000 --> 00:31:50.000 of at the core of any text editor 00:31:50.000 --> 00:31:54.000 about moving the cursor forward and backwards with these commands. 00:31:54.000 --> 00:31:57.000 Jumping to the front and the back of the entire buffer and then inserting and 00:31:57.000 --> 00:32:00.000 deleting in relative to the cursor 00:32:00.000 --> 00:32:01.000 within that buffer. 00:32:01.000 --> 00:32:04.000 And we're going to do this with an extremely old school interface that's 00:32:04.000 --> 00:32:08.000 actually command base. It's like VI comes back from the dead for those of you who 00:32:08.000 --> 00:32:11.000 have ever heard of such things as VI and E Max. 00:32:11.000 --> 00:32:13.000 And so we're going to look at this because there's actually a lot of ways 00:32:13.000 --> 00:32:15.000 you can approach that, 00:32:15.000 --> 00:32:18.000 that take advantage of some of the things that we've already built like the vector, and stacking queue, 00:32:18.000 --> 00:32:21.000 and the link list. And sort of think about what 00:32:21.000 --> 00:32:24.000 kind of options play out well. 00:32:24.000 --> 00:32:28.000 What I'm going to show you first off is just what the tool looks like 00:32:28.000 --> 00:32:31.000 so you can really feel 00:32:31.000 --> 00:32:33.000 schooled in the old way of doing things. 00:32:33.000 --> 00:32:34.000 So I insert 00:32:34.000 --> 00:32:38.000 the characters A, B, C, D, E, F, G into the editor. 00:32:38.000 --> 00:32:41.000 And then I can use the command B to backup. 00:32:41.000 --> 00:32:44.000 I can use the command J to jump to the beginning, E to jump to the end, 00:32:44.000 --> 00:32:46.000 and so F and B, 00:32:46.000 --> 00:32:50.000 B moving me backwards, F moving me forwards, and then once I'm at a place, let's say I jump 00:32:50.000 --> 00:32:53.000 to the end, I can insert Z, Z, 00:32:53.000 --> 00:32:58.000 Y, X here at the beginning and it'll slide things over, and then I can delete characters 00:32:58.000 --> 00:33:00.000 shuffling things down. 00:33:00.000 --> 00:33:04.000 So that's what we've got. Imagine now, it's like building 00:33:04.000 --> 00:33:07.000 Microsoft Word on top of this. There's a lot of stuff that goes, you know, between here 00:33:07.000 --> 00:33:11.000 and there. but it does give you an idea that the kind of core data requirements 00:33:11.000 --> 00:33:12.000 every word processor 00:33:12.000 --> 00:33:16.000 needs some tool like this some abstraction underneath it to manage the 00:33:16.000 --> 00:33:23.000 character data. Okay. Where do we start? Where do 00:33:23.000 --> 00:33:31.000 we start? 00:33:31.000 --> 00:33:33.000 Anyone want to propose an implementation. 00:33:33.000 --> 00:33:37.000 What's the easiest thing to do? [Inaudible]. In a 00:33:37.000 --> 00:33:40.000 way. Something like that. You know, and if you think Array, 00:33:40.000 --> 00:33:43.000 then you should immediately think after it, no, I don't want to deal with Array, I want a vector. 00:33:43.000 --> 00:33:44.000 I'd like a vector. 00:33:44.000 --> 00:33:45.000 Vector please. 00:33:45.000 --> 00:33:47.000 Right. So I'm going to show you what the interface looks like, here, that we 00:33:47.000 --> 00:33:50.000 have a buffer in these six operations. We're going 00:33:50.000 --> 00:33:52.000 to think about what the private part looks like, 00:33:52.000 --> 00:33:54.000 and we say, what about vector. 00:33:54.000 --> 00:33:57.000 Vector handles big, you know, 00:33:57.000 --> 00:34:00.000 chunky things indexed 00:34:00.000 --> 00:34:01.000 by slots. 00:34:01.000 --> 00:34:05.000 That might be a good idea. So if I had the buffer contents A, B, C, D, E, 00:34:05.000 --> 00:34:08.000 right, and if I could slam those into a vector, 00:34:08.000 --> 00:34:10.000 and then I would need one other little bit of information here, which is 00:34:10.000 --> 00:34:14.000 where is the cursor in any given point because the cursor is actually where the 00:34:14.000 --> 00:34:16.000 uncertain lead operations take place relative to the cursor. 00:34:16.000 --> 00:34:19.000 The buffer's also going to manage that, knowing where the insertion point is 00:34:19.000 --> 00:34:22.000 and then deleting it and inserting relative to that. 00:34:22.000 --> 00:34:23.000 So I say, okay. 00:34:23.000 --> 00:34:25.000 I need some index. 00:34:25.000 --> 00:34:29.000 The cursor actually is between two character positions. 00:34:29.000 --> 00:34:31.000 00:34:31.000 --> 00:34:31.000 00:34:31.000 --> 00:34:33.000 This is like slot zero, one, and two. 00:34:33.000 --> 00:34:37.000 And the cursor is between, actually, one 00:34:37.000 --> 00:34:41.000 and two. And there's just a minor detail to kind of have worked out 00:34:41.000 --> 00:34:46.000 before we start trying to write this code, which is do we want the 00:34:46.000 --> 00:34:49.000 cursor to hold the index on the character that's to the left of it or to the 00:34:49.000 --> 00:34:50.000 right of it. 00:34:50.000 --> 00:34:52.000 It's totally symmetric, right. 00:34:52.000 --> 00:34:55.000 If I have these five characters, it could be that my cursor then goes from 00:34:55.000 --> 00:34:56.000 zero 00:34:56.000 --> 00:34:58.000 to four, it could be that it goes 00:34:58.000 --> 00:35:01.000 from zero to five, or it could be it goes from negative one to four, depending on 00:35:01.000 --> 00:35:05.000 which way I'm doing it. I'm going to happen to use the one that does zero 00:35:05.000 --> 00:35:07.000 to five 00:35:07.000 --> 00:35:12.000 where it's actually recorded as the character that's after it. Okay. 00:35:12.000 --> 00:35:16.000 So that's just kind of the staring point. I'm going to write some code and 00:35:16.000 --> 00:35:20.000 make it happen. 00:35:20.000 --> 00:35:23.000 So I'm going 00:35:23.000 --> 00:35:25.000 to remove 00:35:25.000 --> 00:35:34.000 this. Reference it, that's okay. 00:35:34.000 --> 00:35:36.000 And then I'm going to add the 00:35:36.000 --> 00:35:37.000 things I want to 00:35:37.000 --> 00:35:39.000 replace it with. So I put in the 00:35:39.000 --> 00:35:45.000 editor code and to put in the buffer code. Was buffer already in here? I think buffer's already here. 00:35:45.000 --> 00:35:51.000 Okay. Let's take a look. I get my buffer. 00:35:51.000 --> 00:36:02.000 So 00:36:02.000 --> 00:36:08.000 right now, let's take a look what's in buffered. I think I have the 00:36:08.000 --> 00:36:11.000 starting set information. So it has move cursor over, you know move the 00:36:11.000 --> 00:36:14.000 cursor around, it has the delete, and it has - let 00:36:14.000 --> 00:36:18.000 me make it have 00:36:18.000 --> 00:36:20.000 some variables that I want. I've got 00:36:20.000 --> 00:36:23.000 my cursor. I've got my vector of characters. Right now, it has 00:36:23.000 --> 00:36:26.000 a lot of copying, I think, just in anticipation of going places 00:36:26.000 --> 00:36:29.000 where copying is going to be required. 00:36:29.000 --> 00:36:31.000 Right now, because it's using vector and vector has deep copying behavior, I'm 00:36:31.000 --> 00:36:37.000 actually okay if I let windows copy and go through. But I'm actually going to 00:36:37.000 --> 00:36:39.000 not do that right away. 00:36:39.000 --> 00:36:45.000 All right. So then let's look at the implementation side of this and I have an 00:36:45.000 --> 00:36:47.000 implementation in here I want to get rid of. Sorry about this but I 00:36:47.000 --> 00:36:54.000 left the old one in here. That will do all the things that will need to 00:36:54.000 --> 00:36:56.000 get done. 00:36:56.000 --> 00:36:58.000 Okay. So let's deal with 00:36:58.000 --> 00:37:06.000 at the beginning. Starting off, right, we will set the cursor to zero. So that's 00:37:06.000 --> 00:37:08.000 the vector gets started, 00:37:08.000 --> 00:37:11.000 initialize empty, and nothing else I need to do with the vector. 00:37:11.000 --> 00:37:16.000 A character [inaudible] cursor position in that, and then moving the cursor position 00:37:16.000 --> 00:37:17.000 forward, 00:37:17.000 --> 00:37:21.000 it's mostly just doing this, right, cursor plus, plus, right. It's just 00:37:21.000 --> 00:37:23.000 an easy index to move it down. But 00:37:23.000 --> 00:37:26.000 I do need to make sure that the cursor isn't already at the end. So if 00:37:26.000 --> 00:37:29.000 the cursor is less than 00:37:29.000 --> 00:37:36.000 the size of the vector then I will let it advance. But it never gets beyond the 00:37:36.000 --> 00:37:38.000 00:37:38.000 --> 00:37:40.000 size itself. So 00:37:40.000 --> 00:37:43.000 like all of these are going to have same problems. Like, if I'm already at the end and somebody asks me to advance, I don't 00:37:43.000 --> 00:37:46.000 want to suddenly get my cursor in some lax stated that 00:37:46.000 --> 00:37:52.000 sort of back that. Similarly, on this one, if the cursor 00:37:52.000 --> 00:37:56.000 is greater than zero, all right, then we can back it up and 00:37:56.000 --> 00:37:58.000 move the cursor around. 00:37:58.000 --> 00:38:01.000 Moving the cursor at the start is very easy. 00:38:01.000 --> 00:38:03.000 Moving the cursor to the end is, also, 00:38:03.000 --> 00:38:05.000 very easy, right. We have a 00:38:05.000 --> 00:38:08.000 convention about what it is. 00:38:08.000 --> 00:38:11.000 And so then inserting a character 00:38:11.000 --> 00:38:15.000 is, also, pretty easy because all the hard work is done 00:38:15.000 --> 00:38:17.000 by the vector. 00:38:17.000 --> 00:38:18.000 00:38:18.000 --> 00:38:20.000 When I say insert this new character 00:38:20.000 --> 00:38:23.000 at the cursor position, right, then this character advance the 00:38:23.000 --> 00:38:26.000 cursor to be kind of past it, and then 00:38:26.000 --> 00:38:31.000 deleting the character, also, pretty easy. Let 00:38:31.000 --> 00:38:34.000 me show you, before I go finishing the cursor, I just want to show 00:38:34.000 --> 00:38:38.000 you this little diagram of what's happening while I'm doing stuff. So this is kind of a 00:38:38.000 --> 00:38:41.000 visualization of the things that are happening. So this is showing the vector and 00:38:41.000 --> 00:38:45.000 its length. And then the cursor position 00:38:45.000 --> 00:38:48.000 as we've done things. Inserted six characters, the cursor's currently sitting 00:38:48.000 --> 00:38:52.000 at the end, and for the length of the cursor, actually, the same value right now. If 00:38:52.000 --> 00:38:56.000 I issue a back command, right, that deducts the cursor by 00:38:56.000 --> 00:38:58.000 one, it shows another one backs up by one, 00:38:58.000 --> 00:39:01.000 and then eventually the cursor gets to the position zero and subsequent backs don't 00:39:01.000 --> 00:39:04.000 do anything to it, it just kind of bounces off the edge. And 00:39:04.000 --> 00:39:08.000 moving forward, things are easier to deal with. It will advance until the gets to the length, 00:39:08.000 --> 00:39:10.000 the size of that text, and it won't go any further. 00:39:10.000 --> 00:39:14.000 And that jumping to the front in the end are just a matter of updating that 00:39:14.000 --> 00:39:17.000 cursor position from zero to the size and whatnot. 00:39:17.000 --> 00:39:19.000 The operation where 00:39:19.000 --> 00:39:21.000 things get a little 00:39:21.000 --> 00:39:22.000 more 00:39:22.000 --> 00:39:25.000 bogged down is on this insert 00:39:25.000 --> 00:39:30.000 where I need to insert a position. If I insert the X, Y, Z 00:39:30.000 --> 00:39:32.000 into the middle there and you can see it kind of 00:39:32.000 --> 00:39:37.000 chopping those characters down, making that space to insert those things in the 00:39:37.000 --> 00:39:37.000 middle. 00:39:37.000 --> 00:39:41.000 Similarly, doing delete operations, if I jump 00:39:41.000 --> 00:39:43.000 to the 00:39:43.000 --> 00:39:45.000 beginning I start doing deletes here. 00:39:45.000 --> 00:39:48.000 It has to copy all those characters over and 00:39:48.000 --> 00:39:53.000 deduct that length, right, so that the vectors do that work for us on remove that. 00:39:53.000 --> 00:39:55.000 That means that if the vector's very large, which is 00:39:55.000 --> 00:39:58.000 not a typical in a word processor situation, you could have you PhD theses 00:39:58.000 --> 00:40:00.000 which has hundreds of pages, 00:40:00.000 --> 00:40:03.000 if you go back to the beginning and you start deleting characters you'd hate to think it was just 00:40:03.000 --> 00:40:05.000 taking this massive shuffle time 00:40:05.000 --> 00:40:09.000 to get the work done. So 00:40:09.000 --> 00:40:13.000 let's see my - I'm going 00:40:13.000 --> 00:40:14.000 to 00:40:14.000 --> 00:40:24.000 bring in the display that 00:40:24.000 --> 00:40:38.000 - 00:40:38.000 --> 00:40:40.000 whoops, 00:40:40.000 --> 00:40:42.000 I don't like something here. [Inaudible]. Now, what do we have? Oh, look at this. Well, well, we will continue on. 00:40:42.000 --> 00:41:00.000 This inside - okay. Hello. [Inaudible]. I have no idea. I'm not going to try to [inaudible]. 00:41:00.000 --> 00:41:02.000 Okay. 00:41:02.000 --> 00:41:08.000 It's inside. 00:41:08.000 --> 00:41:21.000 That's got me totally upset. Okay. We still have our old code somewhere. Okay. Why do I still have the wrong - what is - okay. Oh, 00:41:21.000 --> 00:41:25.000 well. Today's not my lucky day. I'm not going to worry about that too much. I'm just going to show [inaudible]. Okay. 00:41:25.000 --> 00:41:27.000 So 00:41:27.000 --> 00:41:29.000 seeing what's actually happening, right, it's like just mostly 00:41:29.000 --> 00:41:31.000 00:41:31.000 --> 00:41:34.000 kind of moving that stuff back and forth, and then having the vector do the big 00:41:34.000 --> 00:41:37.000 work, right, of inserting and deleting those characters, and doing all the copying 00:41:37.000 --> 00:41:40.000 to get the thing done. 00:41:40.000 --> 00:41:43.000 We'll come back over here 00:41:43.000 --> 00:41:47.000 and kind of see what good it does and what things it's not so hot 00:41:47.000 --> 00:41:48.000 at, right, 00:41:48.000 --> 00:41:51.000 is that it is easy to move the cursor wherever you want. 00:41:51.000 --> 00:41:54.000 You know, that moving it a long distance or a short distance, moving it is a little 00:41:54.000 --> 00:41:55.000 bit 00:41:55.000 --> 00:41:56.000 00:41:56.000 --> 00:41:59.000 or to the beginning to the end, equally easy as by just resetting 00:41:59.000 --> 00:42:01.000 some variable. 00:42:01.000 --> 00:42:05.000 It's the enter and delete, right where this one actually really suffers, right, 00:42:05.000 --> 00:42:08.000 based on how many characters, right, follow that cursor, 00:42:08.000 --> 00:42:11.000 right you could potentially be moving the entire contents of the buffer. 00:42:11.000 --> 00:42:14.000 Adding at the end is actually going to be relatively fast. So if you have 00:42:14.000 --> 00:42:17.000 to type in order, like you just type you theses out 00:42:17.000 --> 00:42:19.000 and never go back to edit anything, 00:42:19.000 --> 00:42:22.000 then it would be fine, adding those characters at the end. But at any 00:42:22.000 --> 00:42:25.000 point, if you move the cursor back into the mid of the body, somewhere in 00:42:25.000 --> 00:42:28.000 the front, somewhere in the middle, right, a lot of characters get shuffled as you 00:42:28.000 --> 00:42:30.000 change, makes edits in the middle there. 00:42:30.000 --> 00:42:35.000 And so what this seems that it actually has good movement but bad editing 00:42:35.000 --> 00:42:38.000 as a buffer strategy. 00:42:38.000 --> 00:42:39.000 That's 00:42:39.000 --> 00:42:40.000 probably, 00:42:40.000 --> 00:42:43.000 you know, given that we have these six operations we'll all interested in, 00:42:43.000 --> 00:42:46.000 this is probably not the mix that seems to make the most sense for something 00:42:46.000 --> 00:42:49.000 that's called an editor, right. It is the editing operations are the ones that is 00:42:49.000 --> 00:42:52.000 weakest on, it has its most 00:42:52.000 --> 00:42:53.000 00:42:53.000 --> 00:42:54.000 debilitating performance, 00:42:54.000 --> 00:42:58.000 it wouldn't make that good of an editor, right, if that was going to be your behavior. It 00:42:58.000 --> 00:43:02.000 has one advantage that we're going see, actually, we're going to have to kind of 00:43:02.000 --> 00:43:06.000 make compromises on it, which it actually uses very little extra space, though, but 00:43:06.000 --> 00:43:08.000 it's using the vector, 00:43:08.000 --> 00:43:12.000 which potentially might have excess capacity up to twice that. So maybe it's 00:43:12.000 --> 00:43:14.000 about two bites per char in the very worse case. 00:43:14.000 --> 00:43:18.000 But it actually has no per character overhead that's already imbedded 00:43:18.000 --> 00:43:23.000 in the data structure from this side. 00:43:23.000 --> 00:43:31.000 Now, I'm just going to go totally somewhere else. Okay. 00:43:31.000 --> 00:43:34.000 So instead of 00:43:34.000 --> 00:43:37.000 thinking of it as vector, thinking of it as continuous block, 00:43:37.000 --> 00:43:41.000 is to kind of realize the editing operations, right, 00:43:41.000 --> 00:43:44.000 that comes back to the idea, like if I were working at the very end of my document that I 00:43:44.000 --> 00:43:47.000 can edit there efficiently. 00:43:47.000 --> 00:43:48.000 00:43:48.000 --> 00:43:51.000 It kind of inspires me to think of, how do I think I can make it so 00:43:51.000 --> 00:43:55.000 the insertion point is somehow in the efficient place. Like the 00:43:55.000 --> 00:43:58.000 edits that are happening on the insertion point, can I somehow make it to where 00:43:58.000 --> 00:44:01.000 access to the insertion point is easy? 00:44:01.000 --> 00:44:04.000 That rather than bearing that in the middle of my vector, if I can expose that 00:44:04.000 --> 00:44:08.000 to make it accessible easily in the way the code manipulates the internal of the 00:44:08.000 --> 00:44:09.000 buffer. 00:44:09.000 --> 00:44:14.000 And so the idea here is to break up the text into two pieces. 00:44:14.000 --> 00:44:17.000 Things that are to the left of the cursor, things that are to the right, and I'm calling this 00:44:17.000 --> 00:44:19.000 the before and the after. 00:44:19.000 --> 00:44:20.000 And then 00:44:20.000 --> 00:44:24.000 organize those so they're actually averted from each other to where all the 00:44:24.000 --> 00:44:27.000 characters that lead up to the cursor 00:44:27.000 --> 00:44:31.000 are set up to where B is very close, assessable at the top of, in this 00:44:31.000 --> 00:44:32.000 case, a stack, 00:44:32.000 --> 00:44:36.000 and that the things that are farther away are buried further down in the stack in that 00:44:36.000 --> 00:44:37.000 inaccessible region. 00:44:37.000 --> 00:44:41.000 And similarly over here, but inverted, that C is the top of this stack and D 00:44:41.000 --> 00:44:44.000 and E, and things are further down, they are buried 00:44:44.000 --> 00:44:46.000 away from me, 00:44:46.000 --> 00:44:50.000 figuring the things I really need access to are right next to the cursor 00:44:50.000 --> 00:44:55.000 that I'm willing to kind of move those other things father away to gain that access. 00:44:55.000 --> 00:45:00.000 And so what this buffer does is it uses two stacks, a 00:45:00.000 --> 00:45:01.000 before stack, 00:45:01.000 --> 00:45:03.000 an after stack, 00:45:03.000 --> 00:45:07.000 and that the operations for moving the data around 00:45:07.000 --> 00:45:09.000 become 00:45:09.000 --> 00:45:10.000 transferring things 00:45:10.000 --> 00:45:13.000 from the before to the after stack. 00:45:13.000 --> 00:45:16.000 Where's the cursor? 00:45:16.000 --> 00:45:19.000 How does the cursor represent it? Do I need some more data here? 00:45:19.000 --> 00:45:25.000 Some integer some ? 00:45:25.000 --> 00:45:28.000 It's really kind of odd to think about but there is no explicit cursor, right, 00:45:28.000 --> 00:45:31.000 being stored here but the cursor is, in this case, right, like 00:45:31.000 --> 00:45:34.000 modeled by what's on the before stack or all the things to the left of the cursor. 00:45:34.000 --> 00:45:38.000 What's on the after stack is following the cursor. So in 00:45:38.000 --> 00:45:39.000 fact, 00:45:39.000 --> 00:45:42.000 the cursor is represented as the empty space between these two 00:45:42.000 --> 00:45:45.000 where you have, 00:45:45.000 --> 00:45:48.000 you know, how many ever characters wrong before is actually the index of where 00:45:48.000 --> 00:45:51.000 the cursor is. So kind of a 00:45:51.000 --> 00:45:54.000 wacky way of thinking about it, but one that actually have some pretty neat properties 00:45:54.000 --> 00:45:57.000 in terms of making it run efficiently. 00:45:57.000 --> 00:46:01.000 So let me show you 00:46:01.000 --> 00:46:02.000 the 00:46:02.000 --> 00:46:03.000 00:46:03.000 --> 00:46:05.000 diagram of it happening. 00:46:05.000 --> 00:46:09.000 So insert A, B, C, D, E, F, G. 00:46:09.000 --> 00:46:12.000 And so, the operation of inserting 00:46:12.000 --> 00:46:15.000 a new character into this form of the buffer 00:46:15.000 --> 00:46:19.000 is pushing something on the before stack. 00:46:19.000 --> 00:46:23.000 So if I want to move the cursor, let's say I want to move it back one, 00:46:23.000 --> 00:46:25.000 then I pop 00:46:25.000 --> 00:46:27.000 the top off of the before push it on the after. 00:46:27.000 --> 00:46:32.000 I keep doing that and it transfers the G to the H, you know, the E and so on, as I 00:46:32.000 --> 00:46:33.000 back up. 00:46:33.000 --> 00:46:37.000 Moving forward does that same operation in the inverse. If I want to move forward I take 00:46:37.000 --> 00:46:40.000 something off the top of the after and push it on before. So it's kind of shuffling it 00:46:40.000 --> 00:46:42.000 from one side to the other. 00:46:42.000 --> 00:46:46.000 Given that these push and pop operations are constant on both implementations of 00:46:46.000 --> 00:46:47.000 the stack we saw, 00:46:47.000 --> 00:46:52.000 then that means that our cursor movement is also of one so I can do 00:46:52.000 --> 00:46:55.000 this. And if I do deletes it's actually just popping from the after stack and 00:46:55.000 --> 00:46:57.000 throwing it away. 00:46:57.000 --> 00:47:00.000 So also easy to do edits 00:47:00.000 --> 00:47:04.000 because I'm talking about adding things to the before and deleting things from after, 00:47:04.000 --> 00:47:06.000 both of which can be done very efficiently. 00:47:06.000 --> 00:47:07.000 The one operation 00:47:07.000 --> 00:47:10.000 that this guy has trouble with 00:47:10.000 --> 00:47:14.000 is big cursor movement. 00:47:14.000 --> 00:47:17.000 If I want to go all the way to the beginning 00:47:17.000 --> 00:47:19.000 or all the way to the end, 00:47:19.000 --> 00:47:22.000 then everything's got to go. No 00:47:22.000 --> 00:47:23.000 matter 00:47:23.000 --> 00:47:28.000 how many things I have to empty out the entire after stack to position the 00:47:28.000 --> 00:47:29.000 cursor at the end, 00:47:29.000 --> 00:47:31.000 or empty out the entire before stack 00:47:31.000 --> 00:47:35.000 to get it all the way over here. 00:47:35.000 --> 00:47:38.000 So I could of sort of talk you though this code, and actually, I won't type it 00:47:38.000 --> 00:47:41.000 just because I actually want to talk about its analysis more than I want to see its code. Each of 00:47:41.000 --> 00:47:43.000 these actually becomes a one liner. 00:47:43.000 --> 00:47:46.000 Insert is push on the before stack. 00:47:46.000 --> 00:47:49.000 Delete is pop from the after stack. The cursor movement is popping 00:47:49.000 --> 00:47:53.000 from one and pushing on the other depending on the direction and then that same code 00:47:53.000 --> 00:47:56.000 just for the Y loop, like wild or something on the one stack, just keep 00:47:56.000 --> 00:47:59.000 pushing and popping from one empty to the other. 00:47:59.000 --> 00:48:02.000 So very little code to write, right, 00:48:02.000 --> 00:48:05.000 depending a lot on the stacks abstractions coming through for us 00:48:05.000 --> 00:48:09.000 and then making this ultra fit that we've managed to get editing suddenly, 00:48:09.000 --> 00:48:10.000 like, a lot more efficient 00:48:10.000 --> 00:48:15.000 but the cost of sacrificing one of our other operations. Let's take a look at what that 00:48:15.000 --> 00:48:19.000 look like, right. 00:48:19.000 --> 00:48:22.000 Suddenly I can do all this insert and delete 00:48:22.000 --> 00:48:23.000 at the insertion point 00:48:23.000 --> 00:48:24.000 00:48:24.000 --> 00:48:25.000 with very little trouble, 00:48:25.000 --> 00:48:29.000 and I suddenly made this other operation, oddly enough, 00:48:29.000 --> 00:48:30.000 slow. 00:48:30.000 --> 00:48:33.000 All a sudden it's like if you go back to the beginning of your document you're in the 00:48:33.000 --> 00:48:36.000 middle of editing, you're at the bottom. You say, oh, I want to go back to the beginning 00:48:36.000 --> 00:48:39.000 and you can imagine how that would feel if you were typing 00:48:39.000 --> 00:48:42.000 it would actually be the act of going up and clicking up in the top right by where you made 00:48:42.000 --> 00:48:44.000 the insertion and all a sudden you'd see the white cursor 00:48:44.000 --> 00:48:45.000 and you'd be like, 00:48:45.000 --> 00:48:49.000 why is that taking so long. How could that possibly be, you know, 00:48:49.000 --> 00:48:51.000 that it's doing this kind of rearrangement, but yet it made 00:48:51.000 --> 00:48:54.000 editing even on a million character document fast. But 00:48:54.000 --> 00:48:55.000 that movement 00:48:55.000 --> 00:48:58.000 long distances, you know, jumping a couple of pages or 00:48:58.000 --> 00:48:59.000 front to back, 00:48:59.000 --> 00:49:05.000 suddenly, having to do this big transfer behind the scenes, so 00:49:05.000 --> 00:49:08.000 different, right. So now I had six operations, I had four fast, 00:49:08.000 --> 00:49:09.000 two slow, 00:49:09.000 --> 00:49:12.000 now I have four fast, two slow. 00:49:12.000 --> 00:49:16.000 It still might be that, actually, this is an interesting improvement, though, because 00:49:16.000 --> 00:49:19.000 we have decided those are operations that we're willing to tolerate, 00:49:19.000 --> 00:49:23.000 being slower, especially, if it means some operation that we're more interested in, if 00:49:23.000 --> 00:49:24.000 performance being faster. 00:49:24.000 --> 00:49:28.000 And that likely seems like it's moving in the right direction because editing, right, being 00:49:28.000 --> 00:49:31.000 the whole purpose, right, behind what we're trying to do is 00:49:31.000 --> 00:49:32.000 a text setter, 00:49:32.000 --> 00:49:34.000 that seems like that might be a good call. 00:49:34.000 --> 00:49:38.000 What I'm going to start looking at and it's going to be something I want to talk about 00:49:38.000 --> 00:49:39.000 next time, 00:49:39.000 --> 00:49:42.000 is what can a link list do for us? 00:49:42.000 --> 00:49:46.000 Both of those are fighting against that continuous thing, the copying and the 00:49:46.000 --> 00:49:48.000 shuffling, and the inserting 00:49:48.000 --> 00:49:51.000 is there something about that flexibility of the rewiring 00:49:51.000 --> 00:49:53.000 that can kind of get us out of this 00:49:53.000 --> 00:49:55.000 some operations having to be slow 00:49:55.000 --> 00:50:00.000 because other operations are fast. Question. [Inaudible]. Yeah. 00:50:00.000 --> 00:50:03.000 Why is space used for stacks when 00:50:03.000 --> 00:50:04.000 those are half? Yeah. 00:50:04.000 --> 00:50:08.000 So in this case, right, depending on how you're 00:50:08.000 --> 00:50:09.000 modeling your stack, if it is with 00:50:09.000 --> 00:50:12.000 vectors, right, then you're likely to have the before and the after stack, both 00:50:12.000 --> 00:50:16.000 have capacity for everything, even when they're not used, right. And so it's very 00:50:16.000 --> 00:50:17.000 likely that either right 00:50:17.000 --> 00:50:20.000 give 100 characters they're either on one stack or the other, but the other one might be kind of 00:50:20.000 --> 00:50:23.000 harboring the 100 spots for them when they come back. 00:50:23.000 --> 00:50:27.000 Or it could just be that you have the link list, which are allocating and de allocating but 00:50:27.000 --> 00:50:30.000 has the overhead of that. So it's likely that no matter which implementation the stack 00:50:30.000 --> 00:50:33.000 you have, you probably have about twice the storage that you need because of the 00:50:33.000 --> 00:50:38.000 two sides and the overhead that's kind of coming and going there. 00:50:38.000 --> 00:50:41.000 So we will see the link list and we'll talk that guy through on 00:50:41.000 --> 00:50:46.000 Friday. I'll see you then.