r/programming • • Dec 14 '19

Challenging projects every programmer should try

http://web.eecs.utk.edu/~azh/blog/challengingprojects.html
636 Upvotes

99 comments sorted by

View all comments

-73

u/MetalSlug20 Dec 14 '19 edited Dec 14 '19

It's not efficient, but with how much memory we have to work with these days I think a text editor could just use an array, and just copy to a whole new array during an insert operation.. Basically still just O(n) time. Or it could just use a tree with each word being a node for O(log n) time . Yes use more memory but hey why not As long as you aren't creating gigabytes files, probably work just fine these days.

I'd stay away from word wrap I hear it turns out to be 20x harder than most people think

Fortunately all the other projects are ones that I actually have done. Just never bothered with a text editor yet lol.

31

u/pm_plz_im_lonely Dec 14 '19

Maybe you should bother.

12

u/Eirenarch Dec 14 '19

Basically still just O(n) time.

The array solution is O(N) in the length of the whole text, the proper solution is O(1) in the length of the text and O(N) in the length of the inserted text.

-10

u/MetalSlug20 Dec 15 '19

For just byte data computers are well fast enough to not even care about this for simple text

7

u/Eirenarch Dec 15 '19

And then you open a 1GB log file and your editor freezes

-1

u/MetalSlug20 Dec 15 '19

Just like windows notepad does? Which is well enough for small text files..

6

u/Eirenarch Dec 15 '19

Well, notepad is not a very high goal.

1

u/[deleted] Dec 15 '19

[removed] — view removed comment

3

u/Eirenarch Dec 15 '19

Probably but you might also go for Visual Studio or IntelliJ IDEA

→ More replies (1)

1

u/ismtrn Dec 15 '19

You end up allocating and freeing lots of memory all the time. This is not very fast even if the time complexity is "only" linear.

24

u/[deleted] Dec 14 '19

Create a whole new array on insert? My my what an efficient piece of code that would be. You should win a prize.

→ More replies (7)

0

u/[deleted] Dec 15 '19

[deleted]

-2

u/Narishma Dec 15 '19

Depending on the size of your list, linear may be faster than binary search.

8

u/Y_Less Dec 15 '19

Look up the "rope" data type.

0

u/MetalSlug20 Dec 15 '19

I did. Thanks

7

u/flatfinger Dec 14 '19

A nice approach is to use a "gap buffer", which keeps all text as two contiguous blocks, one of which is located at the start of the buffer and one at the end. Any time one needs to perform an operation which isn't at the gap, one would move all the text between the gap location and the new edit location to the other end of the buffer.

As for word wrap, the complexity of that depends upon whether one is using proportional fonts and/or Unicode. Proportional fonts add some complexity, but nothing overly difficult. Full Unicode support, however, is another story. Even not counting font shapes, I'm not sure it would be possible to implement an HTML-canvas based text editor with full Unicode support, which uses fillText for all its text rendering (and no external references), in less than a megabyte of Javascript. I'd even regard a "render paragraph" function that handles all applicable corner cases involving grapheme clusters, zero-width joins, bidirectional text, Emoji skin tone modifiers, etc. in less than a megabyte as being rather impressive.

-56

u/MetalSlug20 Dec 15 '19 edited Dec 15 '19

Jesus Christ people downvotes this? You guys are so damn harsh. Maybe have a fucking conversation?

How many lines typically are in a basic text editor! Even windows notepad chokes in large files.

If you for example use it for a programming scratchpad well the longest since file I ever had was maybe 8000 lines long. And say 80 characters wide. They ain't shit for memory. You could literally just make a whole new array each time without problems. Not even something to worry about in modern computers

I get some of you guys are data structure nerds and I never said it wasn't worth using them. I just said you probably could get away with not even bothering to do so anymore, unless you are planning on working on some giant manuscripts. And for just text O(n) is plenty fast

Fucking premature optimizers the lot of ya

Even my solution is faster than O(n) if you resize the array in place (or even just allocate a big ass chunk of ram ahead of time why not) you only have to move the characters after the insert to make room!

46

u/_3442 Dec 15 '19

You must love Electron

9

u/robchroma Dec 15 '19

The longest files I have during programming are million-line log files. Open that, do one edit command, and watch the computer churn a little too long.

0

u/[deleted] Dec 16 '19

[deleted]

2

u/robchroma Dec 16 '19

Characterizing random processes, and to create summary statistics.

7

u/[deleted] Dec 16 '19

You could literally just make a whole new array each time without problems. Not even something to worry about in modern computers

This is what webshits actually believe.

7

u/unholyground Dec 16 '19

Good that you get downvoted into oblivion, where the rest of you poisonous morons belong.

Premature optimization is only premature in situations where performance is clearly irrelevant or when optimizations should be delayed for the sake of achieving a reference that can be measured against.

In those situations, your optimizations should always be easy to integrate or add to the system.

Resources are also always limited.

So shut the fuck up, you dumb fucking code monkey. You know nothing and do not deserve to speak. Your words are cancerous and reek of the filth that this industry is plagued with.

→ More replies (3)