Esbasic reusable. Net class library (11) -- bidirectional ibing ibidirectionalmapping

Source: Internet
Author: User

1.Origin:

Assume that our user management system requires the user's ID And Name Must be unique and the user's ID And Name Once confirmed, it cannot be modified. And the management system often needs ID To find Name , Also often need to follow Name To find ID . Based on this requirement, we can consider using Dictionary To ID And Name Cache, usually ID As Key , Name As Value . In this way ID Query Name However Name Search ID It's not that fast, because it involves Dictionary Of Values Perform traversal. Then, it is possible that Name Search ID Speed and pass ID Search Name Is the speed as fast as possible?

So I designedEsbasic. objectmanagement. cache. ibidirectionalmapping(Bidirectional ing) to solve this problem.

The image of Bidirectional ing is as follows:

2.Applicable scenarios:

If the following conditions are met, you can use bidirectional ing:

(1)KeyIs unique,ValueIt is also unique.

(2)NeedKeyAndValueCache.

(3)It is often necessaryKeyTo findValue.

(4)It is often necessaryValueTo findKey.

 

3Design ideas and implementation

IbidirectionalmappingThe interface is defined as follows:

///   <Summary>
/// Ibidirectionalmapping. That is, keys and values are unique. In this case, using ibidirectionalmapping can speed up key searching based on values.
/// The implementation of this interface must be thread-safe. 2008.08.20
///   </Summary>
Public   Interface   Ibidirectionalmapping < T1, T2 >
{
Int Count { Get ;}

/// <Summary>
///Add add ing. If the same key/value already exists, it will overwrite it.
/// </Summary>
VoidAdd (T1 T1, T2 T2 );

VoidRemovebyt1 (T1 T1 );
VoidRemovebyt2 (t2 T2 );

T1 gett1 (t2 T2 );
T2 gett2 (T1 T1 );

BoolContainst1 (T1 T1 );
BoolContainst2 (t2 T2 );

//


// getallt1listcopy returns a copy of The T1 type element list.
//
ilist t1 > getallt1listcopy ();

///   <Summary>
/// Getallt2listcopy returns the copy of the list of T2 elements.
///   </Summary>
Ilist < T2 > Getallt2listcopy ();
}

 

This interface uses two generic parameters. According to the above description, one generic parameter represents Key Another generic parameter Vlaue . Because, in bidirectional ing, Key And Value Is symmetric, so I didn't use Tkey And Tvalue To name them, but to use T1 And T2 .

In implementation Bidirectionalmapping We use two Dictionary To complete the bidirectional ing function. One Dictionary To T1 Is Key , T2 Is Value The other is exactly the opposite.

Pay attention to the following points during implementation:

(1)To allow bidirectional ing in a multi-threaded environmentBidirectionalmappingMust be insideDictionaryLock control during operation.

(2)In implementationAddWhen adding a "ing pair", you must determine whether the same value already exists. If yes, delete the old ing pair and add a new ing pair.

(3) Pay attention to a detail, Getallt1listcopy And Getallt2listcopy Is used Lock This is because Keys Or Values Proceed Foreach In Dictionary In Foreach When traversing, if you add or delete elements to the element Foreach The operation throws an exception.

 

4.Precautions for use

BidirectionalmappingUpgradedNameSearchIDThis is done by using a larger memory. It is a typical example of "space for time. Therefore, pay attention to the memory usage for the caching of ing pairs of a large scale.

In addition, the types of the two elements in the ing pair are not necessarilyIDOrNameSuch a simple object, in fact, very complex objects can also be cached in bidirectional ing, as long as theirGethashcodeIf the method is implemented properly, there will be no problems.

 

5.Extension

Bidirectional ingBidirectionalmappingThere are currently no extensions.

Note: The esbasic source code can be foundHttp://esbasic.codeplex.com/Download.
Esbasic open source Preface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.