make methods public
This commit is contained in:
		
							parent
							
								
									1b3ca7b515
								
							
						
					
					
						commit
						eb82d79371
					
				
					 2 changed files with 110 additions and 110 deletions
				
			
		|  | @ -1,15 +1,15 @@ | ||||||
| package priority_queue | package priority_queue | ||||||
| 
 | 
 | ||||||
| type IndexPriorityQueue[T any] interface { | type IndexPriorityQueue[T any] interface { | ||||||
| 	top() T                    // get item with biggest priority | 	Top() T                    // get item with biggest priority | ||||||
| 	topIndex() int             // get index of an item with biggest priority | 	TopIndex() int             // get index of an item with biggest priority | ||||||
| 	remove() T                 // removes item with the biggest priority | 	Remove() T                 // removes item with the biggest priority | ||||||
| 	removeAtIndex(index int) T // removes item at specified index | 	RemoveAtIndex(index int) T // removes item at specified index | ||||||
| 	insert(index int, item T)  // adds item at specified index | 	Insert(index int, item T)  // adds item at specified index | ||||||
| 	change(index int, item T)  // changes item at specified index and preserves ordering | 	Change(index int, item T)  // changes item at specified index and preserves ordering | ||||||
| 	contains(index int) bool   // checks if item exists at specified index | 	Contains(index int) bool   // checks if item exists at specified index | ||||||
| 	isEmpty() bool | 	IsEmpty() bool | ||||||
| 	size() int | 	Size() int | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type indexPriorityQueue[T any] struct { | type indexPriorityQueue[T any] struct { | ||||||
|  | @ -47,15 +47,15 @@ func NewIPQ[T any](less func(T, T) bool, indexSize int) IndexPriorityQueue[T] { | ||||||
| 	return &indexPriorityQueue[T]{n, items, pq, qp, less} | 	return &indexPriorityQueue[T]{n, items, pq, qp, less} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q *indexPriorityQueue[T]) top() T { | func (q *indexPriorityQueue[T]) Top() T { | ||||||
| 	return q.items[q.pq[0]] | 	return q.items[q.pq[0]] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q *indexPriorityQueue[T]) topIndex() int { | func (q *indexPriorityQueue[T]) TopIndex() int { | ||||||
| 	return q.pq[0] | 	return q.pq[0] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q *indexPriorityQueue[T]) insert(index int, item T) { | func (q *indexPriorityQueue[T]) Insert(index int, item T) { | ||||||
| 	q.pq[q.n] = index | 	q.pq[q.n] = index | ||||||
| 	q.qp[index] = q.n | 	q.qp[index] = q.n | ||||||
| 	q.items[index] = item | 	q.items[index] = item | ||||||
|  | @ -63,11 +63,11 @@ func (q *indexPriorityQueue[T]) insert(index int, item T) { | ||||||
| 	q.n++ | 	q.n++ | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q *indexPriorityQueue[T]) remove() T { | func (q *indexPriorityQueue[T]) Remove() T { | ||||||
| 	return q.removeAtIndex(q.topIndex()) | 	return q.RemoveAtIndex(q.TopIndex()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q *indexPriorityQueue[T]) removeAtIndex(index int) T { | func (q *indexPriorityQueue[T]) RemoveAtIndex(index int) T { | ||||||
| 	pivot := q.qp[index] | 	pivot := q.qp[index] | ||||||
| 	q.n-- | 	q.n-- | ||||||
| 	q.swap(pivot, q.n) | 	q.swap(pivot, q.n) | ||||||
|  | @ -80,21 +80,21 @@ func (q *indexPriorityQueue[T]) removeAtIndex(index int) T { | ||||||
| 	return q.items[index] | 	return q.items[index] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q *indexPriorityQueue[T]) change(index int, item T) { | func (q *indexPriorityQueue[T]) Change(index int, item T) { | ||||||
| 	q.items[index] = item | 	q.items[index] = item | ||||||
| 	q.swim(q.qp[index]) | 	q.swim(q.qp[index]) | ||||||
| 	q.sink(q.qp[index]) | 	q.sink(q.qp[index]) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (pq *indexPriorityQueue[_]) size() int { | func (pq *indexPriorityQueue[_]) Size() int { | ||||||
| 	return pq.n | 	return pq.n | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (pq *indexPriorityQueue[_]) isEmpty() bool { | func (pq *indexPriorityQueue[_]) IsEmpty() bool { | ||||||
| 	return pq.n == 0 | 	return pq.n == 0 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q *indexPriorityQueue[_]) contains(index int) bool { | func (q *indexPriorityQueue[_]) Contains(index int) bool { | ||||||
| 	return q.qp[index] != -1 | 	return q.qp[index] != -1 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -12,151 +12,151 @@ func TestNewIPQ(t *testing.T) { | ||||||
| 	q := NewIPQ(intDescending, 1) | 	q := NewIPQ(intDescending, 1) | ||||||
| 	assert.NotNil(t, q) | 	assert.NotNil(t, q) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 0, q.size()) | 	assert.Equal(t, 0, q.Size()) | ||||||
| 	assert.Equal(t, true, q.isEmpty()) | 	assert.Equal(t, true, q.IsEmpty()) | ||||||
| 	assert.Equal(t, -1, q.topIndex()) | 	assert.Equal(t, -1, q.TopIndex()) | ||||||
| 	// TODO: maybe should return nil? | 	// TODO: maybe should return nil? | ||||||
| 	// assert.Equal(t, 0, q.top()) | 	// assert.Equal(t, 0, q.Top()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestIPQInsert(t *testing.T) { | func TestIPQInsert(t *testing.T) { | ||||||
| 	pq := NewIPQ(intDescending, 10) | 	pq := NewIPQ(intDescending, 10) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(1, 3) | 	pq.Insert(1, 3) | ||||||
| 	assert.Equal(t, 3, pq.top()) | 	assert.Equal(t, 3, pq.Top()) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(2, 4) | 	pq.Insert(2, 4) | ||||||
| 	assert.Equal(t, 4, pq.top()) | 	assert.Equal(t, 4, pq.Top()) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(3, 1) | 	pq.Insert(3, 1) | ||||||
| 	assert.Equal(t, 4, pq.top()) | 	assert.Equal(t, 4, pq.Top()) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(4, 4) | 	pq.Insert(4, 4) | ||||||
| 	assert.Equal(t, 4, pq.top()) | 	assert.Equal(t, 4, pq.Top()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestMoreIPQInsert(t *testing.T) { | func TestMoreIPQInsert(t *testing.T) { | ||||||
| 	pq := NewIPQ(intDescending, 10) | 	pq := NewIPQ(intDescending, 10) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(1, 10) | 	pq.Insert(1, 10) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 1, pq.topIndex()) | 	assert.Equal(t, 1, pq.TopIndex()) | ||||||
| 	assert.Equal(t, 10, pq.top()) | 	assert.Equal(t, 10, pq.Top()) | ||||||
| 	assert.Equal(t, 1, pq.size()) | 	assert.Equal(t, 1, pq.Size()) | ||||||
| 	assert.Equal(t, true, pq.contains(1)) | 	assert.Equal(t, true, pq.Contains(1)) | ||||||
| 	assert.Equal(t, false, pq.isEmpty()) | 	assert.Equal(t, false, pq.IsEmpty()) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(2, 20) | 	pq.Insert(2, 20) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 2, pq.topIndex()) | 	assert.Equal(t, 2, pq.TopIndex()) | ||||||
| 	assert.Equal(t, 20, pq.top()) | 	assert.Equal(t, 20, pq.Top()) | ||||||
| 	assert.Equal(t, 2, pq.size()) | 	assert.Equal(t, 2, pq.Size()) | ||||||
| 	assert.Equal(t, true, pq.contains(2)) | 	assert.Equal(t, true, pq.Contains(2)) | ||||||
| 	assert.Equal(t, false, pq.isEmpty()) | 	assert.Equal(t, false, pq.IsEmpty()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestIPQRemove(t *testing.T) { | func TestIPQRemove(t *testing.T) { | ||||||
| 	pq := NewIPQ(intDescending, 10) | 	pq := NewIPQ(intDescending, 10) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(1, 10) | 	pq.Insert(1, 10) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 1, pq.topIndex()) | 	assert.Equal(t, 1, pq.TopIndex()) | ||||||
| 	assert.Equal(t, 10, pq.top()) | 	assert.Equal(t, 10, pq.Top()) | ||||||
| 	assert.Equal(t, 1, pq.size()) | 	assert.Equal(t, 1, pq.Size()) | ||||||
| 	assert.Equal(t, true, pq.contains(1)) | 	assert.Equal(t, true, pq.Contains(1)) | ||||||
| 	assert.Equal(t, false, pq.isEmpty()) | 	assert.Equal(t, false, pq.IsEmpty()) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(2, 20) | 	pq.Insert(2, 20) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 2, pq.topIndex()) | 	assert.Equal(t, 2, pq.TopIndex()) | ||||||
| 	assert.Equal(t, 20, pq.top()) | 	assert.Equal(t, 20, pq.Top()) | ||||||
| 	assert.Equal(t, 2, pq.size()) | 	assert.Equal(t, 2, pq.Size()) | ||||||
| 	assert.Equal(t, true, pq.contains(2)) | 	assert.Equal(t, true, pq.Contains(2)) | ||||||
| 	assert.Equal(t, false, pq.isEmpty()) | 	assert.Equal(t, false, pq.IsEmpty()) | ||||||
| 
 | 
 | ||||||
| 	removed := pq.remove() | 	removed := pq.Remove() | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 20, removed) | 	assert.Equal(t, 20, removed) | ||||||
| 	assert.Equal(t, 10, pq.top()) | 	assert.Equal(t, 10, pq.Top()) | ||||||
| 	assert.Equal(t, 1, pq.size()) | 	assert.Equal(t, 1, pq.Size()) | ||||||
| 	assert.Equal(t, false, pq.contains(2)) | 	assert.Equal(t, false, pq.Contains(2)) | ||||||
| 	assert.Equal(t, true, pq.contains(1)) | 	assert.Equal(t, true, pq.Contains(1)) | ||||||
| 	assert.Equal(t, false, pq.isEmpty()) | 	assert.Equal(t, false, pq.IsEmpty()) | ||||||
| 
 | 
 | ||||||
| 	removed = pq.remove() | 	removed = pq.Remove() | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 10, removed) | 	assert.Equal(t, 10, removed) | ||||||
| 	// TODO: should return nil? | 	// TODO: should return nil? | ||||||
| 	// assert.Equal(t, "", pq.top()) | 	// assert.Equal(t, "", pq.Top()) | ||||||
| 	assert.Equal(t, 0, pq.size()) | 	assert.Equal(t, 0, pq.Size()) | ||||||
| 	assert.Equal(t, false, pq.contains(1)) | 	assert.Equal(t, false, pq.Contains(1)) | ||||||
| 	assert.Equal(t, true, pq.isEmpty()) | 	assert.Equal(t, true, pq.IsEmpty()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestIPQRemoveAtIndex(t *testing.T) { | func TestIPQRemoveAtIndex(t *testing.T) { | ||||||
| 	pq := NewIPQ(intDescending, 10) | 	pq := NewIPQ(intDescending, 10) | ||||||
| 
 | 
 | ||||||
| 	// top -> 40 - 30 - 20 - 10 | 	// Top -> 40 - 30 - 20 - 10 | ||||||
| 	pq.insert(8, 10) | 	pq.Insert(8, 10) | ||||||
| 	pq.insert(5, 20) | 	pq.Insert(5, 20) | ||||||
| 	pq.insert(3, 30) | 	pq.Insert(3, 30) | ||||||
| 	pq.insert(4, 40) | 	pq.Insert(4, 40) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 40, pq.top()) | 	assert.Equal(t, 40, pq.Top()) | ||||||
| 	assert.Equal(t, 4, pq.topIndex()) | 	assert.Equal(t, 4, pq.TopIndex()) | ||||||
| 
 | 
 | ||||||
| 	// top -> 40 - 30 - 10 | 	// Top -> 40 - 30 - 10 | ||||||
| 	removed := pq.removeAtIndex(5) | 	removed := pq.RemoveAtIndex(5) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 20, removed) | 	assert.Equal(t, 20, removed) | ||||||
| 	assert.Equal(t, 40, pq.top()) | 	assert.Equal(t, 40, pq.Top()) | ||||||
| 	assert.Equal(t, 4, pq.topIndex()) | 	assert.Equal(t, 4, pq.TopIndex()) | ||||||
| 
 | 
 | ||||||
| 	// top -> 30 - 10 | 	// Top -> 30 - 10 | ||||||
| 	removed = pq.removeAtIndex(4) | 	removed = pq.RemoveAtIndex(4) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 40, removed) | 	assert.Equal(t, 40, removed) | ||||||
| 	assert.Equal(t, 30, pq.top()) | 	assert.Equal(t, 30, pq.Top()) | ||||||
| 	assert.Equal(t, 3, pq.topIndex()) | 	assert.Equal(t, 3, pq.TopIndex()) | ||||||
| 
 | 
 | ||||||
| 	// top -> 30 - 20 - 10 | 	// Top -> 30 - 20 - 10 | ||||||
| 	pq.insert(5, 20) | 	pq.Insert(5, 20) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 30, pq.top()) | 	assert.Equal(t, 30, pq.Top()) | ||||||
| 	assert.Equal(t, 3, pq.topIndex()) | 	assert.Equal(t, 3, pq.TopIndex()) | ||||||
| 
 | 
 | ||||||
| 	// top -> 10 | 	// Top -> 10 | ||||||
| 	removed = pq.removeAtIndex(3) | 	removed = pq.RemoveAtIndex(3) | ||||||
| 	assert.Equal(t, 30, removed) | 	assert.Equal(t, 30, removed) | ||||||
| 	removed = pq.removeAtIndex(5) | 	removed = pq.RemoveAtIndex(5) | ||||||
| 	assert.Equal(t, 20, removed) | 	assert.Equal(t, 20, removed) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 10, pq.top()) | 	assert.Equal(t, 10, pq.Top()) | ||||||
| 	assert.Equal(t, 8, pq.topIndex()) | 	assert.Equal(t, 8, pq.TopIndex()) | ||||||
| 	assert.Equal(t, 1, pq.size()) | 	assert.Equal(t, 1, pq.Size()) | ||||||
| 	assert.Equal(t, false, pq.contains(5)) | 	assert.Equal(t, false, pq.Contains(5)) | ||||||
| 	assert.Equal(t, false, pq.contains(4)) | 	assert.Equal(t, false, pq.Contains(4)) | ||||||
| 	assert.Equal(t, false, pq.contains(3)) | 	assert.Equal(t, false, pq.Contains(3)) | ||||||
| 	assert.Equal(t, true, pq.contains(8)) | 	assert.Equal(t, true, pq.Contains(8)) | ||||||
| 	assert.Equal(t, false, pq.isEmpty()) | 	assert.Equal(t, false, pq.IsEmpty()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestIndexChange(t *testing.T) { | func TestIndexChange(t *testing.T) { | ||||||
| 	pq := NewIPQ(intDescending, 10) | 	pq := NewIPQ(intDescending, 10) | ||||||
| 
 | 
 | ||||||
| 	pq.insert(1, 9) | 	pq.Insert(1, 9) | ||||||
| 	pq.insert(2, 8) | 	pq.Insert(2, 8) | ||||||
| 	pq.insert(3, 12) | 	pq.Insert(3, 12) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 12, pq.top()) | 	assert.Equal(t, 12, pq.Top()) | ||||||
| 
 | 
 | ||||||
| 	pq.change(3, 7) | 	pq.Change(3, 7) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 9, pq.top()) | 	assert.Equal(t, 9, pq.Top()) | ||||||
| 
 | 
 | ||||||
| 	pq.change(2, 10) | 	pq.Change(2, 10) | ||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, 10, pq.top()) | 	assert.Equal(t, 10, pq.Top()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestMultiwayMerge(t *testing.T) { | func TestMultiwayMerge(t *testing.T) { | ||||||
|  | @ -174,16 +174,16 @@ func TestMultiwayMerge(t *testing.T) { | ||||||
| 
 | 
 | ||||||
| 	for i, stream := range allStreams { | 	for i, stream := range allStreams { | ||||||
| 		rune, _, _ := stream.ReadRune() | 		rune, _, _ := stream.ReadRune() | ||||||
| 		pq.insert(i, string(rune)) | 		pq.Insert(i, string(rune)) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	for !pq.isEmpty() { | 	for !pq.IsEmpty() { | ||||||
| 		actual += string(pq.top()) | 		actual += string(pq.Top()) | ||||||
| 		streamIndex := pq.topIndex() | 		streamIndex := pq.TopIndex() | ||||||
| 		pq.remove() | 		pq.Remove() | ||||||
| 		rune, _, err := allStreams[streamIndex].ReadRune() | 		rune, _, err := allStreams[streamIndex].ReadRune() | ||||||
| 		if err != io.EOF { | 		if err != io.EOF { | ||||||
| 			pq.insert(streamIndex, string(rune)) | 			pq.Insert(streamIndex, string(rune)) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue