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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart._vmservice;
6
7 /// Set like containes which automatically generated String ids for its items
8 class NamedLookup<E> extends Object with IterableMixin<E> {
9 final IdGenerator _generator;
10 final Map<String, E> _elements = new Map<String, E>();
11 final Map<E, String> _ids = new Map<E, String>();
12
13 NamedLookup({String prologue = ''})
14 : super(),
15 _generator = new IdGenerator(prologue: prologue);
16
17 void add(E e) {
18 final id = _generator.newId();
19 _elements[id] = e;
20 _ids[e] = id;
21 }
22
23 void remove(E e) {
24 final id = _ids.remove(e);
25 _elements.remove(id);
26 _generator.release(id);
27 }
28
29 E operator [](String id) => _elements[id];
30 String keyOf(E e) => _ids[e];
31
32 Iterator<E> get iterator => _ids.keys.iterator;
33 }
34
35 /// Generator for unique ids which recycles expired ones
36 class IdGenerator {
37 /// Fixed initial part of the id
38 final String prologue;
39 // Ids in use
40 final Set<String> _used = new Set<String>();
41
42 /// Ids that has been released (use these before generate new ones)
43 final Set<String> _free = new Set<String>();
44
45 /// Next id to generate if no one can be recycled (first use _free);
46 int _next = 0;
47
48 IdGenerator({this.prologue = ''});
49
50 /// Returns a new Id (possibly recycled)
51 String newId() {
52 var id;
53 if (_free.isEmpty) {
54 id = prologue + (_next++).toString();
55 } else {
56 id = _free.first;
57 }
58 _free.remove(id);
59 _used.add(id);
60 return id;
61 }
62
63 /// Releases the id and mark it for recycle
64 void release(String id) {
65 if (_used.remove(id)) {
66 _free.add(id);
67 }
68 }
69 }
OLDNEW
« 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