]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/@dataframe/private/df_cow.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / @dataframe / private / df_cow.m
1 function [df, S] = df_cow(df, S, col)
2
3   %# function [resu, S] = df_cow(df, S, col)
4   %# Implements Copy-On-Write on dataframe. If one or more columns
5   %# specified in inds is aliased to another one, duplicate it and
6   %# adjust the repetition index to remove the aliasing
7
8   %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
9   %%
10   %% This file is part of Octave.
11   %%
12   %% Octave is free software; you can redistribute it and/or
13   %% modify it under the terms of the GNU General Public
14   %% License as published by the Free Software Foundation;
15   %% either version 2, or (at your option) any later version.
16   %%
17   %% Octave is distributed in the hope that it will be useful,
18   %% but WITHOUT ANY WARRANTY; without even the implied
19   %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20   %% PURPOSE.  See the GNU General Public License for more
21   %% details.
22   %%
23   %% You should have received a copy of the GNU General Public
24   %% License along with Octave; see the file COPYING.  If not,
25   %% write to the Free Software Foundation, 51 Franklin Street -
26   %% Fifth Floor, Boston, MA 02110-1301, USA.
27   
28   %#
29   %# $Id: df_cow.m 9585 2012-02-05 15:32:46Z cdemills $
30   %#
31
32   if length(col) > 1,
33     error("df_cow must work on a column-by-column basis");
34   endif
35   
36   if (1 == length(S.subs)),
37     inds = 1; 
38   else
39     inds = S.subs{2};
40   endif
41
42   if (!isnumeric(inds)), 
43     if !strcmp(inds, ':'),
44       error("Unknown sheet selector %s", inds);
45     endif
46     inds = 1:length(df._rep(col));
47   endif
48
49   for indi = inds(:).',
50     dummy = df._rep{col}; dummy(indi) = 0;
51     [t1, t2] = ismember(df._rep{col}(indi)(:), dummy);
52     for indj = t2(find(t2)), %# Copy-On-Write
53       %# determines the index for the next column
54       t1 = 1+max(df._rep{col}); 
55       %# duplicate the touched column
56       df._data{col} = horzcat(df._data{col}, \
57                               df._data{col}(:, df._rep{col}(indj)));  
58       if (indi > 1),
59         %# a new column has been created
60         df._rep{col}(indi) = t1;
61       else
62         %# update repetition index aliasing this one
63         df._rep{col}(find(dummy == indi)) = t1;
64       endif
65     endfor
66   endfor
67
68   %# reorder S
69   if (length(S.subs) > 1),
70     if (S.subs{2} != 1 || length(S.subs{2}) > 1), 
71       %# adapt sheet index according to df_rep
72       S.subs{2} = df._rep{col}(S.subs{2});
73     endif
74   endif
75
76   df = df_thirddim(df);
77
78 endfunction