Add ExtendableQueue utility and refactor code

This commit is contained in:
Moritz Hölting 2024-04-02 17:10:57 +02:00
parent fedb55c50a
commit f0d7f7499f
4 changed files with 96 additions and 30 deletions

View File

@ -3,15 +3,15 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
util::compile::{CompileOptions, FunctionCompilerState, MutCompilerState}, util::{
compile::{CompileOptions, FunctionCompilerState, MutCompilerState},
extendable_queue::ExtendableQueue,
},
virtual_fs::VFolder, virtual_fs::VFolder,
}; };
use super::{function::Function, tag::Tag}; use super::{function::Function, tag::Tag};
use std::{ use std::collections::{HashMap, VecDeque};
collections::{HashMap, VecDeque},
sync::{Arc, Mutex},
};
/// Namespace of a datapack /// Namespace of a datapack
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -89,20 +89,10 @@ impl Namespace {
functions.push_front(("main".to_string(), self.main_function.clone())); functions.push_front(("main".to_string(), self.main_function.clone()));
} }
let functions = Arc::new(Mutex::new(functions)); let mut functions = ExtendableQueue::from(functions);
loop { while let Some((path, function)) = functions.next() {
let Some((path, function)) = ({ let function_state = FunctionCompilerState::new(&path, &self.name, functions.clone());
let mut functions = functions.lock().unwrap();
let entry = functions.pop_front();
drop(functions);
entry
}) else {
break;
};
let function_state =
FunctionCompilerState::new(&path, &self.name, Arc::downgrade(&functions));
root_folder.add_file( root_folder.add_file(
&format!("functions/{}.mcfunction", path), &format!("functions/{}.mcfunction", path),
function.compile(options, state, &function_state), function.compile(options, state, &function_state),

View File

@ -1,16 +1,14 @@
//! Compile options for the compiler. //! Compile options for the compiler.
use std::{ use std::{cell::Cell, sync::Mutex};
cell::Cell,
collections::VecDeque,
sync::{Mutex, Weak},
};
use getset::Getters; use getset::Getters;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::datapack::Function; use crate::datapack::Function;
use super::extendable_queue::ExtendableQueue;
/// Compile options for the compiler. /// Compile options for the compiler.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompileOptions { pub struct CompileOptions {
@ -46,7 +44,7 @@ pub struct FunctionCompilerState {
functions: FunctionQueue, functions: FunctionQueue,
} }
type FunctionQueue = Weak<Mutex<VecDeque<(String, Function)>>>; type FunctionQueue = ExtendableQueue<(String, Function)>;
impl FunctionCompilerState { impl FunctionCompilerState {
/// Create a new function compiler state. /// Create a new function compiler state.
@ -61,11 +59,6 @@ impl FunctionCompilerState {
/// Add a function to the queue. /// Add a function to the queue.
pub fn add_function(&self, name: &str, function: Function) { pub fn add_function(&self, name: &str, function: Function) {
if let Some(queue) = self.functions.upgrade() { self.functions.push((name.to_string(), function));
queue
.lock()
.unwrap()
.push_back((name.to_string(), function));
}
} }
} }

View File

@ -0,0 +1,82 @@
//! A queue that can be extended while iterating over it.
use std::{
collections::VecDeque,
sync::{Arc, RwLock, Weak},
};
/// A queue that can be extended while iterating over it.
#[derive(Debug, Clone)]
pub struct ExtendableQueue<T> {
queue: Arc<RwLock<VecDeque<T>>>,
}
impl Default for ExtendableQueue<String> {
fn default() -> Self {
Self {
queue: Arc::new(RwLock::new(VecDeque::new())),
}
}
}
impl<T, V> From<V> for ExtendableQueue<T>
where
V: Into<VecDeque<T>>,
{
fn from(value: V) -> Self {
Self {
queue: Arc::new(RwLock::new(value.into())),
}
}
}
impl<T> ExtendableQueue<T> {
/// Add an element to the queue.
pub fn push(&self, value: T) {
self.queue.write().unwrap().push_back(value);
}
/// Get the queue.
pub fn get(&self) -> &Arc<RwLock<VecDeque<T>>> {
&self.queue
}
/// Get a weak reference to the queue.
pub fn get_weak(&self) -> Weak<RwLock<VecDeque<T>>> {
Arc::downgrade(&self.queue)
}
/// Clear the queue.
pub fn clear(&self) {
self.queue.write().unwrap().clear();
}
/// Get the length of the queue.
pub fn len(&self) -> usize {
self.queue.read().unwrap().len()
}
/// Check if the queue is empty.
pub fn is_empty(&self) -> bool {
self.queue.read().unwrap().is_empty()
}
/// Get and remove the next item without needing mutable access.
pub fn pop_front(&self) -> Option<T> {
self.queue.write().unwrap().pop_front()
}
}
impl<A> Extend<A> for ExtendableQueue<A> {
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
self.queue.write().unwrap().extend(iter);
}
}
impl<T> Iterator for ExtendableQueue<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.queue.write().unwrap().pop_front()
}
}

View File

@ -1,3 +1,4 @@
//! Utility functions for the Shulkerbox project. //! Utility functions for the Shulkerbox project.
pub mod compile; pub mod compile;
pub mod extendable_queue;