Skip to content

Commit

Permalink
#51. Allocate ViewIds instead of hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Aug 19, 2023
1 parent a7efb61 commit 50590c3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::*;
use euclid::*;
use std::any::Any;
use std::any::TypeId;
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::ops;

Expand Down Expand Up @@ -55,6 +53,12 @@ pub struct Context {
/// Layout information for all views.
layout: HashMap<IdPath, LayoutBox>,

/// Allocated ViewIds.
view_ids: HashMap<IdPath, ViewId>,

/// Next allocated id.
next_id: ViewId,

/// Which views each touch (or mouse pointer) is interacting with.
pub(crate) touches: [ViewId; 16],

Expand Down Expand Up @@ -128,6 +132,8 @@ impl Context {
pub fn new() -> Self {
Self {
layout: HashMap::new(),
view_ids: HashMap::new(),
next_id: ViewId { id: 0 },
touches: [ViewId::default(); 16],
starts: [LocalPoint::zero(); 16],
previous_position: [LocalPoint::zero(); 16],
Expand Down Expand Up @@ -337,12 +343,14 @@ impl Context {
}

pub(crate) fn view_id(&mut self, path: &IdPath) -> ViewId {
let mut hasher = DefaultHasher::new();
for id in path {
hasher.write_u64(*id);
}
ViewId {
id: hasher.finish(),
match self.view_ids.get_mut(path) {
Some(id) => *id,
None => {
let id = self.next_id;
self.view_ids.insert(path.clone(), id);
self.next_id.id += 1;
id
}
}
}

Expand Down

0 comments on commit 50590c3

Please sign in to comment.