Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Utils.vala « src « libdecsync « decsync « backend « plugins - github.com/jangernert/FeedReader.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 92d1704269f2cf6fc1f47718c8df34c79c2c5bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * libdecsync-vala - Utils.vala
 *
 * Copyright (C) 2018 Aldo Gunsing
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

public Gee.List<string> toList(string[] input)
{
	return new Gee.ArrayList<string>.wrap(input);
}

public Gee.Predicate<Json.Node> stringEquals(string input)
{
    return json => {
        return json.get_string() == input;
    };
}

public Json.Node boolToNode(bool input)
{
	var node = new Json.Node(Json.NodeType.VALUE);
	node.set_boolean(input);
	return node;
}

public Json.Node stringToNode(string? input)
{
	Json.Node node;
	if (input == null) {
		node = new Json.Node(Json.NodeType.NULL);
	} else {
		node = new Json.Node(Json.NodeType.VALUE);
		node.set_string(input);
	}
	return node;
}

public Json.Node objectToNode(Json.Object input)
{
	var node = new Json.Node(Json.NodeType.OBJECT);
	node.set_object(input);
	return node;
}

[Version (deprecated = true, deprecated_since = "1.1.1", replacement = "groupByPath")]
public Gee.MultiMap<K, V> groupBy<T, K, V>(Gee.Collection<T> inputs, Gee.MapFunc<K, T> k, Gee.MapFunc<V, T>? f = null)
{
	var resultsMap = new Gee.HashMultiMap<K, V>();
	foreach (var input in inputs)
	{
		var key = k(input);
		var value = f == null ? input : f(input);
		resultsMap.@set(key, value);
	}

	return resultsMap;
}

public int pathCompare(Gee.List<string> lhs, Gee.List<string> rhs)
{
        for (int i = 0; i < lhs.size && i < rhs.size; ++i) {
                if (lhs[i] < rhs[i]) return -1;
                if (lhs[i] > rhs[i]) return 1;
        }
        if (lhs.size < rhs.size) return -1;
        if (lhs.size > rhs.size) return 1;
        return 0;
}

public Gee.MultiMap<Gee.List<string>, V> groupByPath<T, V>(
        Gee.Collection<T> inputs,
        Gee.MapFunc<Gee.List<string>, T> toPath,
        Gee.MapFunc<V, T>? f = null)
{
        var resultsMap = new Gee.TreeMultiMap<Gee.List<string>, V>(pathCompare);
        foreach (var input in inputs)
        {
                var path = toPath(input);
                var value = f == null ? input : f(input);
                resultsMap.@set(path, value);
        }
        return resultsMap;
}