- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 HashMap。当我遍历 map 时,数据以(通常是相同的)随机顺序返回。但是数据是按特定顺序插入的,我需要保留插入顺序。我怎样才能在 Vala 中做到这一点?在 Java 中有 LinkedHashMap,但我没有看到 Gee.Map 的任何等效项。
最佳答案
据我所知,Vala 中没有 LinkedHashMap 的等价物。使用 TreeMap 并将比较函数设置为始终为其他 Map 条目返回 1(如果您想要相反的顺序,则为 -1)将保留顺序并允许您按照添加项目的顺序遍历 Map 但 get
将无法按预期运行。
不幸的是,在彻底检查 Gee 源之后,除了自己动手,似乎别无他法。最直接的方法是继承 HashMap 并使用 ArrayList 来跟踪键插入时的顺序。您也可以使用 LinkedList,只需将内部 ArrayList _keys 字段更改为 LinkedList。选择取决于您的用例。来自文档-
This implementation (ArrayList) is pretty good for rarely modified data. Because they are stored in an array this structure does not fit for highly mutable data.
下面是一个基本的实现,在 Vala (arrayhashmap.vala) 中:
using Gee;
public class ArrayHashMap<K,V> : HashMap<K,V> {
private weak Set<K> _keyset;
private weak Collection<V> _values;
private weak Set<Entry<K,V>> _entries;
internal ArrayList<K> _keys = new ArrayList<K>();
private class KeySet<K> : AbstractSet<K> {
private weak ArrayList<K> _keys;
public KeySet (ArrayList<K> keys) {
_keys = keys;
}
public override Iterator<K> iterator () {
return _keys.iterator();
}
public override int size {
get { return _keys.size; }
}
public override bool read_only {
get { return true; }
}
public override bool add (K key) {
assert_not_reached ();
}
public override void clear () {
assert_not_reached ();
}
public override bool remove (K key) {
assert_not_reached ();
}
public override bool contains (K key) {
return _keys.contains (key);
}
}
private class ValueCollection<K,V> : AbstractCollection<V> {
private weak ArrayHashMap<K,V> _map;
public ValueCollection (ArrayHashMap map) {
_map = map;
}
public override Iterator<V> iterator () {
return new ValueIterator<K,V> (_map);
}
public override int size {
get { return _map.size; }
}
public override bool read_only {
get { return true; }
}
public override bool add (V value) {
assert_not_reached ();
}
public override void clear () {
assert_not_reached ();
}
public override bool remove (V value) {
assert_not_reached ();
}
public override bool contains (V value) {
Iterator<V> it = iterator ();
while (it.next ()) {
if (_map.value_equal_func (it.get (), value)) {
return true;
}
}
return false;
}
}
private class ValueIterator<K,V> : Object, Traversable<V>, Iterator<V> {
protected weak ArrayHashMap<K,V> _map;
protected Iterator<K> _keys;
public ValueIterator (ArrayHashMap<K,V> map) {
_map = map;
_keys = map._keys.iterator();
}
public bool next () {
return _keys.next();
}
public bool has_next () {
return _keys.has_next();
}
public virtual bool read_only {
get {
return true;
}
}
public bool valid {
get {
return _keys.valid;
}
}
public new V get () {
return _map.get(_keys.get());
}
public void remove () {
assert_not_reached ();
}
public bool foreach(ForallFunc<V> f) {
foreach (K key in _map._keys)
if (!f(_map.get(key)))
return false;
return true;
}
}
private class EntrySet<K,V> : AbstractSet<Entry<K, V>> {
private weak ArrayHashMap<K,V> _map;
public EntrySet (ArrayHashMap<K,V> map) {
_map = map;
}
public override Iterator<Entry<K, V>> iterator () {
return new EntryIterator<K,V> (_map);
}
public override int size {
get { return _map.size; }
}
public override bool read_only {
get { return true; }
}
public override bool add (Entry<K, V> entry) {
assert_not_reached ();
}
public override void clear () {
assert_not_reached ();
}
public override bool remove (Entry<K, V> entry) {
assert_not_reached ();
}
public override bool contains (Entry<K, V> entry) {
return _map.has (entry.key, entry.value);
}
}
private class EntryIterator<K,V> : Object, Traversable<Entry<K,V>>, Iterator<Entry<K,V>> {
protected weak ArrayHashMap<K,V> _map;
protected Iterator<K> _keys;
public EntryIterator (ArrayHashMap<K,V> map) {
_map = map;
_keys = map._keys.iterator();
}
public bool next () {
return _keys.next();
}
public bool has_next () {
return _keys.has_next();
}
public virtual bool read_only {
get {
return true;
}
}
public bool valid {
get {
return _keys.valid;
}
}
public new Entry<K,V> get () {
K* k = _keys.get();
var ent = new Entry<K,V>(k, _map.get(k));
return ent;
}
public void remove () {
assert_not_reached ();
}
public bool foreach(ForallFunc<Entry<K,V>> f) {
foreach (K key in _map._keys)
if (!f(new Entry<K,V>(key, _map.get(key))))
return false;
return true;
}
}
public class Entry<K,V> : Map.Entry<K,V> {
weak K _key;
weak V _value;
public override K key {
get {
return _key;
}
}
public override V value {
get {
return _value;
} set {
_value = value;
}
}
public override bool read_only {get { return true; }}
public Entry (K key, V value) {
this._key = key;
this._value = value;
}
}
public new void @set(K key, V value) {
if (!_keys.contains(key))
_keys.add(key);
base.set(key, value);
}
public new void unset(K key, out V? value = null) {
_keys.remove(key);
base.unset(key, out value);
}
public new void clear() {
base.clear();
_keys.clear();
}
public new Set<unowned K> keys {
owned get {
Set<K> keys = _keyset;
if (_keyset == null) {
keys = new KeySet<K> (_keys);
_keyset = keys;
keys.add_weak_pointer ((void**) (&_keyset));
}
return keys;
}
}
public new Collection<unowned V> values {
owned get {
Collection<K> values = _values;
if (_values == null) {
values = new ValueCollection<K,V> (this);
_values = values;
values.add_weak_pointer ((void**) (&_values));
}
return values;
}
}
public override Set<Entry<K,V>> entries {
owned get {
Set<Entry<K,V>> entries = _entries;
if (_entries == null) {
entries = new EntrySet<K,V> (this);
_entries = entries;
entries.add_weak_pointer ((void**) (&_entries));
}
return entries;
}
}
}
你可以用这个糟糕的测试用例 (tests.vala) 来测试它:
public static void doTest() {
const string[] strings = { "test", "another", "one-more", "how-about-this-one", "even-more" };
var entries3 = new ArrayHashMap<string, int>();
for (int i = 0; i < strings.length; i++)
entries3.set(strings[i], i);
entries3.unset("one-more");
foreach (var entry in entries3.keys)
message ("%s:%d", entry, entries3.get(entry));
entries3.set ("for-your-viewing-pleasure", 3);
foreach (var entry in entries3.keys)
message ("%s:%d", entry, entries3.get(entry));
entries3.set ("for-your-viewing-pleasure", 7);
foreach (var entry in entries3.entries)
message ("%s:%d", entry.key, entries3.get(entry.key));
}
public static int main (string[] args) {
Test.init(ref args);
Test.add_func ("/ArrayHashMap", doTest);
Test.run();
return 0;
}
一起运行整个包:
valac --pkg gee-0.8 -g tests.vala arrayhashmap.vala
这是一个非常粗略的实现,基于 HashMap 的内部工作方式。您可能想要重构它以获得更好的可维护性并编写更多单元测试。如果您发现任何问题,请告诉我,我们可以解决这些问题。
希望对您有所帮助。
关于vala - 如何在 Vala 的 HashMap 中保留插入顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603925/
如何在 Vala 中创建周期性任务? Vala 中是否有可以在指定时间段调用预定方法的计时器类?如果是,请提供示例代码。 更新: 这是基于答案和评论的示例代码: public class Sample
有没有人尝试在Vala中编写pidgin或libpurple插件?快速浏览Vala homepage似乎是一种很棒的语言,并且是学习Vala的好机会,但是同时学习Vala和pidgin插件的精湛技巧有
Vala 有函数静态变量吗? 我所说的“函数静态变量”是指在函数内部声明的变量,它在调用之间保持其值,如以下 C 示例所示: #include void foo() { int a = 10
在 Java 中你可以有这样的东西 class MyClass { ... 在 C# 中 class MyClass where E : A { ... Vala 中有类似的东西吗? 最佳答案 不,V
Vala Tutorial提到了内置数组的以下方法和属性(和运算符): arr.length arr += element arr.resize() arr.move() (“内置数组”是指像 int
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
在 Vala 的默认应用程序中打开文件的最佳方式是什么? 有点像xdg-open作品。 最佳答案 我在另一个应用程序中找到了一些现有的代码,但后来我也发现了这个GLib.AppInfo.launch_
在 Vala 中,我看到当我声明一个数组时我必须指定类型,比如 int[] myarray = { 1, 2, 3 }; 我想知道是否有一种方法可以像这样混合数组 smtg[] myarray = {
我想在我的应用程序中使用颜色选择器对话框 - https://github.com/satya164/gtk-theme-config 我在 vala 中找不到关于 ColorChooserDialo
我是瓦拉的新手。我对 GObject 不熟悉。据我了解,GObject 是从 GNOME 的 GLib 项目中分离出来的。如果我错了请纠正我。 我确实非常喜欢 Vala 的语法和实现,但我无意为 GN
假设我有一个 C 代码 (dcomplex.h): typedef double dcomplex[2]; 并且想在vala中使用这样的数据类型。 最小的 vapi 文件和 vala 调用是什么? (
我正在尝试使用 Vala 创建一个使用 Glib.Settings 的应用程序。如果架构或键不存在,我不希望我的应用程序崩溃。我已经明白我无法捕获其中的错误( How to handle errors
我正在为 LAME 编码器构建一个前端,以便在 Gtk/Vala 中进行概念验证。 我已经安装了 libmp3lame-dev 并希望使用一些给定的编码参数对 WAV 文件进行简单编码,并使用 Gtk
我正在使用 HashMap。当我遍历 map 时,数据以(通常是相同的)随机顺序返回。但是数据是按特定顺序插入的,我需要保留插入顺序。我怎样才能在 Vala 中做到这一点?在 Java 中有 Link
我在 Vala 应用程序中使用 Glib.Settings。我想确保即使模式或 key 不可用,我的程序也能正常工作。所以我添加了一个 try/catch 块,但是如果我使用不存在的 key ,程序就
在 Linux 中,我在 Gnome Builder (3.26.4) 中使用 Meson (0.44.0) 作为控制台程序,该程序将使用 Gee 和 GXml。我的意图是在 Genie 中写这个。
我刚刚运行了这段代码,它对我来说看起来是不错的语言。 但是当我在 NetBeans IDE 中尝试时,我没有看到任何 Vala,也没有看到任何自动完成可用性。 知道如何让 NetBeans IDE 支
将此库与 Vala 一起使用: http://valadoc.org/#!api=glib-2.0/GLib.DateTime GLib.DateTime now = new GLib.Dat
我正在 win32 上使用 Vala 开发小型命令行实用程序。使用 vala 编译的程序依赖于以下 DLL libgobject-2.0-0.dll libgthread-2.0-0.dll libg
我想使用模拟对象编写单元测试,例如 Moq或 NSubstitute对于 C#。 有人可以推荐一个与 Vala 一起使用的模拟库吗? 最佳答案 由于 Vala 生成 C 代码(使用 GObject 库
我是一名优秀的程序员,十分优秀!