Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greedy dual #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions greedydual/greedydual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2013 Alexandre Passos

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package greedydual

// Cache is a cache. It is not safe for concurrent access.
type Cache struct {
// OnEvicted optionally specificies a callback function to be
// executed when an entry is purged from the cache.
OnEvicted func(key string, value interface{})

operations float64
Heap *Heap
cache map[interface{}]*HeapItem
}

type entry struct {
key string
weights float64
value interface{}
}

// New creates a new Cache.
// If maxEntries is zero, the cache has no limit and it's assumed
// that eviction is done by the caller.
func New() *Cache {
return &Cache{
Heap: NewHeap(),
operations: 0,
cache: make(map[interface{}]*HeapItem),
}
}

// Add adds a value to the cache.
func (c *Cache) Add(key string, value interface{}, cost float64) {
if ele, ok := c.cache[key]; ok {
ee := ele.Value.(*entry)
ee.value = value
priority := c.operations + ee.weights
c.Heap.Reinsert(ele.Position, priority)
return
}
entry := &entry{
key: key,
value: value,
weights: cost,
}
priority := c.operations + cost
ele := c.Heap.Insert(entry, priority)
c.cache[key] = ele
}

// Get looks up a key's value from the cache.
func (c *Cache) Get(key string) (value interface{}, ok bool) {
if ele, hit := c.cache[key]; hit {
ee := ele.Value.(*entry)
priority := c.operations + ee.weights
c.Heap.Reinsert(ele.Position, priority)
return ele.Value.(*entry).value, true
}
return
}

// RemoveOldest removes the oldest item from the cache.
func (c *Cache) RemoveOldest() {
if c.Heap.Size > 0 {
ele := c.Heap.Pop()
c.operations = ele.Priority
kv := ele.Value.(*entry)
delete(c.cache, kv.key)
if c.OnEvicted != nil {
c.OnEvicted(kv.key, kv.value)
}
}
}

// Len returns the number of items in the cache.
func (c *Cache) Len() int {
return c.Heap.Size
}
110 changes: 110 additions & 0 deletions greedydual/heap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package greedydual

type HeapItem struct {
Priority float64
Position int
Value interface{}
}

type Heap struct {
Size int
elements []*HeapItem
}

func NewHeap() *Heap {
return &Heap{
Size: 0,
elements: make([]*HeapItem, 0, 10),
}
}

func (heap *Heap) Swap(i, j int) {
a := heap.elements[i]
heap.elements[i] = heap.elements[j]
heap.elements[j] = a
heap.elements[i].Position = i
heap.elements[j].Position = j
}

func less(i, j *HeapItem) bool {
pi := i.Priority
pj := j.Priority
return pi <= pj
}

func (heap *Heap) Up(index int) {
for {
parent := (index - 1) / 2 // parent
if parent == index || less(heap.elements[parent], heap.elements[index]) {
break
}
heap.Swap(parent, index)
index = parent
}
}

func (heap *Heap) Down(index int) {
for {
left := 2*index + 1
if left >= heap.Size || left < 0 {
break
}
child := left
right := left + 1
if right < heap.Size {
if !less(heap.elements[left], heap.elements[right]) {
child = right
}
}
if !less(heap.elements[child], heap.elements[index]) {
break
}
heap.Swap(index, child)
index = child
}
}

func (heap *Heap) Push(element *HeapItem) {
element.Position = heap.Size
heap.Size += 1
heap.elements = append(heap.elements, element)
heap.Up(element.Position)
}

func (heap *Heap) Insert(element interface{}, Priority float64) *HeapItem {
item := &HeapItem{
Value: element,
Priority: Priority,
Position: -1,
}
heap.Push(item)
return item
}

func (heap *Heap) Pop() *HeapItem {
n := heap.Size - 1
heap.Swap(0, n)
e := heap.elements[n]
heap.Size -= 1
heap.elements = heap.elements[0:heap.Size]
heap.Down(0)
return e
}

func (heap *Heap) Reinsert(index int, Priority float64) {
e := heap.elements[index]
for {
if e.Position == 0 {
break
}
heap.Swap(e.Position, (e.Position-1)/2)
}
heap.Pop()
e.Priority = Priority
heap.Push(e)
}


func (heap *Heap) Head() *HeapItem {
return heap.elements[0]
}
Loading