English | 简体中文
A JavaScript library for managing intensity segments over a timeline. This library provides a robust way to handle overlapping intensity changes and maintain a clean, efficient representation of intensity values over time.
- Efficient intensity value management over time
- Automatic segment merging and optimization
- Zero-intensity segment optimization
- Efficient caching mechanism for intensity queries
- Support for both browser and Node.js environments
npm install intensity-segments
import { IntensitySegments } from 'intensity-segments';
// Create a new instance with custom cache configuration
const segments = new IntensitySegments({
maxSize: 1000, // Maximum cache size
ttl: 5000 // Cache TTL in milliseconds
});
// Add intensity to a range
segments.add(10, 20, 2); // Adds intensity of 2 from time 10 to 20
// Set intensity for a range
segments.set(15, 25, 3); // Sets intensity to 3 from time 15 to 25
// Query intensity at a specific point
const intensity = segments.getIntensityAt(18); // Get intensity at time 18
// Get segment information at a specific point
const segment = segments.getSegmentAt(18); // Get segment containing time 18
// Returns: { start: 15, end: 25, intensity: 3 }
// Get string representation
console.log(segments.toString()); // Shows current segments
The IntensitySegments
class uses an array of [point, intensity]
pairs to represent intensity changes over time. Each pair represents:
point
: The starting point of a segmentintensity
: The intensity value for that segment
For example, [[0, 1], [10, 2], [20, 0]]
represents:
- Intensity of 1 from point 0 to 10
- Intensity of 2 from point 10 to 20
- Intensity of 0 after point 20
Adds an intensity change to a specified range:
- Converts existing segments to point changes
- Adds new intensity changes
- Recalculates all segments
Example:
// Initial state: [[0, 1], [10, 0]]
segments.add(5, 15, 2);
// Result: [[0, 1], [5, 3], [10, 2], [15, 0]]
Sets absolute intensity for a range:
- Preserves segments before the range
- Sets new intensity for the range
- Preserves segments after the range
- Normalizes the resulting segments
Example:
// Initial state: [[0, 1], [10, 2], [20, 1]]
segments.set(5, 15, 3);
// Result: [[0, 1], [5, 3], [15, 1], [20, 0]]
Gets the intensity value at a specific time point:
- Checks cache for existing value
- If not in cache, performs binary search
- Updates cache with new value
- Returns the intensity value
Example:
// State: [[0, 1], [10, 2], [20, 0]]
const intensity = segments.getIntensityAt(15); // Returns 2
Gets detailed information about the segment containing a specific time point:
- Returns null if time is before first segment or after last non-zero segment
- Returns segment information including start time, end time, and intensity
Example:
// State: [[0, 1], [10, 2], [20, 0]]
const segment = segments.getSegmentAt(15);
// Returns: { start: 10, end: 20, intensity: 2 }
-
Caching Mechanism
- LRU (Least Recently Used) cache for intensity queries
- Configurable cache size and TTL
- Automatic cache cleanup for expired entries
-
Zero Handling
- Removes unnecessary zero-intensity segments
- Skips leading zero segments
- Preserves zero segments between non-zero segments
- Optimizes trailing zero segments
-
Segment Merging
- Automatically merges adjacent segments with the same intensity
- Removes redundant points
- Maintains minimal segment representation
-
Edge Cases
- Handles overlapping segments correctly
- Supports negative intensity values
- Maintains proper segment boundaries
const segments = new IntensitySegments();
segments.add(0, 10, 1); // [[0, 1], [10, 0]]
segments.add(5, 15, 2); // [[0, 1], [5, 3], [10, 2], [15, 0]]
const segments = new IntensitySegments();
segments.add(0, 20, 1); // [[0, 1], [20, 0]]
segments.set(10, 30, 2); // [[0, 1], [10, 2], [30, 0]]
const segments = new IntensitySegments();
segments.add(0, 10, 2); // [[0, 2], [10, 0]]
segments.add(0, 10, -2); // Empty result (all zeros removed)
const segments = new IntensitySegments({
maxSize: 1000, // Cache up to 1000 points
ttl: 5000 // Cache entries expire after 5 seconds
});
// First query performs calculation
const intensity1 = segments.getIntensityAt(15);
// Second query uses cached value
const intensity2 = segments.getIntensityAt(15); // Faster response
# Install dependencies
npm install
# Run development
npm run dev
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
# Run build
npm run build
.
├── src/ # Source files
├── tests/ # Test files
├── dist/ # Compiled files
├── package.json # Project configuration
└── README.md # Project documentation
This project is licensed under the MIT License - see the LICENSE file for details.