Skip to content

Instantly share code, notes, and snippets.

deque-blog

Block or report user

Report or block deque-blog

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View LRUResourceCache.java
package com.murex.booking.sequence.sweeper.utils;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
public class LRUResourceCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
private final Consumer<V> closeResource;
View BadLRUCache.java
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
public LRUCache(int capacity) {
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> oldest) {
return size() > capacity;
View wasted_lru_cache.py
@functools.lru_cache(maxsize=2)
def long_computation(arg):
print("Long computation", arg)
return ...
for arg in [1,2,3] * 2:
long_computation(arg)
# Will print (only cache misses!)
"Long computation 1"
View lru_cache.py
@functools.lru_cache(maxsize=YOUR_CACHE_SIZE)
def long_computation(arg1, arg2, arg3):
pass
View LRUCache.py
class LRUCache:
def __init__(self, max_size):
self.dict = OrderedDict()
self.max_size = max_size
def __len__(self):
return len(self.dict)
def __contains__(self, key):
if key in self.dict:
View LRUCache.java
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> oldest) {
View PytharogianRectangles.cpp
// The algorithm itself
xt::xarray<int> next_pytharogian_triangles(xt::xarray<int> const& previous_stage)
{
static const xt::xarray<int> stacked_matrices = {
{ -1, 2, 2 },
{ -2, 1, 2 },
{ -2, 2, 3 },
{ 1, 2, 2 },
{ 2, 1, 2 },
{ 2, 2, 3 },
View pythagorean_triples_filtered.py
def pythagorean_triples(filter):
current = np.array([[3, 4, 5]]) # Initial seed
yield from current # Yield first triple
while current.shape[0]: # While work to do
current = next_pythagorean_triples(current) # Next iteration
current = filter(current) # Filter desired triples
yield from current # Yield each triple
View pythagorean_triples_test.py
[3 4 5]
[15 8 17]
[21 20 29]
[ 5 12 13]
[35 12 37]
[65 72 97]
[33 56 65]
[77 36 85]
...
View pythagorean_triples.py
def pythagorean_triples():
current = np.array([[3, 4, 5]]) # Initial seed
yield from current # Yield first triple
while True:
current = next_pythagorean_triples(current) # Next iteration
yield from current # Yield each triple
You can’t perform that action at this time.