Mercurial > repos > public > sbplib
comparison SolutionFile.m @ 9:6b9b2283e7ed
Added class for handleing .mat files as a key-value store.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 21 Sep 2015 17:41:18 +0200 |
parents | |
children | 4d8068cb5c65 |
comparison
equal
deleted
inserted
replaced
8:67ab7fd8054c | 9:6b9b2283e7ed |
---|---|
1 classdef SolutionFile < handle | |
2 properties | |
3 filename | |
4 matfile | |
5 % keyHeaders | |
6 keys % Cell array of keys. Each key is a structure | |
7 % entries | |
8 end | |
9 | |
10 methods | |
11 function obj = SolutionFile(filename) | |
12 | |
13 obj.filename = filename; | |
14 | |
15 is_new_file = ~exist(filename,'file'); | |
16 | |
17 obj.matfile = matfile(filename,'Writable',true); | |
18 | |
19 if is_new_file | |
20 obj.matfile.keys = {}; | |
21 obj.matfile.entries = {}; | |
22 end | |
23 | |
24 % obj.keyHeaders = obj.matfile.keyHeaders; | |
25 obj.keys = obj.matfile.keys; | |
26 | |
27 end | |
28 | |
29 function list(obj, show_syntax) | |
30 default_arg('show_syntax',false); | |
31 for i = 1:length(obj.keys) | |
32 fprintf('[%d]: %s', i, struct2string(obj.keys{i})); | |
33 | |
34 if show_syntax | |
35 fprintf('\t%s',struct2syntax(obj.keys{i})); | |
36 end | |
37 | |
38 fprintf('\n'); | |
39 end | |
40 end | |
41 | |
42 % Returns the index for a key. Returns 0 if the key doesn't exist | |
43 function I = getIndex(obj, key) | |
44 I = 0; | |
45 | |
46 for i = 1:length(obj.keys) | |
47 if isequal(key,obj.keys{i}); | |
48 I = i; | |
49 return | |
50 end | |
51 end | |
52 end | |
53 | |
54 function b = isKey(obj, key) | |
55 I = obj.getIndex(key); | |
56 | |
57 if I == 0 | |
58 b = false; | |
59 else | |
60 b = true; | |
61 end | |
62 end | |
63 | |
64 % Gets entries where key match exactly. | |
65 function e = get(obj, key) | |
66 if ~obj.isKey(key); | |
67 error('No such key: %s', struct2string(key)); | |
68 end | |
69 | |
70 I = obj.getIndex(key); | |
71 e = getEntryByIndex(I); % unpack the cell array | |
72 end | |
73 | |
74 | |
75 % Handles indexing weirdness of matfile class | |
76 function e = getEntryByIndex(obj, I) | |
77 e = obj.matfile.entries(1,I); | |
78 e = e{1}; | |
79 end | |
80 | |
81 function e = deleteByIndex(obj, I) | |
82 obj.keys(I) = []; | |
83 obj.matfile.keys = obj.keys; | |
84 | |
85 entries = obj.matfile.entries; | |
86 entries(I) = []; | |
87 obj.matfile.entries = entries; | |
88 end | |
89 | |
90 % Handles indexing weirdness of matfile class | |
91 function setEntryByIndex(obj,I, entry, force_flag) | |
92 default_arg('force_flag',false); | |
93 if ~force_flag && ( I < 1 || I > length(obj.keys)) | |
94 error('I is out of range. I = %d',I); | |
95 end | |
96 obj.matfile.entries(1,I) = {entry}; | |
97 end | |
98 | |
99 function store(obj, key, entry) | |
100 if obj.isKey(key); | |
101 I = obj.getIndex(key); | |
102 else | |
103 I = length(obj.keys) + 1; | |
104 end | |
105 | |
106 obj.keys{I} = key; | |
107 obj.matfile.keys = obj.keys; | |
108 obj.setEntryByIndex(I,entry,true); | |
109 end | |
110 | |
111 function delete(obj, key) | |
112 if ~obj.isKey(key); | |
113 error('No such key: %s', struct2string(key)); | |
114 end | |
115 I = obj.getIndex(key); | |
116 obj.deleteByIndex(I); | |
117 end | |
118 | |
119 | |
120 | |
121 % Gets entries where the defined parts of partial_key matches the key of the entry | |
122 function [keys, entries] = find(obj,partial_key) | |
123 keys = {}; | |
124 entries = {}; | |
125 for i = 1:length(obj.keys) | |
126 if structIsSubset(partial_key,obj.keys{i}) | |
127 i | |
128 obj.keys{i} | |
129 keys{end + 1} = obj.keys{i}; | |
130 entries{end + 1} = obj.getEntryByIndex(i); | |
131 end | |
132 end | |
133 | |
134 | |
135 % Returns true if the the fields of struct a exists in A and have the same values | |
136 function b = structIsSubset(a,A) | |
137 fn = fieldnames(a); | |
138 | |
139 b = true; % if a has no filds | |
140 for j = 1:length(fn) | |
141 fname = fn{j}; | |
142 value = a.(fname); | |
143 if isfield(A,fname) && a.(fname) == A.(fname) | |
144 b = true; | |
145 continue; | |
146 else | |
147 b = false; | |
148 break; | |
149 end | |
150 end | |
151 end | |
152 end | |
153 end | |
154 | |
155 end |