Skip to content

Commit e8c95bd

Browse files
committed
sync mtable view with sequence
1 parent 635f96a commit e8c95bd

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

src/madl_mtable.mad

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ local mtable = object 'mtable' {
6969

7070
-- backup original methods and metamethods
7171
local mtable_mt = getmetatable(mtable)
72-
local var_get, var_val, raw_get in mtable
72+
local var_get, var_val in mtable
7373
local tbl_cpy, var_set = mtable.copy, mtable_mt.__newindex
7474

7575
-- identity -------------------------------------------------------------------o
@@ -370,7 +370,7 @@ end
370370
-- build mtable ---------------------------------------------------------------o
371371

372372
-- finalize mtable
373-
local function finish_mtbl (tbl)
373+
local function fini_mtbl (tbl)
374374
-- clear cycle (if any)
375375
tbl:raw_set('__cycle', nil)
376376

@@ -381,6 +381,8 @@ end
381381
-- mtable initialization
382382
local function init_mm (tbl)
383383
local nc = tbl:raw_len()
384+
385+
if nc > 0 then -- check if mtable needs __dat
384386
local pdat = tbl.parent.__dat
385387
nc = nc + pdat.nc
386388
local data = table.new(nc, 9) -- columns (data)
@@ -393,19 +395,16 @@ local function init_mm (tbl)
393395
data.ng = 0 -- #gens
394396
data.rmax = 1 -- allocated rows (specialize after 1st row)
395397

396-
-- disable inheritance
397-
-- tbl:set_final()
398-
399398
-- create columns
400399
for i=0,nc do data[i] = {} end
401400

402401
-- process columns names
403402
tblcpy(pdat.cidx, data.cidx)
404403
data.cidx.is_selected, data.cidx[0] = 0, 'is_selected' -- case pdat.nc == 0
405404

406-
local cref = raw_get(tbl, 'refcol')
405+
local cref = tbl:raw_get'refcol'
407406
for i=pdat.nc+1,nc do
408-
local col = raw_get(tbl, i-pdat.nc) -- column key
407+
local col = tbl:raw_get(i-pdat.nc) -- column key
409408
if is_table(col) then -- cref: { key }
410409
assert(is_rawtable(col) and is_nil(col[2]), "invalid reference column")
411410
data.cref, col = i, col[1]
@@ -435,21 +434,24 @@ local function init_mm (tbl)
435434

436435
-- clear list of column names
437436
tbl:clear_array()
437+
438438
-- set current date and time
439439
tbl.date, tbl.time = os.date'%d/%m/%y', os.date'%H:%M:%S'
440440

441+
-- finalize
442+
tbl.__dat = data
443+
fini_mtbl(tbl)
444+
end -- nc > 0
445+
441446
-- concat header names and column names to parent ones for I/O
442-
local hdrnam, colnam = raw_get(tbl, 'header'), raw_get(tbl, 'column')
447+
local hdrnam, colnam = tbl:raw_get'header', tbl:raw_get'column'
443448
if is_table(hdrnam) and #hdrnam > 0 then
444449
tbl.header = tblcat(tbl.parent.header or {}, hdrnam)
445450
end
446451
if is_table(colnam) and #colnam > 0 then
447452
tbl.column = tblcat(tbl.parent.column or {}, colnam)
448453
end
449454

450-
-- finalized
451-
tbl.__dat = data
452-
finish_mtbl(tbl)
453455
return tbl
454456
end
455457

@@ -458,7 +460,11 @@ end
458460
local function copy_mm (tbl, name_)
459461
assert(not rawequal(tbl, mtable), "invalid argument #1 (cannot copy 'mtable')")
460462
assert(is_mtable(tbl) , "invalid argument #1 (mtable expected)")
461-
local cpy = tbl_cpy(tbl,name_)
463+
local cpy = tbl_cpy(tbl,name_)
464+
465+
-- check if copy needs to be further processed
466+
if tbl:is_view() then return cpy end
467+
462468
local data = tbl.__dat
463469
local nr, nc = data.nr, data.nc
464470
local cdat = table.new(nc, 9) -- see init_mm
@@ -469,6 +475,7 @@ local function copy_mm (tbl, name_)
469475
cdat.nc = nc -- #cols
470476
cdat.ng = 0 -- #gens
471477
cdat.rmax = nr -- allocated rows (no oversize with rmax)
478+
cpy.__dat = cdat
472479

473480
tblcpy(data.cidx, cdat.cidx)
474481
cdat.nvec = data.nvec == data.cidx and cdat.cidx or tblcpy(data.nvec)
@@ -477,8 +484,6 @@ local function copy_mm (tbl, name_)
477484
cdat[i] = isa_matrix(col) and col:copy() or tblicpy(col, table.new(nr,0))
478485
end
479486

480-
cpy:raw_set('__dat', cdat)
481-
482487
return cpy:make_dict()
483488
end
484489

@@ -628,12 +633,17 @@ local function build_index (tbl, key_)
628633
end
629634

630635
build_idx (data)
631-
finish_mtbl(tbl)
636+
fini_mtbl(tbl)
632637
return tbl
633638
end
634639

635640
-- methods (readonly) ---------------------------------------------------------o
636641

642+
local function is_view (tbl)
643+
assert(is_mtable(tbl), "invalid argument #1 (mtable expected)")
644+
return is_nil(tbl:raw_get'__dat')
645+
end
646+
637647
local function index (tbl, idx)
638648
assert(is_mtable (tbl), "invalid argument #1 (mtable expected)")
639649
assert(is_integer(idx), "invalid argument #2 (integer expected)")
@@ -1177,7 +1187,7 @@ local function remove (tbl, rng_, sel_)
11771187
end
11781188
data.nr = ni
11791189

1180-
return build_index(tbl)
1190+
return tbl:make_dict()
11811191
end
11821192

11831193
local function insert (tbl, rows, rng_, sel_)
@@ -1243,7 +1253,7 @@ local function insert (tbl, rows, rng_, sel_)
12431253
i = i-1
12441254
end
12451255

1246-
return build_index(tbl)
1256+
return tbl:make_dict()
12471257
end
12481258

12491259
local function sort (tbl, cmp, rng_, sel_)
@@ -1288,14 +1298,15 @@ local function sort (tbl, cmp, rng_, sel_)
12881298
end
12891299
assert(is_nil(next(idx_l)), "unexpected corrupted lookup indexes (sort)")
12901300

1291-
return build_index(tbl)
1301+
return tbl:make_dict()
12921302
end
12931303

12941304
-- special --------------------------------------------------------------------o
12951305

12961306
local function cycle (tbl, a)
1297-
assert(is_mtable(tbl) , "invalid argument #1 (mtable expected)")
1298-
assert(index_of(tbl, a), "invalid argument #2 (valid reference expected)")
1307+
assert(is_mtable(tbl), "invalid argument #1 (mtable expected)")
1308+
assert(is_nil(a) or index_of(tbl, a),
1309+
"invalid argument #2 (valid reference expected)")
12991310
assert(not tbl:is_readonly(), "invalid write access to readonly mtable")
13001311
tbl:raw_set('__cycle', a)
13011312
return tbl
@@ -1626,12 +1637,9 @@ local function read (tbl, filnam_)
16261637
tbl:addrow(row)
16271638
end
16281639

1629-
-- build index with column 'name' (if any)
1630-
local cref = raw_get(tbl, 'refcol')
1631-
if cref then build_index(tbl, cref) end
1632-
16331640
if is_string(filnam_) then file:close() end
1634-
return tbl
1641+
1642+
return tbl:make_dict()
16351643
end
16361644

16371645
-- members --------------------------------------------------------------------o
@@ -1689,6 +1697,9 @@ mtable :set_methods {
16891697
cycle = cycle,
16901698
iter = iter,
16911699

1700+
-- ownership
1701+
is_view = is_view,
1702+
16921703
-- read/write
16931704
read = read,
16941705
write = write,

0 commit comments

Comments
 (0)