Diff
checker
텍스트
텍스트
이미지
문서
Excel
폴더
Legal
Enterprise
데스크톱
요금제
로그인
데스크톱 앱 다운로드
텍스트 비교
두 텍스트 파일의 차이점을 찾아보세요
도구
기록
실시간 편집
공백 변경 숨기기
변경 없는 행 숨기기
줄바꿈 비활성화
레이아웃
나란히 보기
합쳐 보기
비교 단위
스마트
단어
글자
텍스트 스타일
모양 변경
구문 강조
언어 선택
제외
텍스트 변환
첫 변경으로
수정
Diffchecker Desktop
가장 안전하게 Diffchecker를 사용하는 방법. 데스크톱 앱을 사용하면 비교 데이터가 외부로 전송되지 않습니다!
데스크톱 앱 받기
Untitled diff
생성일
작년
비교 결과 만료 없음
초기화
내보내기
공유
설명
19 삭제
행
총
삭제
글자
총
삭제
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
54 행
복사
31 추가
행
총
추가
글자
총
추가
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
63 행
복사
const std = @import("std");
const std = @import("std");
// Cache requires a function (lambda) to be called when there's no cache hit and an other function(args_to_u64) that can compute args
// Cache requires a function (lambda) to be called when there's no cache hit and an other function(args_to_u64) that can compute args
// of the function to u64 that becomes the identifier to compare same arguments.
// of the function to u64 that becomes the identifier to compare same arguments.
pub fn Cache(lambda: anytype, args_to_u64: fn (anytype) u64) type {
pub fn Cache(lambda: anytype, args_to_u64: fn (anytype) u64) type {
const lambda_info = @typeInfo(@TypeOf(lambda));
const lambda_info = @typeInfo(@TypeOf(lambda));
복사
복사됨
복사
복사됨
@compileLog(lambda_info);
if (lambda_info != .
@"fn"
) {
if (lambda_info != .
Fn
) {
@compileError("lambda should be a function type");
@compileError("lambda should be a function type");
}
}
복사
복사됨
복사
복사됨
const return_type = lambda_info.
Fn
.return_type orelse @compileError("No return type");
const return_type = lambda_info.
@"fn"
.return_type orelse @compileError("No return type");
@compileLog(
return_type
);
const InnerHashMap = std.HashMap(u64,
return_type
, struct {
pub fn hash(_: @This(), key: u64) u64 {
return key;
}
pub fn eql(_: @This(), a: u64, b: u64) bool {
return a == b;
}
}, 80);
return struct {
return struct {
복사
복사됨
복사
복사됨
_inner:
std.
HashMap
(u64, return_type, struct {
_inner:
Inner
HashMap
,
fn hash(_: @This(), key: u64) u64 {
return key;
}
fn eql(_: @This(), a: u64, b: u64) bool {
return a == b;
}
}, 80),
const Self = @This();
const Self = @This();
fn init(allocator: std.mem.Allocator) Cache(lambda, args_to_u64) {
fn init(allocator: std.mem.Allocator) Cache(lambda, args_to_u64) {
복사
복사됨
복사
복사됨
return Self{ ._inner =
std.StringHashMap(return_type)
.init(allocator) };
return Self{ ._inner =
InnerHashMap
.init(allocator) };
}
}
복사
복사됨
복사
복사됨
fn get(self: *Self, args: anytype)
return_type {
fn get(self: *Self, args: anytype)
!
return_type {
const key = args_to_u64(args);
const key = args_to_u64(args);
복사
복사됨
복사
복사됨
const value =
self._inner.get(key) orelse
@call(.
{}
, lambda, args);
if (self._inner.get(key)) |value| {
return value;
std.debug.print("Rertrieving from cache for the key: {d}\n", .{key});
return value;
} else {
std.debug.print("Computing function and storing in cache for the key: {d}\n", .{key});
const value =
@call(.
auto
, lambda, args);
try self._inner.put(key, value);
return value;
}
}
}
};
};
}
}
복사
복사됨
복사
복사됨
// Below tests
addition of two numbers
and cache
// Below tests
to cache
addition of two numbers
fn _add(a: u64, b: u64) u64 {
fn _add(a: u64, b: u64) u64 {
return a + b;
return a + b;
}
}
const HashAdd = struct {
const HashAdd = struct {
var allocator: std.mem.Allocator = undefined;
var allocator: std.mem.Allocator = undefined;
pub fn hash_add(args: anytype) u64 {
pub fn hash_add(args: anytype) u64 {
const temp = std.fmt.allocPrint(allocator, "{}:{}", .{ args[0], args[1] }) catch unreachable;
const temp = std.fmt.allocPrint(allocator, "{}:{}", .{ args[0], args[1] }) catch unreachable;
defer allocator.free(temp);
defer allocator.free(temp);
return std.hash_map.hashString(temp);
return std.hash_map.hashString(temp);
}
}
};
};
test "test_cache" {
test "test_cache" {
const allocator = std.testing.allocator;
const allocator = std.testing.allocator;
HashAdd.allocator = allocator;
HashAdd.allocator = allocator;
var cache = Cache(_add, HashAdd.hash_add).init(allocator);
var cache = Cache(_add, HashAdd.hash_add).init(allocator);
복사
복사됨
복사
복사됨
const value: u64 =
cache.get(.{ 1, 2 });
const value: u64 =
try
cache.get(.{ 1, 2 });
try std.testing.expectEqual(3, value);
try std.testing.expectEqual(3, value);
복사
복사됨
복사
복사됨
const cache_value: u64 = try cache.get(.{ 1, 2 });
try std.testing.expectEqual(3, cache_value);
cache._inner.deinit();
cache._inner.deinit();
}
}
복사
복사됨
복사
복사됨
저장된 비교 결과
원본
파일 열기
const std = @import("std"); // Cache requires a function (lambda) to be called when there's no cache hit and an other function(args_to_u64) that can compute args // of the function to u64 that becomes the identifier to compare same arguments. pub fn Cache(lambda: anytype, args_to_u64: fn (anytype) u64) type { const lambda_info = @typeInfo(@TypeOf(lambda)); @compileLog(lambda_info); if (lambda_info != .Fn) { @compileError("lambda should be a function type"); } const return_type = lambda_info.Fn.return_type orelse @compileError("No return type"); @compileLog(return_type); return struct { _inner: std.HashMap(u64, return_type, struct { fn hash(_: @This(), key: u64) u64 { return key; } fn eql(_: @This(), a: u64, b: u64) bool { return a == b; } }, 80), const Self = @This(); fn init(allocator: std.mem.Allocator) Cache(lambda, args_to_u64) { return Self{ ._inner = std.StringHashMap(return_type).init(allocator) }; } fn get(self: *Self, args: anytype) return_type { const key = args_to_u64(args); const value = self._inner.get(key) orelse @call(.{}, lambda, args); return value; } }; } // Below tests addition of two numbers and cache fn _add(a: u64, b: u64) u64 { return a + b; } const HashAdd = struct { var allocator: std.mem.Allocator = undefined; pub fn hash_add(args: anytype) u64 { const temp = std.fmt.allocPrint(allocator, "{}:{}", .{ args[0], args[1] }) catch unreachable; defer allocator.free(temp); return std.hash_map.hashString(temp); } }; test "test_cache" { const allocator = std.testing.allocator; HashAdd.allocator = allocator; var cache = Cache(_add, HashAdd.hash_add).init(allocator); const value: u64 = cache.get(.{ 1, 2 }); try std.testing.expectEqual(3, value); cache._inner.deinit(); }
수정본
파일 열기
const std = @import("std"); // Cache requires a function (lambda) to be called when there's no cache hit and an other function(args_to_u64) that can compute args // of the function to u64 that becomes the identifier to compare same arguments. pub fn Cache(lambda: anytype, args_to_u64: fn (anytype) u64) type { const lambda_info = @typeInfo(@TypeOf(lambda)); if (lambda_info != .@"fn") { @compileError("lambda should be a function type"); } const return_type = lambda_info.@"fn".return_type orelse @compileError("No return type"); const InnerHashMap = std.HashMap(u64, return_type, struct { pub fn hash(_: @This(), key: u64) u64 { return key; } pub fn eql(_: @This(), a: u64, b: u64) bool { return a == b; } }, 80); return struct { _inner: InnerHashMap, const Self = @This(); fn init(allocator: std.mem.Allocator) Cache(lambda, args_to_u64) { return Self{ ._inner = InnerHashMap.init(allocator) }; } fn get(self: *Self, args: anytype) !return_type { const key = args_to_u64(args); if (self._inner.get(key)) |value| { std.debug.print("Rertrieving from cache for the key: {d}\n", .{key}); return value; } else { std.debug.print("Computing function and storing in cache for the key: {d}\n", .{key}); const value = @call(.auto, lambda, args); try self._inner.put(key, value); return value; } } }; } // Below tests to cache addition of two numbers fn _add(a: u64, b: u64) u64 { return a + b; } const HashAdd = struct { var allocator: std.mem.Allocator = undefined; pub fn hash_add(args: anytype) u64 { const temp = std.fmt.allocPrint(allocator, "{}:{}", .{ args[0], args[1] }) catch unreachable; defer allocator.free(temp); return std.hash_map.hashString(temp); } }; test "test_cache" { const allocator = std.testing.allocator; HashAdd.allocator = allocator; var cache = Cache(_add, HashAdd.hash_add).init(allocator); const value: u64 = try cache.get(.{ 1, 2 }); try std.testing.expectEqual(3, value); const cache_value: u64 = try cache.get(.{ 1, 2 }); try std.testing.expectEqual(3, cache_value); cache._inner.deinit(); }
비교하기