Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. an interview question of undo/redo

an interview question of undo/redo

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelpcareer
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qudayong
    wrote on last edited by
    #1

    I got an interview yestoday and was stumbed by the following question, Suppose you have a MS-Word like application. How do implement the UNDO/REDO function. Initially, I suggest saving the file everytime with a change. After I spoke it out. I realize that it's impossible. I can't save the file everytime I typed a character. I can't give a resonable solution on this one. Anybody can help?

    J D C 3 Replies Last reply
    0
    • Q qudayong

      I got an interview yestoday and was stumbed by the following question, Suppose you have a MS-Word like application. How do implement the UNDO/REDO function. Initially, I suggest saving the file everytime with a change. After I spoke it out. I realize that it's impossible. I can't save the file everytime I typed a character. I can't give a resonable solution on this one. Anybody can help?

      J Offline
      J Offline
      Jose Lamas Rios
      wrote on last edited by
      #2

      It's usually implemented using GOF's Command design pattern[^]. This article, A Basic Undo/Redo Framework For C++[^], might be of help too. -- jlr http://jlamas.blogspot.com/[^]

      1 Reply Last reply
      0
      • Q qudayong

        I got an interview yestoday and was stumbed by the following question, Suppose you have a MS-Word like application. How do implement the UNDO/REDO function. Initially, I suggest saving the file everytime with a change. After I spoke it out. I realize that it's impossible. I can't save the file everytime I typed a character. I can't give a resonable solution on this one. Anybody can help?

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        After each action that can be undone, push the current state of the document onto a stack. Each time an "undo" is requested, pop the most recent document state off the stack and notify the view that a "new" document needs to be rendered.


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        Q 1 Reply Last reply
        0
        • D David Crow

          After each action that can be undone, push the current state of the document onto a stack. Each time an "undo" is requested, pop the most recent document state off the stack and notify the view that a "new" document needs to be rendered.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          Q Offline
          Q Offline
          qudayong
          wrote on last edited by
          #4

          If this is a grphic oriented application, I agree that every primitive has a counter to track the refernece number. This is text processing, MS-Word like application. Is it resonable to change the document state everytime when you type a character? Thank you for response

          D T 2 Replies Last reply
          0
          • Q qudayong

            If this is a grphic oriented application, I agree that every primitive has a counter to track the refernece number. This is text processing, MS-Word like application. Is it resonable to change the document state everytime when you type a character? Thank you for response

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            qudayong wrote: Is it resonable to change the document state everytime when you type a character? I don't know if it's a good idea or not. I've never had the need to implement such a feature so I'm not sure how I'd actually do it. It was just a guess.


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            1 Reply Last reply
            0
            • Q qudayong

              If this is a grphic oriented application, I agree that every primitive has a counter to track the refernece number. This is text processing, MS-Word like application. Is it resonable to change the document state everytime when you type a character? Thank you for response

              T Offline
              T Offline
              Toby Opferman
              wrote on last edited by
              #6

              Just think of the stack: character: rowpos, colnumber For graphics you could have something like: OperationType, MetaData For example. So it's just a stack of commands. You could even determine that a "word" is basically a state, so once an entire word is typed you put it on the stack. So, some considerations could be: 1. How much data are you storing per-letter/state operation? 2. How long of a history buffer do you want? If you store a letter as being character, followed by rowposition, column number you are actually storing 2 bytes (unicode let's say), then 2 bytes (say you can't have a line longer than 65536), then say 4 bytes (you have up to 4 billion lines in the document). 2+2+4 = 8 bytes per character operation. If you type 10,000 words and say each word is an average of 7 characters, that's 70,000 characters, that's 560,000 bytes or about 560k of undo changes. That's also 70,000 entries in your undo list. Not to mention possible optimizations in referencing and things or combining operations that cancel each other out. How many does word go back to? 8bc7c0ec02c0e404c0cc0680f7018827ebee

              T 1 Reply Last reply
              0
              • T Toby Opferman

                Just think of the stack: character: rowpos, colnumber For graphics you could have something like: OperationType, MetaData For example. So it's just a stack of commands. You could even determine that a "word" is basically a state, so once an entire word is typed you put it on the stack. So, some considerations could be: 1. How much data are you storing per-letter/state operation? 2. How long of a history buffer do you want? If you store a letter as being character, followed by rowposition, column number you are actually storing 2 bytes (unicode let's say), then 2 bytes (say you can't have a line longer than 65536), then say 4 bytes (you have up to 4 billion lines in the document). 2+2+4 = 8 bytes per character operation. If you type 10,000 words and say each word is an average of 7 characters, that's 70,000 characters, that's 560,000 bytes or about 560k of undo changes. That's also 70,000 entries in your undo list. Not to mention possible optimizations in referencing and things or combining operations that cancel each other out. How many does word go back to? 8bc7c0ec02c0e404c0cc0680f7018827ebee

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #7

                One option (which is required for usability) is to group words into single undo entries. Otherwise, just undoing single letters gets to be really annoying. Anyway, most people use backspace to correct single letter errors, not undo. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                Q 1 Reply Last reply
                0
                • T Tim Smith

                  One option (which is required for usability) is to group words into single undo entries. Otherwise, just undoing single letters gets to be really annoying. Anyway, most people use backspace to correct single letter errors, not undo. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                  Q Offline
                  Q Offline
                  qudayong
                  wrote on last edited by
                  #8

                  Thank you. You opinion ia valuable

                  1 Reply Last reply
                  0
                  • Q qudayong

                    I got an interview yestoday and was stumbed by the following question, Suppose you have a MS-Word like application. How do implement the UNDO/REDO function. Initially, I suggest saving the file everytime with a change. After I spoke it out. I realize that it's impossible. I can't save the file everytime I typed a character. I can't give a resonable solution on this one. Anybody can help?

                    C Offline
                    C Offline
                    Chris Losinger
                    wrote on last edited by
                    #9

                    for text, it's pretty easy: when the user performs an action, save the action in a stack, then perform the action on the document. for undo, pop the last action off the stack and reverse the action. the trick is to define 'action' in such a way that you can undo it. ex. for deleting text, save the deleted text and its position as the action (to undo, insert the deleted text at the position). for inserting text, save the position and the number of characters added (to undo, remove the number of characters at the position). Cleek | Image Toolkits | Thumbnail maker

                    Q 2 Replies Last reply
                    0
                    • C Chris Losinger

                      for text, it's pretty easy: when the user performs an action, save the action in a stack, then perform the action on the document. for undo, pop the last action off the stack and reverse the action. the trick is to define 'action' in such a way that you can undo it. ex. for deleting text, save the deleted text and its position as the action (to undo, insert the deleted text at the position). for inserting text, save the position and the number of characters added (to undo, remove the number of characters at the position). Cleek | Image Toolkits | Thumbnail maker

                      Q Offline
                      Q Offline
                      qudayong
                      wrote on last edited by
                      #10

                      Suppose you have 20 pages file. You last action is delete 19 pages, and the perform undo. If you perform this action servral times, your stack will be overloaded.

                      C M 2 Replies Last reply
                      0
                      • Q qudayong

                        Suppose you have 20 pages file. You last action is delete 19 pages, and the perform undo. If you perform this action servral times, your stack will be overloaded.

                        C Offline
                        C Offline
                        Chris Losinger
                        wrote on last edited by
                        #11

                        that's why programs put up messages like "Insufficient space in the Undo buffer to save this action. Do you wish to proceed anyway?" (from the Visual Studio editor) Cleek | Image Toolkits | Thumbnail maker

                        1 Reply Last reply
                        0
                        • C Chris Losinger

                          for text, it's pretty easy: when the user performs an action, save the action in a stack, then perform the action on the document. for undo, pop the last action off the stack and reverse the action. the trick is to define 'action' in such a way that you can undo it. ex. for deleting text, save the deleted text and its position as the action (to undo, insert the deleted text at the position). for inserting text, save the position and the number of characters added (to undo, remove the number of characters at the position). Cleek | Image Toolkits | Thumbnail maker

                          Q Offline
                          Q Offline
                          qudayong
                          wrote on last edited by
                          #12

                          Having a test on MS-WORD, I find that you delete 20 page then perform undo. After 30 times there's no complain about memory and the action performs very fast. So I assume, there must has other way than "command pattern" to perform such an action. cheers

                          1 Reply Last reply
                          0
                          • Q qudayong

                            Suppose you have 20 pages file. You last action is delete 19 pages, and the perform undo. If you perform this action servral times, your stack will be overloaded.

                            M Offline
                            M Offline
                            Mister Transistor
                            wrote on last edited by
                            #13

                            that's not usually true - you can't undo an undo - when you do the first undo, the stack is emptied

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups