I'm working on an event-driven particle simulation. The code below defines the struct Event and implements the equality traits that are needed for a binary heap. This code is working but the object event built by the second constructor (new_event) has redundant member variables. I want to have a binary heap which works with two different event structs in order to avoid this redundancy and improve my code efficiency which is crucial for my project. Can I achieve this using traits?
pub struct Event
{
pub time: f64,
pub event_type: EventType,
pub particle_index1: usize,
pub particle_index2: usize,
pub particle_count1: usize,
pub particle_count2: usize
}
impl Event
{
pub fn new_collision(time: f64, particle_index1: usize, particle_index2: usize,
particle_count1: usize, particle_count2: usize) -> Self
{
Self {
time,
particle_index1,
particle_index2,
event_type: EventType::EventCollision,
particle_count1,
particle_count2
}
}
pub fn new_event(time: f64, event_type: EventType, particle_index1: usize, particle_count1:
usize) -> Self
{
Self {
time,
particle_index1,
particle_index2: 0, //REDUNDANT
event_type,
particle_count1,
particle_count2: 0 //REDUNDANT
}
}
}
impl PartialEq for Event
{
fn eq(&self, other: &Self) -> bool
{
self.time == other.time
}
}
impl Eq for Event{}
impl PartialOrd for Event
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
{
other.time.partial_cmp(&self.time) //reverse order
}
}
impl Ord for Event
{
fn cmp(&self, other: &Self) -> Ordering
{
self.partial_cmp(other).unwrap()
}
}