View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  1999-2020, University of Amsterdam
    7                              CWI, Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(table_util,
   37          [ sort_table/2,               % +Handle, +OutputFile
   38            verify_table_order/1        % +Handle
   39          ]).   40:- autoload(library(backcomp),[flush/0]).   41:- autoload(library(table),
   42	    [ get_table_attribute/3,
   43	      read_table_record_data/4,
   44	      compare_strings/4,
   45	      read_table_fields/4
   46	    ]).

Tabular file handling utilities

*/

 sort_table(+Table, +File)
Read the records from the given table, sort them according to the ordering information on the key field and write the result to the given filename. Note this may require a lot of memory.
   57sort_table(Table, File) :-
   58    open(File, write, OutFd),       % fail early :-)
   59    get_table_attribute(Table, key_field, Key),
   60    !,
   61    get_table_attribute(Table, field(Key), Term),
   62    get_table_attribute(Table, file, InFile),
   63    functor(Term, KeyName, _),
   64    arg(2, Term, Attributes),
   65    format('Sorting table "~w" ', [InFile]),
   66    (   memberchk(sorted(Order), Attributes)
   67    ->  true
   68    ;   memberchk(sorted, Attributes),
   69        Order = exact
   70    ),
   71    format('sorted(~w) on field "~w" ... ', [Order, KeyName]),
   72    flush,
   73    read_table(Table, KeyName, Fields),
   74    sort_fields(Order, Fields, SortedFields),
   75    write_table(SortedFields, Table, OutFd),
   76    close(OutFd),
   77    format('done.~n', []).
   78
   79read_table(Table, KeyName, Fields) :-
   80    format('(reading) ... ', []), flush,
   81    read_table(Table, KeyName, 0, Fields).
   82
   83read_table(Table, KeyName, From, [KeyValue-From|T]) :-
   84    read_field(Table, From, To, KeyName, KeyValue),
   85    !,
   86    read_table(Table, KeyName, To, T).
   87read_table(_, _, _, []).
   88
   89sort_fields(Order, Fields, Sorted) :-
   90    length(Fields, N),
   91    format('(sorting ~D records) ... ', [N]), flush,
   92    sort_keyed_strings(Order, Fields, Sorted).
   93
   94write_table(Records, Table, OutFd) :-
   95    format('(writing) ... ', []), flush,
   96    get_table_attribute(Table, record_separator, Sep),
   97    write_records(Records, Table, Sep, OutFd).
   98
   99write_records([], _, _, _).
  100write_records([_-From|T], Table, Sep, OutFd) :-
  101    read_table_record_data(Table, From, _To, RecordData),
  102    format(OutFd, '~s~c', [RecordData, Sep]),
  103    write_records(T, Table, Sep, OutFd).
 sort_keyed_strings(+Table, +List, -Sorted)
Sort a list of KeyName-Index pairs on their KeyName using the given ordering table.
  111sort_keyed_strings(Table, List, Sorted) :-
  112    length(List, Length),
  113    do_sort(Length, Table, List, _, Result),
  114    Sorted = Result.
  115
  116do_sort(2, Table, [X1, X2|L], L, R) :-
  117    !,
  118    X1 = K1-_,
  119    X2 = K2-_,
  120    compare_strings(Table, K1, K2, Cmp),
  121    merge2(Cmp, X1, X2, R).
  122do_sort(1, _, [X|L], L, [X]) :- !.
  123do_sort(0, _, L, L, []) :- !.
  124do_sort(N, Table, L1, L3, R) :-
  125    N1 is N // 2,
  126    N2 is N - N1,
  127    do_sort(N1, Table, L1, L2, R1),
  128    do_sort(N2, Table, L2, L3, R2),
  129    do_merge(R1, R2, Table, R).
  130
  131do_merge([], R, _, R) :- !.
  132do_merge(R, [], _, R) :- !.
  133do_merge(R1, R2, Table, [X|R]) :-
  134    R1 = [X1|R1a],
  135    R2 = [X2|R2a],
  136    X1 = K1-_,
  137    X2 = K2-_,
  138    (   compare_strings(Table, K1, K2, >)
  139    ->  X = X2, do_merge(R1, R2a, Table, R)
  140    ;   X = X1, do_merge(R1a, R2, Table, R)
  141    ).
  142
  143merge2(>, A, B, [B, A]) :- !.
  144merge2(_, A, B, [A, B]).
  145
  146
  147                 /*******************************
  148                 *             VERIFY           *
  149                 *******************************/
 verify_table_order(+Table)
If Table is a handle to a defined table and the table contains a key-fields, check that the fields in the table are really sorted according to the order defined in the table. Errors are reported.
  157verify_table_order(Table) :-
  158    get_table_attribute(Table, key_field, Key),
  159    !,
  160    get_table_attribute(Table, field(Key), Term),
  161    get_table_attribute(Table, file, File),
  162    functor(Term, KeyName, _),
  163    arg(2, Term, Attributes),
  164    format('Checking "~w" ', [File]),
  165    (   memberchk(sorted(Order), Attributes)
  166    ->  true
  167    ;   memberchk(sorted, Attributes),
  168        Order = exact
  169    ),
  170    (   memberchk(unique, Attributes)
  171    ->  Cmp = >,
  172        format('uniquely ', [])
  173    ;   Cmp = [>, =]
  174    ),
  175    format('sorted(~w) on field "~w" ... ', [Order, KeyName]),
  176    flush,
  177    read_field(Table, 0, To, KeyName, KeyValue),
  178    verify_table(Table, To, KeyName, KeyValue, Order, Cmp),
  179    format('done.~n', []).
  180
  181verify_table(Table, From, KeyName, PrevValue, Order, Cmp) :-
  182    read_field(Table, From, To, KeyName, KeyValue),
  183    !,
  184    (   compare_strings(Order, KeyValue, PrevValue, Rval),
  185        ok_cmp(Rval, Cmp)
  186    ->  verify_table(Table, To, KeyName, KeyValue, Order, Cmp)
  187    ;   format('~N!! Order conflict: ~w < ~w~n', [KeyValue, PrevValue]),
  188        verify_table(Table, To, KeyName, KeyValue, Order, Cmp)
  189    ).
  190verify_table(_, _, _, _, _, _).
  191
  192ok_cmp(Cmp, Cmp) :- !.
  193ok_cmp(Cmp, List) :-
  194    memberchk(Cmp, List).
  195
  196read_field(Table, From, To, Field, Value) :-
  197    functor(Term, Field, 1),
  198    read_table_fields(Table, From, To,  [Term]),
  199    arg(1, Term, Value)