{"id":1158,"date":"2014-05-07T04:55:53","date_gmt":"2014-05-07T04:55:53","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1158"},"modified":"2014-05-08T07:19:26","modified_gmt":"2014-05-08T07:19:26","slug":"unity3d-list-compare","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1158\/","title":{"rendered":"Unity3D List compare"},"content":{"rendered":"<p>\u6628\u5929\u8655\u7406\u5230 server room \u7684 list update, \u5e0c\u671b\u5617\u8a66\u00a0list compare delegates, \u7d50\u679c\u628a\u6574\u500b\u7de8\u7a0b\u601d\u8def\u91cd\u65b0\u512a\u5316.<\/p>\n<p>\u539f\u672c\u662f\u5e0c\u671b\u627e\u51fa 2 \u689d list \u4e2d\u7684\u7684\u4e0d\u540c\u7684\u5730\u65b9\u4e26\u4e14\u9032\u884c\u4e0d\u540c\u7684\u64cd\u4f5c.<\/p>\n<pre class=\"brush:csharp\">List&lt;RoomInfo&gt; _newRooms = _serverRooms.FindAll(delegate(RoomInfo thisRoom) {\r\n\treturn !mRooms.Exists(delegate(RoomInfo i){\r\n\t\t\t\treturn i.name!=thisRoom.name;\r\n\t\t\t});\r\n});\r\n\r\nList&lt;RoomInfo&gt; _existButUpdatedRooms = _serverRooms.FindAll(delegate(RoomInfo thisRoom) {\r\n\tif( mRooms.Exists(delegate(RoomInfo i){return i.name!=thisRoom.name;}) )\r\n\t{\r\n\t\treturn false; \/\/ exists but same.\r\n\t}\r\n\tRoomInfo _found = mRooms.Find(delegate(RoomInfo obj) {\r\n\t\treturn thisRoom.name.Equals(obj.name);\r\n\t});\r\n\tif( ReferenceEquals(null,_found) )\r\n\t{\r\n\t\treturn false; \/\/ not exists\r\n\t}\r\n\tif( thisRoom.playerCount!=_found.playerCount )\r\n\t{\r\n\t\treturn true; \/\/ need update.\r\n\t}\r\n\t\/\/ little difference, but we don't care.\r\n\treturn false;\r\n});\r\n\r\n\/\/ find non-existed room, remove those in mRooms\r\nmRooms.RemoveAll(delegate(RoomInfo thisRoom) {\r\n\treturn _serverRooms.Exists(delegate(RoomInfo i){\r\n\t\t\t\treturn i.name!=thisRoom.name;\r\n\t\t\t} );\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>\u9019\u662f\u7b2c\u4e00\u6b21\u5beb\u7684\u5783\u573e code \u4e5f\u4e0d\u77e5\u9053\u662f\u5426\u80fd\u8dd1 XD, \u4e00\u5806 delegate, \u770b\u4e5f\u770b\u50bb\u4e86.<\/p>\n<p>\u89ba\u5f97\u592a\u7169\u7463\u5c31\u627e\u4e00\u4e0b\u662f\u5426\u6709 short form. \u7136\u5f8c\u5c31\u4fee\u6539\u70ba<\/p>\n<pre class=\"brush:csharp\">List&lt;RoomInfo&gt; _newRooms = _serverRooms.FindAll(thisRoom =&gt;\r\n\t!mRooms.Exists(i=&gt; i.name!=thisRoom.name)\r\n);\r\n\r\nList&lt;RoomInfo&gt; _existButUpdatedRooms = _serverRooms.FindAll(thisRoom=&gt;{\r\n\tif( mRooms.Exists(i =&gt; i.name!=thisRoom.name) )\r\n\t{\r\n\t\treturn false; \/\/ exists but same.\r\n\t}\r\n\tRoomInfo _found = mRooms.Find(obj =&gt; thisRoom.name.Equals(obj.name));\r\n\tif( ReferenceEquals(null,_found) )\r\n\t{\r\n\t\treturn false; \/\/ not exists\r\n\t}\r\n\tif( thisRoom.playerCount!=_found.playerCount )\r\n\t{\r\n\t\treturn true; \/\/ need update.\r\n\t}\r\n\t\/\/ little difference, but we don't care.\r\n\treturn false;\r\n});\r\n\r\n\/\/ find non-existed room, remove those in mRooms\r\nmRooms.RemoveAll(thisRoom =&gt; _serverRooms.Exists(i=&gt;i.name!=thisRoom.name));<\/pre>\n<p>\u958b\u59cb\u627e\u5230\u611f\u89ba, \u7136\u5f8c\u53c8\u767c\u73fe\u4e00\u4e9b\u56e0\u70ba\u5beb\u592a\u9577\u592a\u66f2\u6298\u6240\u4ee5\u60f3\u932f\u7684\u5730\u65b9&#8230; double anti&#8230; \u4e4b\u985e\u7684\u5730\u65b9.<\/p>\n<p>\u4e5f\u628a\u5e38\u7528\u7684 room name checking \u53e6\u5beb function.<\/p>\n<pre class=\"brush:csharp\">_serverRooms.ForEach(_room =&gt; {\r\n\tRoomInfo _found = mOldRooms.Find(pt=&gt;IsSameRoom(pt,_room) );\r\n\tif( _found!=null )\r\n\t{\r\n\t\t\/\/ update player numbers.\r\n\t}\r\n\telse\r\n\t{\r\n\t\t\/\/ if it's a new room\r\n\t}\r\n});\r\n\r\n\/\/ find non-existed room, remove those in mRooms\r\nmOldRooms.RemoveAll(_room =&gt; !_serverRooms.Exists(pt=&gt; IsSameRoom(pt,_room)) );\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nprivate bool IsSameRoom(RoomInfo _r1, RoomInfo _r2)\r\n{\r\n\treturn _r1.name.Equals(_r2.name);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u53c8\u4e00\u6b21\u8a3c\u660e\u628acoding \u5beb\u5f97\u7cbe\u7c21\u771f\u7684\u5f88\u91cd\u8981. (O.o)&gt;<br \/>\n<a href=\"http:\/\/huan-lin.blogspot.com\/2009\/01\/from-lambda-to-linq.html\">Huan-Lin <\/a>\u6587\u7ae0\u4e2d\u6709\u5217\u51fa4\u7a2e\u7684\u8b8a\u7a2e\u5beb\u6cd5.<\/p>\n<pre class=\"brush:csharp\">Predicate&lt;Employee&gt; p1 = delegate(Employee e)    { return e.Name.StartsWith(\"J\"); };\r\nPredicate&lt;Employee&gt; p2 =                  (e) =&gt; { return e.Name.StartsWith(\"J\"); };\r\nPredicate&lt;Employee&gt; p3 =                   e  =&gt; { return e.Name.StartsWith(\"J\"); };\r\nPredicate&lt;Employee&gt; p4 =                   e  =&gt;          e.Name.StartsWith(\"J\")   ;<\/pre>\n<p>\u53c3\u8003\u6587\u4ef6 : delegate<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/zh-tw\/library\/bb882516.aspx\">http:\/\/msdn.microsoft.com\/zh-tw\/library\/bb882516.aspx<\/a><\/li>\n<li><a href=\"http:\/\/huan-lin.blogspot.com\/2009\/01\/from-lambda-to-linq.html\">http:\/\/huan-lin.blogspot.com\/2009\/01\/from-lambda-to-linq.html<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6628\u5929\u8655\u7406\u5230 server room \u7684 list update, \u5e0c\u671b\u5617\u8a66\u00a0list compare &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[54,43],"class_list":["post-1158","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-delegate","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1158"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1158\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}