update lab1a go solution
This commit is contained in:
parent
062dfd9685
commit
cd9c0f52e9
@ -1,5 +1,5 @@
|
|||||||
# Lab1
|
# Lab1
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
go run limited_buffer.go main.go ../IF-1-1_PuzonasR_L1_dat_1.json IF-1-1_PuzonasR_L1_rez.txt
|
go run sorted_list.go limited_buffer.go main.go ../IF-1-1_PuzonasR_L1_dat_1.json IF-1-1_PuzonasR_L1_rez.txt
|
||||||
```
|
```
|
||||||
|
@ -10,21 +10,12 @@ type LimitedBuffer struct {
|
|||||||
mutex *sync.Mutex
|
mutex *sync.Mutex
|
||||||
updateCond *sync.Cond
|
updateCond *sync.Cond
|
||||||
|
|
||||||
estimatedSize int
|
estimatedCount int
|
||||||
estimatedSizeMutex *sync.Mutex
|
reservedCount int
|
||||||
|
|
||||||
consumedCount int
|
|
||||||
consumedMutex *sync.Mutex
|
|
||||||
|
|
||||||
processedCount int
|
|
||||||
processedMutex *sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeLimitedBuffer(bufferSize int) LimitedBuffer {
|
func makeLimitedBuffer(bufferSize int) LimitedBuffer {
|
||||||
var container = make([]DataEntry, bufferSize)
|
var container = make([]DataEntry, bufferSize)
|
||||||
var estimatedSizeMutex = sync.Mutex{}
|
|
||||||
var processedMutex = sync.Mutex{}
|
|
||||||
var consumedMutex = sync.Mutex{}
|
|
||||||
|
|
||||||
var itemsMutex = sync.Mutex{}
|
var itemsMutex = sync.Mutex{}
|
||||||
var cond = sync.NewCond(&itemsMutex)
|
var cond = sync.NewCond(&itemsMutex)
|
||||||
@ -32,9 +23,6 @@ func makeLimitedBuffer(bufferSize int) LimitedBuffer {
|
|||||||
items: container,
|
items: container,
|
||||||
mutex: &itemsMutex,
|
mutex: &itemsMutex,
|
||||||
updateCond: cond,
|
updateCond: cond,
|
||||||
estimatedSizeMutex: &estimatedSizeMutex,
|
|
||||||
processedMutex: &processedMutex,
|
|
||||||
consumedMutex: &consumedMutex,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,43 +51,14 @@ func (buffer *LimitedBuffer) Remove() DataEntry {
|
|||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *LimitedBuffer) IsDone() bool {
|
func (buffer *LimitedBuffer) ReserveEntry() bool {
|
||||||
return buffer.estimatedSize == buffer.processedCount
|
buffer.mutex.Lock()
|
||||||
}
|
defer buffer.mutex.Unlock()
|
||||||
|
|
||||||
func (buffer *LimitedBuffer) WaitUntilDone() {
|
if buffer.estimatedCount == buffer.reservedCount {
|
||||||
for !buffer.IsDone() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (buffer *LimitedBuffer) ConsumeEstimatedEntry() bool {
|
|
||||||
buffer.estimatedSizeMutex.Lock()
|
|
||||||
defer buffer.estimatedSizeMutex.Unlock()
|
|
||||||
|
|
||||||
if (buffer.estimatedSize == buffer.consumedCount) {
|
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
buffer.consumedCount += 1
|
buffer.reservedCount++
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (buffer *LimitedBuffer) IsEstimatedEmpty() bool {
|
|
||||||
buffer.estimatedSizeMutex.Lock()
|
|
||||||
defer buffer.estimatedSizeMutex.Unlock()
|
|
||||||
|
|
||||||
return buffer.estimatedSize == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (buffer *LimitedBuffer) UpdateEstimated(change int) {
|
|
||||||
buffer.estimatedSizeMutex.Lock()
|
|
||||||
defer buffer.estimatedSizeMutex.Unlock()
|
|
||||||
|
|
||||||
buffer.estimatedSize += change
|
|
||||||
}
|
|
||||||
|
|
||||||
func (buffer *LimitedBuffer) MarkAsProcessed() {
|
|
||||||
buffer.processedMutex.Lock()
|
|
||||||
defer buffer.processedMutex.Unlock()
|
|
||||||
|
|
||||||
buffer.processedCount += 1
|
|
||||||
}
|
|
||||||
|
@ -5,16 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const inputBufferSize = 10
|
const bufferSize = 10
|
||||||
const inputMonitorCount = 3
|
const threadCount = 3
|
||||||
|
|
||||||
const outputBufferSize = 10
|
|
||||||
const outputMonitorCount = 1
|
|
||||||
|
|
||||||
type DataEntry struct {
|
type DataEntry struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -22,48 +18,24 @@ type DataEntry struct {
|
|||||||
Criteria int `json:"criteria"`
|
Criteria int `json:"criteria"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterEntry(entry DataEntry) bool {
|
func processEntry(entry DataEntry, outputEntries *SortedList, outputMutex *sync.Mutex) {
|
||||||
time.Sleep(time.Millisecond * 100 + time.Millisecond * time.Duration(entry.Criteria))
|
time.Sleep(time.Millisecond * 100 + time.Millisecond * time.Duration(entry.Criteria))
|
||||||
return entry.Sugar > float32(entry.Criteria)
|
if entry.Sugar > float32(entry.Criteria) {
|
||||||
|
outputMutex.Lock()
|
||||||
|
fmt.Println("Output:", entry)
|
||||||
|
outputEntries.Append(entry)
|
||||||
|
outputMutex.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputEntry(outputEntries *[]DataEntry, entry DataEntry) {
|
func processEntries(monitor *LimitedBuffer, outputEntries *SortedList, outputMutex *sync.Mutex, waitGroup *sync.WaitGroup) {
|
||||||
time.Sleep(time.Millisecond * 200)
|
for monitor.ReserveEntry() {
|
||||||
fmt.Println("Output:", entry)
|
entry := monitor.Remove()
|
||||||
*outputEntries = append(*outputEntries, entry)
|
|
||||||
sort.Slice(*outputEntries, func(i, j int) bool {
|
|
||||||
return (*outputEntries)[i].Sugar < (*outputEntries)[j].Sugar
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func filterEntries(inputBuffer *LimitedBuffer, outputBuffer *LimitedBuffer) {
|
|
||||||
for inputBuffer.ConsumeEstimatedEntry() {
|
|
||||||
entry := inputBuffer.Remove()
|
|
||||||
fmt.Println("Started to filter:", entry)
|
fmt.Println("Started to filter:", entry)
|
||||||
isFiltered := filterEntry(entry)
|
processEntry(entry, outputEntries, outputMutex)
|
||||||
fmt.Println("Finished to filter:", entry)
|
fmt.Println("Finished to filter:", entry)
|
||||||
if (isFiltered) {
|
|
||||||
outputBuffer.UpdateEstimated(+1)
|
|
||||||
outputBuffer.Insert(entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
inputBuffer.MarkAsProcessed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func outputEntries(outputBuffer *LimitedBuffer, inputBuffer *LimitedBuffer, outputList *[]DataEntry, outputMutex *sync.Mutex) {
|
|
||||||
for !inputBuffer.IsDone() {
|
|
||||||
for outputBuffer.ConsumeEstimatedEntry() {
|
|
||||||
entry := outputBuffer.Remove()
|
|
||||||
outputMutex.Lock()
|
|
||||||
fmt.Println("Started to output:", entry)
|
|
||||||
outputEntry(outputList, entry)
|
|
||||||
fmt.Println("Finished to output:", entry)
|
|
||||||
outputMutex.Unlock()
|
|
||||||
|
|
||||||
outputBuffer.MarkAsProcessed()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
waitGroup.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -85,25 +57,22 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
outputList := []DataEntry{}
|
outputList := makeSortedList(len(entries))
|
||||||
|
|
||||||
inputBuffer := makeLimitedBuffer(inputBufferSize)
|
|
||||||
outputBuffer := makeLimitedBuffer(outputBufferSize)
|
|
||||||
outputMutex := sync.Mutex{}
|
outputMutex := sync.Mutex{}
|
||||||
inputBuffer.UpdateEstimated(len(entries))
|
waitGroup := sync.WaitGroup{}
|
||||||
|
|
||||||
for i := 0; i < inputMonitorCount; i++ {
|
monitor := makeLimitedBuffer(bufferSize)
|
||||||
go filterEntries(&inputBuffer, &outputBuffer)
|
monitor.estimatedCount = len(entries)
|
||||||
}
|
|
||||||
for i := 0; i < outputMonitorCount; i++ {
|
for i := 0; i < threadCount; i++ {
|
||||||
go outputEntries(&outputBuffer, &inputBuffer, &outputList, &outputMutex)
|
go processEntries(&monitor, &outputList, &outputMutex, &waitGroup)
|
||||||
|
waitGroup.Add(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
inputBuffer.Insert(entry);
|
monitor.Insert(entry);
|
||||||
}
|
}
|
||||||
inputBuffer.WaitUntilDone()
|
waitGroup.Wait()
|
||||||
outputBuffer.WaitUntilDone()
|
|
||||||
|
|
||||||
outputFile, err := os.OpenFile(outputFilename, os.O_TRUNC | os.O_CREATE | os.O_WRONLY, 0644)
|
outputFile, err := os.OpenFile(outputFilename, os.O_TRUNC | os.O_CREATE | os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -111,7 +80,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer outputFile.Close()
|
defer outputFile.Close()
|
||||||
|
|
||||||
outputListBytes, err := json.MarshalIndent(outputList, "", "\t")
|
outputListBytes, err := json.MarshalIndent(outputList.GetSlice(), "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
31
lab1a/go/sorted_list.go
Normal file
31
lab1a/go/sorted_list.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type SortedList struct {
|
||||||
|
items []DataEntry
|
||||||
|
count int
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeSortedList(capacity int) SortedList {
|
||||||
|
var items = make([]DataEntry, capacity)
|
||||||
|
return SortedList{
|
||||||
|
items: items,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *SortedList) Append(item DataEntry) {
|
||||||
|
if len(self.items) == self.count {
|
||||||
|
panic("SortedList is full")
|
||||||
|
}
|
||||||
|
|
||||||
|
self.items[self.count] = item
|
||||||
|
self.count++
|
||||||
|
for i := self.count-1; i >= 1; i-- {
|
||||||
|
if self.items[i].Sugar > self.items[i-1].Sugar { break }
|
||||||
|
|
||||||
|
self.items[i], self.items[i-1] = self.items[i-1], self.items[i] // Swap elements [i] and [i-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *SortedList) GetSlice() []DataEntry {
|
||||||
|
return self.items[:self.count]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user