diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-08-24 17:25:35 +0100 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-08-24 17:25:35 +0100 |
| commit | c8f7879655125bcbf18f41897cf3f7e7385693aa (patch) | |
| tree | 9678045cd73cbcd02d9cc95e7969d580a76b3ef9 /queue/elementarypriority.go | |
| parent | b6329c9c286b09d2f974fd134cbe3029b1e4e11f (diff) | |
refactor
Diffstat (limited to 'queue/elementarypriority.go')
| -rw-r--r-- | queue/elementarypriority.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/queue/elementarypriority.go b/queue/elementarypriority.go new file mode 100644 index 0000000..508cbcb --- /dev/null +++ b/queue/elementarypriority.go @@ -0,0 +1,60 @@ +package queue + +import "algorithms/ds" + +type ElementaryPriority struct { + a ds.ArrayList + size int + // Initial capacity + capacity int +} + +func NewElementaryPriority(capacity int) *ElementaryPriority { + return &ElementaryPriority{make(ds.ArrayList, 0, capacity), 0, capacity} +} + +func (q *ElementaryPriority) Insert(a int) { + q.a = append(q.a, a) + q.size++ +} + +func (q *ElementaryPriority) Max() (max int) { + _, max = q.max() + return +} + +func (q *ElementaryPriority) DeleteMax() int { + if q.Empty() { + return 0 + } + + ind, max := q.max() + for i := ind + 1; i < q.Size(); i++ { + q.a[i-1] = q.a[i] + } + q.size-- + + return max +} + +func (q *ElementaryPriority) Empty() bool { + return q.Size() == 0 +} + +func (q *ElementaryPriority) Size() int { + return q.size +} + +func (q *ElementaryPriority) Clear() { + q.size = 0 + q.a = make(ds.ArrayList, 0, q.capacity) +} + +func (q *ElementaryPriority) max() (ind, max int) { + for i, a := range q.a { + if a > max { + ind, max = i, a + } + } + return ind, max +} |
