Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/freddiehaddad/justification
Multithreaded Text Justification
https://github.com/freddiehaddad/justification
concurrency concurrent-programming go golang leetcode leetcode-go leetcode-golang leetcode-solution multithreading parallel-programming
Last synced: 14 days ago
JSON representation
Multithreaded Text Justification
- Host: GitHub
- URL: https://github.com/freddiehaddad/justification
- Owner: freddiehaddad
- Created: 2024-03-01T04:20:49.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-03-01T15:39:11.000Z (11 months ago)
- Last Synced: 2025-01-03T03:48:41.887Z (21 days ago)
- Topics: concurrency, concurrent-programming, go, golang, leetcode, leetcode-go, leetcode-golang, leetcode-solution, multithreading, parallel-programming
- Language: Go
- Homepage: https://leetcode.com/problems/text-justification/description/?envType=study-plan-v2&envId=top-interview-150
- Size: 3.91 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Multithreaded Text Justification
Program that reads an array of words and generates an array of strings made up
of the words fully justified such that each array element's length is set to the
specified width.The program works by reading words from the input array until the total width
with spaces would exceed the specified line width. A thread is then created that
fully justifies the words and creates a string. The process repeats for all
lines excluding the last line. The last line is left justified in in a separate
thread as well.In addition to the words provided as input to the justification threads, the
line number is also provided.When the threads are finished justifying the line, the data is written to a
channel.Another thread waiting for input from the channel writes the justified lines to
an array preserving the order by the provided line number. The array is
pre-sized to the maximum number in order to handle asynchronous arrival of lines
and to avoid the need for a synchronization primitive (i.e. mutex). This is an
example of a lock free algorithm.```text
+------------------------------------------+
| ["foo", "bar", "baz", "foobar", ...] G |
| |
| |
| FullJustifiy() |
+---------------------+--------------------+
|
+------------------------------------+
|
v
+----------------------------------+ +--------------------------+
| ["foo bar", "baz ", ...] G | | ["foo", "bar"] G |-+
| | | | |-+
| |<------+ | | |
| processIncomingLines() | | *JustifyLine() | | |
+----------------------------------+ +--------------------------+ | |
+--------------------------+ |
+--------------------------+G: Go Routine
```## Examples
```Go
words := []string{
"Science", "is", "what", "we", "understand", "well", "enough", "to",
"explain", "to", "a", "computer.", "Art", "is", "everything", "else",
"we", "do",
}
width := 20
result := []string{
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do ",
},
```