dora_ssr/dora/
array.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn array_type() -> i32;
11	fn array_get_count(slf: i64) -> i64;
12	fn array_is_empty(slf: i64) -> i32;
13	fn array_add_range(slf: i64, other: i64);
14	fn array_remove_from(slf: i64, other: i64);
15	fn array_clear(slf: i64);
16	fn array_reverse(slf: i64);
17	fn array_shrink(slf: i64);
18	fn array_swap(slf: i64, index_a: i32, index_b: i32);
19	fn array_remove_at(slf: i64, index: i32) -> i32;
20	fn array_fast_remove_at(slf: i64, index: i32) -> i32;
21	fn array_new() -> i64;
22}
23use crate::dora::IObject;
24/// An array data structure that supports various operations.
25pub struct Array { raw: i64 }
26crate::dora_object!(Array);
27impl Array {
28	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
29		(unsafe { array_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
30			match raw {
31				0 => None,
32				_ => Some(Box::new(Array { raw: raw }))
33			}
34		})
35	}
36	/// Gets the number of items in the array.
37	pub fn get_count(&self) -> i64 {
38		return unsafe { array_get_count(self.raw()) };
39	}
40	/// Gets whether the array is empty or not.
41	pub fn is_empty(&self) -> bool {
42		return unsafe { array_is_empty(self.raw()) != 0 };
43	}
44	/// Adds all items from another array to the end of this array.
45	///
46	/// # Arguments
47	///
48	/// * `other` - Another array object.
49	pub fn add_range(&mut self, other: &crate::dora::Array) {
50		unsafe { array_add_range(self.raw(), other.raw()); }
51	}
52	/// Removes all items from this array that are also in another array.
53	///
54	/// # Arguments
55	///
56	/// * `other` - Another array object.
57	pub fn remove_from(&mut self, other: &crate::dora::Array) {
58		unsafe { array_remove_from(self.raw(), other.raw()); }
59	}
60	/// Removes all items from the array.
61	pub fn clear(&mut self) {
62		unsafe { array_clear(self.raw()); }
63	}
64	/// Reverses the order of the items in the array.
65	pub fn reverse(&mut self) {
66		unsafe { array_reverse(self.raw()); }
67	}
68	/// Removes any empty slots from the end of the array.
69	/// This method is used to release the unused memory this array holds.
70	pub fn shrink(&mut self) {
71		unsafe { array_shrink(self.raw()); }
72	}
73	/// Swaps the items at two given indices.
74	///
75	/// # Arguments
76	///
77	/// * `index_a` - The first index.
78	/// * `index_b` - The second index.
79	pub fn swap(&mut self, index_a: i32, index_b: i32) {
80		unsafe { array_swap(self.raw(), index_a, index_b); }
81	}
82	/// Removes the item at the given index.
83	///
84	/// # Arguments
85	///
86	/// * `index` - The index to remove.
87	///
88	/// # Returns
89	///
90	/// * `bool` - `true` if an item was removed, `false` otherwise.
91	pub fn remove_at(&mut self, index: i32) -> bool {
92		unsafe { return array_remove_at(self.raw(), index) != 0; }
93	}
94	/// Removes the item at the given index without preserving the order of the array.
95	///
96	/// # Arguments
97	///
98	/// * `index` - The index to remove.
99	///
100	/// # Returns
101	///
102	/// * `bool` - `true` if an item was removed, `false` otherwise.
103	pub fn fast_remove_at(&mut self, index: i32) -> bool {
104		unsafe { return array_fast_remove_at(self.raw(), index) != 0; }
105	}
106	/// Creates a new array object
107	pub fn new() -> Array {
108		unsafe { return Array { raw: array_new() }; }
109	}
110}