Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, 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 library object_graph; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 import 'dart:typed_data'; | |
| 9 | |
| 10 // Port of dart::ReadStream from vm/datastream.h. | |
| 11 class ReadStream { | |
| 12 int _cur = 0; | |
| 13 ByteData _data; | |
|
Cutch
2014/12/02 16:46:26
could this be final?
koda
2014/12/02 18:47:12
Done.
| |
| 14 | |
| 15 ReadStream(this._data); | |
| 16 | |
| 17 int get pendingBytes => _data.lengthInBytes - _cur; | |
| 18 | |
| 19 int readUnsigned() { | |
| 20 int result = 0; | |
| 21 int shift = 0; | |
| 22 while (_data.getUint8(_cur) <= maxUnsignedDataPerByte) { | |
| 23 result |= _data.getUint8(_cur) << shift; | |
| 24 shift += dataBitsPerByte; | |
| 25 ++_cur; | |
| 26 } | |
| 27 result |= (_data.getUint8(_cur) & byteMask) << shift; | |
| 28 ++_cur; | |
| 29 return result; | |
| 30 } | |
| 31 | |
| 32 static const int dataBitsPerByte = 7; | |
| 33 static const int byteMask = (1 << dataBitsPerByte) - 1; | |
| 34 static const int maxUnsignedDataPerByte = byteMask; | |
| 35 } | |
| 36 | |
| 37 class ObjectVertex { | |
| 38 int _id; | |
|
Cutch
2014/12/02 16:46:26
could this be final?
koda
2014/12/02 18:47:12
Done.
| |
| 39 int size; | |
| 40 int classId; | |
|
Cutch
2014/12/02 16:46:26
could these be made final? or at least have non-pu
koda
2014/12/02 18:47:12
Hid the setters.
| |
| 41 List<ObjectVertex> succ = new List<ObjectVertex>(); | |
|
Cutch
2014/12/02 16:46:26
could this be final?
koda
2014/12/02 18:47:12
Done.
| |
| 42 ObjectVertex(this._id); | |
| 43 } | |
| 44 | |
| 45 // See implementation of ObjectGraph::Serialize for format. | |
| 46 class ObjectGraph { | |
| 47 Map<int, ObjectVertex> _idToVertex = new Map<int, ObjectVertex>(); | |
|
Cutch
2014/12/02 16:46:26
could this be final?
koda
2014/12/02 18:47:12
Done.
| |
| 48 | |
| 49 ObjectVertex _asVertex(int id) { | |
| 50 return _idToVertex.putIfAbsent(id, () => new ObjectVertex(id)); | |
| 51 } | |
| 52 | |
| 53 void _addFrom(ReadStream stream) { | |
| 54 ObjectVertex obj = _asVertex(stream.readUnsigned()); | |
| 55 obj.size = stream.readUnsigned(); | |
| 56 obj.classId = stream.readUnsigned(); | |
|
Cutch
2014/12/02 16:46:26
if you read the id, size, and classId and then cal
koda
2014/12/02 18:47:12
No.
| |
| 57 int last = stream.readUnsigned(); | |
| 58 while (last != 0) { | |
| 59 obj.succ.add(_asVertex(last)); | |
|
Cutch
2014/12/02 16:46:26
Oh, I see why you can't make _asVertex take all th
koda
2014/12/02 18:47:12
Right. I added a comment explaining the two-stage
| |
| 60 last = stream.readUnsigned(); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 ObjectGraph(ReadStream reader) { | |
| 65 while (reader.pendingBytes > 0) { | |
| 66 _addFrom(reader); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 Iterable<ObjectVertex> get vertices => _idToVertex.values; | |
| 71 } | |
| OLD | NEW |