Anonymous
add 1 MAIN: (size = 1, capacity = 3) 1, _, _ UNDO: (size = 2, capacity = 12) { ADD, 1 }, _, _, _, _, _ add 2 MAIN: (size = 2, capacity = 3) 1, 2, _ UNDO: (size = 4, capacity = 12) { ADD, 1 }, { ADD, 2 }, _, _, _, _ add 3 MAIN: (size = 3, capacity = 3) 1, 2, 3 UNDO: (size = 6, capacity = 12) { ADD, 1 }, { ADD, 2 }, { ADD, 3 }, _, _, _ add 4 MAIN: (size = 3, capacity = 3) 2, 3, 4 UNDO: (size = 8, capacity = 12) { ADD, 1 }, { ADD, 2 }, { ADD, 3 }, { ADD, 4 }, _, _ add 5 MAIN: (size = 3, capacity = 3) 3, 4, 5 UNDO: (size = 10, capacity = 12) { ADD, 1 }, { ADD, 2 }, { ADD, 3 }, { ADD, 4 }, { ADD, 5 }, _
Anonymous
i cannot undo pop 2 without having to rebuild the undo queue since the front is 1 and the back is 5
Ehsan
add 4 it should be 4 2 3
Ehsan
you don’t move the 2!
Anonymous
internally it is 4 2 3
Ehsan
you just do forward++
Ehsan
so?
Ehsan
undo: 1 out of queuebuff 1 2 3
Ehsan
forward— back—
Anonymous
this is how i print my buffers https://gist.github.com/2848aafc0bc728e57c102c7c419c7823
Ehsan
one last example
Anonymous
yea
Anonymous
add 4 it should be 4 2 3
so internally it IS that, but printing wise it is 2 3 4, eg in terms of indexes, printed as [1] [2] [0]
Ehsan
add 1 2 3 1 2 3 add 4 queueBuf adds 1 4 2 3 add 5 queueBuf adds 2 4 5 3 undo add queueBuf gets 2 4 2 3 undo add queueBuf gets 1 1 2 3 undo add since queueBuf is empty when we can just do back—
Ehsan
you got the solution
Ehsan
you just need to work out how to store the commands 🙂 I know you can do it it’s simple
Ehsan
you can make it uniform by pushing struct pointers instead
Anonymous
so... something like this then? if (main->size() == main->capacity()) { push_front(undo_, ADD_WRAPPED, main->front()); } else push_front(undo_, ADD, n);
Ehsan
struct command{ bool add; int value; };
Ehsan
anyway it’s easy
Anonymous
eg 123 add 5 push ADD_WRAPPED 1 to undo_ should i also push ADD 5 to undo_ as well?
Ehsan
for redo?
Anonymous
yea
Ehsan
redo buf is seperate 🙂
Anonymous
eg so it can re add the 1, and then re add the 5
Ehsan
it’s the same logic
Ehsan
yes
ok do the same but in reverse
Ehsan
it’s not hard if you understood how undo works
Anonymous
eg UNDO: {ADD_WRAPPED 1}, {ADD 5}
Anonymous
and assuming MAIN: 2,3,5 undo: push 1 to left side no need to process add 5 since it just pops main
Ehsan
I am busy now but I think you got it
Anonymous
ok
Anonymous
i hope that will work tho
Anonymous
Okay so I've been Googling this for a while
Anonymous
I need the kbhit() function, which requires conio.h
Anonymous
For linux
Anonymous
does anyone have an easier way to format a string based on its maximum actual length? as formatting based on max POSSIBLE length is very anoying since the max is 23 (14 for command string, 9 for other) eg MAIN: (size = 02, capacity = 03) 5, 6, _ UNDO: (size = 04, capacity = 06) { ADD, 5 }, { ADD, 6 }, _
Anonymous
Anonymous
also how do i implement push_back in a queue that can only push front, pop front, and pop back?
Luis
Get best-book
Luis
get #best-book
Mar!o
Mihai
get #best-book
Get a search on Google 🤷‍♂
Diego
get #best-book
/get cppbookguide
Luis
Thanks
Sandro
https://pastebin.com/sGKEa6k1 Someone know why on GBD this code work well up to certain numbers of total nodes? Than memory segmentation fault occurs? Thnx
Anonymous
just concatenat strings
that does not work for, say | 0 1 2 3 | 1 24 93 APPLE it should be | 0 1 2 3 | 1 24 93 APPLE
Anonymous
ok i can now fully undo and redo an add(value) even when the circular buffer wraps around 🙂 https://gist.github.com/80a69b9d54f4d80fe03be8fecbaea6fb https://del.dog/uthomerofo.txt
Anonymous
int i; Printf("%d\n", i);
Anonymous
Why does this print 0 even when i is neither a global variable nor a static int ?
Anonymous
Also if I increment before printing, it prints 1
Anonymous
You're the best 🤗🤗
Vlad
It can print anything. 0 included
Anonymous
Because UB
My bad but, UB ?
klimi
Undefined behaviour
Anonymous
Oh
Vlad
My bad but, UB ?
Your integer is uninitialized reading its value is undefined behavior
Anonymous
But it prints 0 everytime, so it shouldn't be some random behaviour I mean it should sometimes print something other than 0
Anonymous
But it doesn't
Vlad
But it prints 0 everytime, so it shouldn't be some random behaviour I mean it should sometimes print something other than 0
It doesn't matter what it prints. What matters is that your program is Ill formed
Nameful
C/C++ does not specify how this should be handled
klimi
Depends what you have currently in memory ¯\_ (ツ) _/¯
Nameful
Depends what you have currently in memory ¯\_ (ツ) _/¯
couldn't the compiler technically initialise the variable if it wanted to?
Nameful
and that would be okay since it's undefined behaviour
Nameful
Nameful
so it doesn't necessarily depend on what you have in memory
klimi
Anonymous
It doesn't matter what it prints. What matters is that your program is Ill formed
Well I made that program the way it is intentionally cos in other cases when I accidentally forget to initialise my variables, the output was never a 0, mostly something like -36292 or 83018 and also used to change whenever I recompile it But In this case the output is always 0 no matter how many times I compile the program
Anonymous
couldn't the compiler technically initialise the variable if it wanted to?
It does Global vars and static vars are automatically initialised to 0
Carl
Question : print the string in reverse. using recursion