39 lines
894 B
Plaintext
39 lines
894 B
Plaintext
# Create an unnamed m*n array as a table with m rows, each row being a list
|
|
create or replace function matrix(m, n, init) as table (
|
|
with recursive row as (
|
|
select list_transform(range(0,n), x -> z) as row
|
|
from (select init as z)),
|
|
cte as (
|
|
select 1 as i, row from row
|
|
union all
|
|
select i+1, row
|
|
from cte
|
|
where i < m
|
|
)
|
|
from cte
|
|
);
|
|
|
|
# The backing table
|
|
create or replace table t_ (
|
|
i INTEGER NOT NULL check(i>0), check(i<= getenv('M')::INTEGER),
|
|
row DOUBLE[]
|
|
);
|
|
|
|
# The view
|
|
create or replace view t as from t_ order by i;
|
|
|
|
# Initialize the backing table
|
|
insert into t_ from matrix( getvariable('M')::INT, getvariable('N')::INT, 0.0);
|
|
|
|
prepare update_matrix as /* $1=i $2=j $3=value */
|
|
update t_
|
|
set row = row[1:$2-1] || [$3] || row[$2+1:]
|
|
where i = $1
|
|
;
|
|
|
|
create or replace function matrix_get(ii,jj) as (
|
|
select row[jj]
|
|
from t
|
|
where i = ii
|
|
);
|