Hone logo
Hone
Problems

Pointer Compression in Rust

Pointer compression is a technique used to reduce the memory footprint of pointers, particularly in environments where pointers are frequently used and memory is constrained. This challenge asks you to implement a simplified version of pointer compression in Rust, focusing on the core logic of mapping original pointers to compressed values and vice versa. This is useful for embedded systems, game development, or any scenario where minimizing memory usage is critical.

Problem Description

You are tasked with implementing a PointerCompressor struct in Rust that can compress and decompress pointers. The compressor will use a fixed-size buffer to store the compressed values. The core functionality involves mapping original pointers (represented as usize) to smaller, compressed values (also usize) and back. The compression scheme is simple: a hash function maps the original pointer to a compressed value within the buffer's capacity. Collision handling is not required for this challenge; assume the buffer is large enough to avoid collisions.

Key Requirements:

  • Implement a PointerCompressor struct with a capacity field (usize) representing the size of the compression buffer.
  • Implement a compress method that takes an original pointer (usize) as input and returns its compressed representation (usize).
  • Implement a decompress method that takes a compressed value (usize) as input and returns the original pointer (usize).
  • The compress and decompress methods should be inverses of each other; that is, decompress(compress(ptr)) should return ptr.
  • The compression function should be a simple hash function: compressed_value = ptr % capacity.

Expected Behavior:

The compress method should return a value between 0 and capacity - 1 (inclusive). The decompress method should return the original pointer value.

Edge Cases to Consider:

  • capacity being zero. Handle this gracefully (e.g., return 0 for compression, panic for decompression).
  • Large pointer values. The modulo operation should handle these correctly.

Examples

Example 1:

Input: compressor = PointerCompressor { capacity: 10 }; ptr = 12345;
Output: compressed_value = 5; original_ptr = 12345;
Explanation: compress(12345) = 12345 % 10 = 5. decompress(5) = 5.

Example 2:

Input: compressor = PointerCompressor { capacity: 256 }; ptr = 65535;
Output: compressed_value = 255; original_ptr = 65535;
Explanation: compress(65535) = 65535 % 256 = 255. decompress(255) = 255.

Example 3: (Edge Case)

Input: compressor = PointerCompressor { capacity: 0 }; ptr = 42;
Output: compressed_value = 0;
Explanation: When capacity is 0, compression always returns 0. Decompression with capacity 0 should panic.

Constraints

  • capacity will be a non-negative usize.
  • Pointers (usize) can be arbitrarily large.
  • The compression function is ptr % capacity.
  • The compress method should return a value within the range [0, capacity - 1].
  • The decompress method should return the original pointer value.
  • Performance is not a primary concern for this challenge; clarity and correctness are more important.

Notes

  • Consider using Rust's panic! macro to handle invalid states, such as attempting to decompress with a zero capacity.
  • The focus is on the core compression/decompression logic. Error handling beyond the zero-capacity case is not required.
  • Think about how to structure your code to ensure that compress and decompress are inverses of each other. The modulo operator is key to achieving this.
  • This is a simplified model. Real-world pointer compression involves more sophisticated techniques to handle collisions and maintain performance.
Loading editor...
rust