Skip to content

Commit 9144453

Browse files
BFV sketch
1 parent 309c614 commit 9144453

24 files changed

+2217
-39
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ eyre ="0.6.12"
1616
rand ="0.8.5"
1717
rand_core ="0.6.4"
1818
sha2 ="0.10.8"
19-
thiserror ="1.0.63"
19+
thiserror ="2.0.9"
2020
serde ={ version="1.0.205", features=["derive"] }
2121
serde_derive="1.0.205"
2222
serde_json ="1.0.122"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::{
16+
marker::Send,
17+
ops::{Add, Index, IndexMut, Range},
18+
};
19+
20+
/// Stores values in a 2 dimensional array.
21+
#[derive(PartialEq, Clone, Debug)]
22+
pub struct Array2d<T: PartialEq + Add<Output = T> + Send + Clone + Default> {
23+
data: Vec<T>,
24+
pub row_count: usize,
25+
pub column_count: usize,
26+
}
27+
28+
impl<T: PartialEq + Add<Output = T> + Send + Clone + Default> Array2d<T> {
29+
/// Returns the shape of the array as a tuple (row_count, column_count).
30+
pub fn shape(&self) -> (usize, usize) {
31+
(self.row_count, self.column_count)
32+
}
33+
34+
/// Returns the total number of elements in the array.
35+
pub fn count(&self) -> usize {
36+
self.row_count * self.column_count
37+
}
38+
39+
/// Creates a new `Array2d` with the given data and dimensions.
40+
pub fn new(data: Vec<T>, row_count: usize, column_count: usize) -> Self {
41+
assert_eq!(data.len(), (row_count * column_count), "Data size does not match dimensions");
42+
Self { data, row_count, column_count }
43+
}
44+
45+
/// Calculates the linear index for a given (row, column) pair.
46+
pub fn index(&self, row: usize, column: usize) -> usize {
47+
row * self.column_count + column
48+
}
49+
50+
/// Returns the range of indices for a given row.
51+
pub fn row_indices(&self, row: usize) -> Range<usize> {
52+
let start = self.index(row, 0);
53+
let end = start + self.column_count;
54+
start..end
55+
}
56+
57+
/// Returns an iterator over the columns of the array.
58+
pub fn columns_iter(&self) -> impl Iterator<Item = Vec<&T>> {
59+
(0..self.column_count).map(move |col| {
60+
(0..self.row_count).map(move |row| &self.data[self.index(row, col)]).collect()
61+
})
62+
}
63+
}
64+
65+
impl<T: PartialEq + Add<Output = T> + Send + Clone + Default> Index<usize> for Array2d<T> {
66+
type Output = [T];
67+
68+
fn index(&self, row: usize) -> &Self::Output {
69+
let start = self.index(row, 0);
70+
let end = start + self.column_count;
71+
&self.data[start..end]
72+
}
73+
}
74+
75+
impl<T: PartialEq + Add<Output = T> + Send + Clone + Default> IndexMut<usize> for Array2d<T> {
76+
fn index_mut(&mut self, row: usize) -> &mut Self::Output {
77+
let start = self.index(row, 0);
78+
let end = start + self.column_count;
79+
&mut self.data[start..end]
80+
}
81+
}
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
#[test]
88+
fn test_array2d() {
89+
let data = vec![1, 2, 3, 4, 5, 6];
90+
let mut array = Array2d::new(data, 2, 3);
91+
92+
// Test shape and count
93+
assert_eq!(array.shape(), (2, 3));
94+
assert_eq!(array.count(), 6);
95+
96+
// Test double indexing
97+
assert_eq!(array[0][0], 1);
98+
assert_eq!(array[0][1], 2);
99+
assert_eq!(array[0][2], 3);
100+
assert_eq!(array[1][0], 4);
101+
assert_eq!(array[1][1], 5);
102+
assert_eq!(array[1][2], 6);
103+
104+
// Test mutability
105+
array[1][2] = 42;
106+
assert_eq!(array[1][2], 42);
107+
108+
// Test row_indices
109+
let row_1_indices = array.row_indices(1);
110+
assert_eq!(row_1_indices, 3..6);
111+
}
112+
}

0 commit comments

Comments
 (0)