1%%% -*- coding: utf-8 -*-
2%%% -*- erlang-indent-level: 2 -*-
3%%% -------------------------------------------------------------------
4%%% Copyright 2010-2017 Manolis Papadakis <manopapad@gmail.com>,
5%%%                     Eirini Arvaniti <eirinibob@gmail.com>
6%%%                 and Kostis Sagonas <kostis@cs.ntua.gr>
7%%%
8%%% This file is part of PropEr.
9%%%
10%%% PropEr is free software: you can redistribute it and/or modify
11%%% it under the terms of the GNU General Public License as published by
12%%% the Free Software Foundation, either version 3 of the License, or
13%%% (at your option) any later version.
14%%%
15%%% PropEr is distributed in the hope that it will be useful,
16%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
17%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18%%% GNU General Public License for more details.
19%%%
20%%% You should have received a copy of the GNU General Public License
21%%% along with PropEr.  If not, see <http://www.gnu.org/licenses/>.
22
23%%% @copyright 2010-2017 Manolis Papadakis, Eirini Arvaniti and Kostis Sagonas
24%%% @version {@version}
25%%% @author Manolis Papadakis
26%%% @doc Parametric wrapper to array module.
27%%% @private
28
29-module(proper_array).
30
31-export([new/0, new/1, new/2, is_array/1, set/3, get/2, size/1,
32	 sparse_size/1, default/1, reset/2, to_list/1, sparse_to_list/1,
33	 from_list/1, from_list/2, to_orddict/1, sparse_to_orddict/1,
34	 from_orddict/1, from_orddict/2, map/2, sparse_map/2, foldl/3,
35	 foldr/3, sparse_foldl/3, sparse_foldr/3, fix/1, relax/1, is_fix/1,
36	 resize/1, resize/2]).
37
38-export_type([array/1]).
39
40-opaque array(T) :: array:array(T).
41
42-type array_size() :: non_neg_integer().
43-type array_indx() :: non_neg_integer().
44-type indx_pairs(T) :: proper_orddict:orddict(array_indx(),T).
45
46-type array_opt(T) :: 'fixed' | array_size()
47                    | {'default', T} | {'fixed', boolean()}
48                    | {'size', array_size()}.
49-type array_opts(T) :: array_opt(T) | [array_opt(T)].
50
51%%------------------------------------------------------------------------------
52%% API functions
53%%------------------------------------------------------------------------------
54
55-spec new() -> array(_T).
56new() ->
57    array:new().
58
59-spec new(array_opts(T)) -> array(T).
60new(Opts) ->
61    array:new(Opts).
62
63-spec new(array_size(), array_opts(T)) -> array(T).
64new(Size, Opts) ->
65    array:new(Size, Opts).
66
67-spec is_array(term()) -> boolean().
68is_array(X) ->
69    array:is_array(X).
70
71-spec size(array(_T)) -> array_size().
72size(Array) ->
73    array:size(Array).
74
75-spec default(array(T)) -> T.
76default(Array) ->
77    array:default(Array).
78
79-spec fix(array(T)) -> array(T).
80fix(Array) ->
81    array:fix(Array).
82
83-spec is_fix(array(_T)) -> boolean().
84is_fix(Array) ->
85    array:is_fix(Array).
86
87-spec relax(array(T)) -> array(T).
88relax(Array) ->
89    array:relax(Array).
90
91-spec resize(array_size(), array(T)) -> array(T).
92resize(Size, Array) ->
93    array:resize(Size, Array).
94
95-spec resize(array(T)) -> array(T).
96resize(Array) ->
97    array:resize(Array).
98
99-spec set(array_indx(), T, array(T)) -> array(T).
100set(Index, Value, Array) ->
101    array:set(Index, Value, Array).
102
103-spec get(array_indx(), array(T)) -> T.
104get(Index, Array) ->
105    array:get(Index, Array).
106
107-spec reset(array_indx(), array(T)) -> array(T).
108reset(Index, Array) ->
109    array:reset(Index, Array).
110
111-spec to_list(array(T)) -> [T].
112to_list(Array) ->
113    array:to_list(Array).
114
115-spec sparse_to_list(array(T)) -> [T].
116sparse_to_list(Array) ->
117    array:sparse_to_list(Array).
118
119-spec from_list([T]) -> array(T).
120from_list(List) ->
121    array:from_list(List).
122
123-spec from_list([T], T) -> array(T).
124from_list(List, Default) ->
125    array:from_list(List, Default).
126
127-spec to_orddict(array(T)) -> indx_pairs(T).
128to_orddict(Array) ->
129    array:to_orddict(Array).
130
131-spec sparse_to_orddict(array(T)) -> indx_pairs(T).
132sparse_to_orddict(Array) ->
133    array:sparse_to_orddict(Array).
134
135-spec from_orddict(indx_pairs(T)) -> array(T).
136from_orddict(Dict) ->
137    array:from_orddict(Dict).
138
139-spec from_orddict(indx_pairs(T), T) -> array(T).
140from_orddict(Dict, Default) ->
141    array:from_orddict(Dict, Default).
142
143-spec map(fun((array_indx(),T1) -> T2), array(T1)) -> array(T2).
144map(Fun, Array) ->
145    array:map(Fun, Array).
146
147-spec sparse_map(fun((array_indx(),T1) -> T2), array(T1)) -> array(T2).
148sparse_map(Fun, Array) ->
149    array:sparse_map(Fun, Array).
150
151-spec foldl(fun((array_indx(),T,A) -> A), A, array(T)) -> A.
152foldl(Fun, Acc0, Array) ->
153    array:foldl(Fun, Acc0, Array).
154
155-spec sparse_foldl(fun((array_indx(),T,A) -> A), A, array(T)) -> A.
156sparse_foldl(Fun, Acc0, Array) ->
157    array:sparse_foldl(Fun, Acc0, Array).
158
159-spec foldr(fun((array_indx(),T,A) -> A), A, array(T)) -> A.
160foldr(Fun, Acc0, Array) ->
161    array:foldr(Fun, Acc0, Array).
162
163-spec sparse_foldr(fun((array_indx(),T,A) -> A), A, array(T)) -> A.
164sparse_foldr(Fun, Acc0, Array) ->
165    array:sparse_foldr(Fun, Acc0, Array).
166
167-spec sparse_size(array(_T)) -> array_size().
168sparse_size(Array) ->
169    array:sparse_size(Array).
170