first implementation finished
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import 'dart:collection';
|
||||
|
||||
class LRUCache<K, V> {
|
||||
int _capacity;
|
||||
final LinkedHashMap<K, V> _cache;
|
||||
|
||||
LRUCache(
|
||||
this._capacity, {
|
||||
bool Function(K, K)? equals,
|
||||
int Function(K)? hashCode,
|
||||
bool Function(dynamic)? isValidKey,
|
||||
}) : _cache = LinkedHashMap<K, V>(
|
||||
equals: equals,
|
||||
hashCode: hashCode,
|
||||
isValidKey: isValidKey,
|
||||
);
|
||||
|
||||
void resize(int capacity) {
|
||||
if (_capacity > capacity) {
|
||||
_cache.keys.take(_capacity - capacity).forEach(_cache.remove);
|
||||
}
|
||||
|
||||
_capacity = capacity;
|
||||
}
|
||||
|
||||
V? get(K key) {
|
||||
final value = _cache.remove(key); // Temporarily remove the item.
|
||||
|
||||
if (value != null) {
|
||||
_cache[key] =
|
||||
value; // Re-inserting the item makes it the most-recently used.
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void set(K key, V value) {
|
||||
if (_cache.containsKey(key)) {
|
||||
_cache.remove(key); // Remove the existing item before updating.
|
||||
} else if (_cache.length == _capacity) {
|
||||
_cache.remove(
|
||||
_cache.keys.first,
|
||||
); // Explicitly remove the least recently used item if at capacity.
|
||||
}
|
||||
|
||||
_cache[key] = value; // Inserting or updating the item.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
bool isAndroid() {
|
||||
return !kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
||||
}
|
||||
|
||||
bool isIOS() {
|
||||
return !kIsWeb && defaultTargetPlatform == TargetPlatform.iOS;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void showErrorMessage(BuildContext context, String message) {
|
||||
final snackBar = SnackBar(
|
||||
content: Text(
|
||||
message,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
),
|
||||
backgroundColor: Theme.of(context).colorScheme.onError,
|
||||
);
|
||||
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
Uri? tryParseUrl(String? input) {
|
||||
if (input != null) {
|
||||
final uri = Uri.tryParse(input);
|
||||
if (uri != null &&
|
||||
uri.hasAuthority &&
|
||||
(uri.isScheme('http') || uri.isScheme('https') || !uri.hasScheme)) {
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user