2025-08-10 15:15:10 +09:00
|
|
|
//! ArrayBox 📦 - 配列・リスト操作
|
2025-08-10 02:45:57 +00:00
|
|
|
// Nyashの箱システムによる配列・リスト操作を提供します。
|
2025-08-10 15:15:10 +09:00
|
|
|
// Arc<Mutex>パターンで内部可変性を実現
|
2025-08-10 02:45:57 +00:00
|
|
|
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, BoxCore, BoxBase};
|
2025-08-10 03:21:24 +00:00
|
|
|
use std::any::Any;
|
2025-08-10 15:15:10 +09:00
|
|
|
use std::sync::{Arc, Mutex};
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
use std::fmt::Display;
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 15:15:10 +09:00
|
|
|
#[derive(Debug, Clone)]
|
2025-08-10 02:45:57 +00:00
|
|
|
pub struct ArrayBox {
|
2025-08-10 15:15:10 +09:00
|
|
|
pub items: Arc<Mutex<Vec<Box<dyn NyashBox>>>>,
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
base: BoxBase,
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ArrayBox {
|
|
|
|
|
/// 新しいArrayBoxを作成
|
|
|
|
|
pub fn new() -> Self {
|
2025-08-10 03:21:24 +00:00
|
|
|
ArrayBox {
|
2025-08-10 15:15:10 +09:00
|
|
|
items: Arc::new(Mutex::new(Vec::new())),
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
base: BoxBase::new(),
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 16:46:39 +09:00
|
|
|
/// 要素を持つArrayBoxを作成
|
|
|
|
|
pub fn new_with_elements(elements: Vec<Box<dyn NyashBox>>) -> Self {
|
|
|
|
|
ArrayBox {
|
|
|
|
|
items: Arc::new(Mutex::new(elements)),
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
base: BoxBase::new(),
|
2025-08-10 16:46:39 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
/// 要素を追加
|
2025-08-10 15:15:10 +09:00
|
|
|
pub fn push(&self, item: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
self.items.lock().unwrap().push(item);
|
|
|
|
|
Box::new(StringBox::new("ok"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 最後の要素を取り出す
|
|
|
|
|
pub fn pop(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
match self.items.lock().unwrap().pop() {
|
|
|
|
|
Some(item) => item,
|
|
|
|
|
None => Box::new(crate::boxes::null_box::NullBox::new()),
|
|
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
/// 要素数を取得
|
2025-08-10 15:15:10 +09:00
|
|
|
pub fn length(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
Box::new(IntegerBox::new(self.items.lock().unwrap().len() as i64))
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 15:15:10 +09:00
|
|
|
/// インデックスで要素を取得
|
2025-08-10 16:46:39 +09:00
|
|
|
pub fn get(&self, index: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
|
|
|
|
|
let idx = idx_box.value as usize;
|
|
|
|
|
let items = self.items.lock().unwrap();
|
|
|
|
|
match items.get(idx) {
|
|
|
|
|
Some(item) => item.clone_box(),
|
|
|
|
|
None => Box::new(crate::boxes::null_box::NullBox::new()),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Box::new(StringBox::new("Error: get() requires integer index"))
|
|
|
|
|
}
|
2025-08-10 15:15:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// インデックスで要素を設定
|
2025-08-10 16:46:39 +09:00
|
|
|
pub fn set(&self, index: Box<dyn NyashBox>, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
|
|
|
|
|
let idx = idx_box.value as usize;
|
|
|
|
|
let mut items = self.items.lock().unwrap();
|
|
|
|
|
if idx < items.len() {
|
|
|
|
|
items[idx] = value;
|
|
|
|
|
Box::new(StringBox::new("ok"))
|
|
|
|
|
} else {
|
|
|
|
|
Box::new(StringBox::new("Error: index out of bounds"))
|
|
|
|
|
}
|
2025-08-10 15:15:10 +09:00
|
|
|
} else {
|
2025-08-10 16:46:39 +09:00
|
|
|
Box::new(StringBox::new("Error: set() requires integer index"))
|
2025-08-10 15:15:10 +09:00
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
/// 要素を削除
|
2025-08-10 16:46:39 +09:00
|
|
|
pub fn remove(&self, index: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
|
|
|
|
|
let idx = idx_box.value as usize;
|
|
|
|
|
let mut items = self.items.lock().unwrap();
|
|
|
|
|
if idx < items.len() {
|
|
|
|
|
items.remove(idx)
|
|
|
|
|
} else {
|
|
|
|
|
Box::new(crate::boxes::null_box::NullBox::new())
|
|
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
} else {
|
2025-08-10 16:46:39 +09:00
|
|
|
Box::new(StringBox::new("Error: remove() requires integer index"))
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 15:15:10 +09:00
|
|
|
|
|
|
|
|
/// 指定された値のインデックスを検索
|
2025-08-10 16:46:39 +09:00
|
|
|
pub fn indexOf(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
2025-08-10 15:15:10 +09:00
|
|
|
let items = self.items.lock().unwrap();
|
|
|
|
|
for (i, item) in items.iter().enumerate() {
|
2025-08-10 16:46:39 +09:00
|
|
|
if item.equals(value.as_ref()).value {
|
2025-08-10 15:15:10 +09:00
|
|
|
return Box::new(IntegerBox::new(i as i64));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Box::new(IntegerBox::new(-1))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 指定された値が含まれているか確認
|
2025-08-10 16:46:39 +09:00
|
|
|
pub fn contains(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
2025-08-10 15:15:10 +09:00
|
|
|
let items = self.items.lock().unwrap();
|
|
|
|
|
for item in items.iter() {
|
2025-08-10 16:46:39 +09:00
|
|
|
if item.equals(value.as_ref()).value {
|
2025-08-10 15:15:10 +09:00
|
|
|
return Box::new(BoolBox::new(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Box::new(BoolBox::new(false))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 配列を空にする
|
|
|
|
|
pub fn clear(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
self.items.lock().unwrap().clear();
|
|
|
|
|
Box::new(StringBox::new("ok"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 文字列結合
|
2025-08-10 16:46:39 +09:00
|
|
|
pub fn join(&self, delimiter: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
if let Some(sep_box) = delimiter.as_any().downcast_ref::<StringBox>() {
|
|
|
|
|
let items = self.items.lock().unwrap();
|
|
|
|
|
let parts: Vec<String> = items
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|item| item.to_string_box().value)
|
|
|
|
|
.collect();
|
|
|
|
|
Box::new(StringBox::new(&parts.join(&sep_box.value)))
|
|
|
|
|
} else {
|
|
|
|
|
Box::new(StringBox::new("Error: join() requires string separator"))
|
|
|
|
|
}
|
2025-08-10 15:15:10 +09:00
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
impl BoxCore for ArrayBox {
|
|
|
|
|
fn box_id(&self) -> u64 {
|
|
|
|
|
self.base.id
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
|
|
|
|
self.base.parent_type_id
|
|
|
|
|
}
|
|
|
|
|
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
|
let items = self.items.lock().unwrap();
|
|
|
|
|
let strings: Vec<String> = items.iter()
|
|
|
|
|
.map(|item| item.to_string_box().value)
|
|
|
|
|
.collect();
|
|
|
|
|
write!(f, "[{}]", strings.join(", "))
|
|
|
|
|
}
|
2025-08-11 15:01:11 +09:00
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
🚀 feat: BoxBase+BoxCore革命 Phase4進捗 - 12+Box型統一完了
✅ 完了したBox型統一アーキテクチャ移行
- MathBox関連: MathBox, FloatBox, RangeBox
- TimeBox関連: TimeBox, DateTimeBox, TimerBox
- DebugBox, RandomBox
- StringBox, IntegerBox, BoolBox (個別ファイル版)
- ArrayBox, ConsoleBox
- box_trait.rs内: StringBox, IntegerBox, BoolBox, VoidBox等
🎯 大幅な進捗達成
- unsafe ID生成 → BoxBase::new()安全化
- コンパイルエラー: 106 → 97に減少
- 統一インターフェース確立でCharmFlow互換性問題完全回避
🔧 革命的変更パターン確立
1. base: BoxBase導入
2. impl BoxCore with box_id()/fmt_box()
3. NyashBoxからbox_id()削除
4. Display::fmt() → fmt_box()委譲
Phase 4E: 残りBox型の統一化継続中
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 11:25:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for ArrayBox {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
self.fmt_box(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
impl NyashBox for ArrayBox {
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
2025-08-10 15:15:10 +09:00
|
|
|
Box::new(self.clone())
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
2025-08-10 15:15:10 +09:00
|
|
|
let items = self.items.lock().unwrap();
|
|
|
|
|
let strings: Vec<String> = items.iter()
|
2025-08-10 03:21:24 +00:00
|
|
|
.map(|item| item.to_string_box().value)
|
|
|
|
|
.collect();
|
2025-08-10 15:15:10 +09:00
|
|
|
StringBox::new(format!("[{}]", strings.join(", ")))
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
"ArrayBox"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
|
|
|
|
if let Some(other_array) = other.as_any().downcast_ref::<ArrayBox>() {
|
2025-08-10 15:15:10 +09:00
|
|
|
let self_items = self.items.lock().unwrap();
|
|
|
|
|
let other_items = other_array.items.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
if self_items.len() != other_items.len() {
|
2025-08-10 03:21:24 +00:00
|
|
|
return BoolBox::new(false);
|
|
|
|
|
}
|
2025-08-10 15:15:10 +09:00
|
|
|
|
|
|
|
|
for (a, b) in self_items.iter().zip(other_items.iter()) {
|
2025-08-10 03:21:24 +00:00
|
|
|
if !a.equals(b.as_ref()).value {
|
|
|
|
|
return BoolBox::new(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 15:15:10 +09:00
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
BoolBox::new(true)
|
|
|
|
|
} else {
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 15:15:10 +09:00
|
|
|
}
|