Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Unified Diff: sdk/lib/vmservice/named_lookup.dart

Issue 2980733003: Introduced support for external services registration in the ServiceProtocol (Closed)
Patch Set: Address comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/vmservice/message_router.dart ('k') | sdk/lib/vmservice/running_isolate.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/vmservice/named_lookup.dart
diff --git a/sdk/lib/vmservice/named_lookup.dart b/sdk/lib/vmservice/named_lookup.dart
new file mode 100644
index 0000000000000000000000000000000000000000..1dab5f7409f9affead1b3ca16d9aef97867dca54
--- /dev/null
+++ b/sdk/lib/vmservice/named_lookup.dart
@@ -0,0 +1,69 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of dart._vmservice;
+
+/// Set like containes which automatically generated String ids for its items
+class NamedLookup<E> extends Object with IterableMixin<E> {
+ final IdGenerator _generator;
+ final Map<String, E> _elements = new Map<String, E>();
+ final Map<E, String> _ids = new Map<E, String>();
+
+ NamedLookup({String prologue = ''})
+ : super(),
+ _generator = new IdGenerator(prologue: prologue);
+
+ void add(E e) {
+ final id = _generator.newId();
+ _elements[id] = e;
+ _ids[e] = id;
+ }
+
+ void remove(E e) {
+ final id = _ids.remove(e);
+ _elements.remove(id);
+ _generator.release(id);
+ }
+
+ E operator [](String id) => _elements[id];
+ String keyOf(E e) => _ids[e];
+
+ Iterator<E> get iterator => _ids.keys.iterator;
+}
+
+/// Generator for unique ids which recycles expired ones
+class IdGenerator {
+ /// Fixed initial part of the id
+ final String prologue;
+ // Ids in use
+ final Set<String> _used = new Set<String>();
+
+ /// Ids that has been released (use these before generate new ones)
+ final Set<String> _free = new Set<String>();
+
+ /// Next id to generate if no one can be recycled (first use _free);
+ int _next = 0;
+
+ IdGenerator({this.prologue = ''});
+
+ /// Returns a new Id (possibly recycled)
+ String newId() {
+ var id;
+ if (_free.isEmpty) {
+ id = prologue + (_next++).toString();
+ } else {
+ id = _free.first;
+ }
+ _free.remove(id);
+ _used.add(id);
+ return id;
+ }
+
+ /// Releases the id and mark it for recycle
+ void release(String id) {
+ if (_used.remove(id)) {
+ _free.add(id);
+ }
+ }
+}
« no previous file with comments | « sdk/lib/vmservice/message_router.dart ('k') | sdk/lib/vmservice/running_isolate.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698