Use HashSet instead of Vec

This commit is contained in:
Vincent Foulon 2021-03-30 18:33:01 +02:00 committed by Marcel Märtens
parent 211ab02897
commit b1718cf578

View File

@ -1,6 +1,6 @@
use specs::Component; use specs::Component;
use specs_idvs::IdvStorage; use specs_idvs::IdvStorage;
use std::mem; use std::{collections::HashSet, mem};
use crate::trade::SiteId; use crate::trade::SiteId;
@ -11,11 +11,11 @@ use crate::trade::SiteId;
/// state when needed /// state when needed
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
pub struct Behavior { pub struct Behavior {
tags: Vec<BehaviorTag>, tags: HashSet<BehaviorTag>,
} }
/// Versatile tags attached to behaviors /// Versatile tags attached to behaviors
#[derive(PartialEq, Clone, Debug)] #[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub enum BehaviorTag { pub enum BehaviorTag {
/// The entity is allowed to speak /// The entity is allowed to speak
CanSpeak, CanSpeak,
@ -40,20 +40,14 @@ impl Behavior {
/// Apply a tag to the Behavior /// Apply a tag to the Behavior
pub fn add_tag(&mut self, tag: BehaviorTag) { pub fn add_tag(&mut self, tag: BehaviorTag) {
if !self.has_tag(&tag) { if !self.has_tag(&tag) {
self.tags.push(tag); self.tags.insert(tag);
} }
} }
/// Revoke a tag to the Behavior /// Revoke a tag to the Behavior
pub fn remove_tag(&mut self, tag: BehaviorTag) { pub fn remove_tag(&mut self, tag: BehaviorTag) {
if self.has_tag(&tag) { if self.has_tag(&tag) {
while let Some(position) = self self.tags.remove(&tag);
.tags
.iter()
.position(|behavior_tag| behavior_tag == &tag)
{
self.tags.remove(position);
}
} }
} }