tag 标签: union

相关博文
  • 热度 23
    2012-12-28 12:20
    1614 次阅读|
    0 个评论
    I just learned a whole bunch of new "stuff" that's why I'm currently as happy as... well, something that's very happy. It all came about when, a week or so ago, I started re-watching all of the new Doctor Who episodes starting with "Rose" from 2005. In fact, I was watching one last night where someone mentioned "The Union Jack" (the national flag of the United Kingdom) and Rose noted that it should more properly be called "The Union Flag"; also that "Union Jack" should only be used if the flag was flying on one of Her Majesty's Warships. Well, I'm always interested in discovering something new, so a few minutes ago I bounced over to the Wikipedia to learn more ( Click Here to visit the Union Jack page on the Wikipedia). From there I became embroiled in a delightful morass of terminology, including works like Blazon , which is a formal description of a coat of arms, flag, or similar emblem, from which the reader can reconstruct the appropriate image. As the Wikipedia says: The visual depiction of a coat of arms or flag traditionally has considerable latitude in design, while a blazon specifies the essentially distinctive elements; thus it can be said that a coat of arms or flag is primarily defined not by a picture but rather by the wording of its blazon. Blazon also refers to the specialized language in which a blazon is written, and, as a verb, to the act of writing such a description. This language has its own vocabulary, grammar, and syntax (rules governing word order), which becomes essential for comprehension when blazoning a complex coat of arms. As you may or may not know, the Union Jack is actually formed from three major elements: The red St. George's Cross , the white diagonal St. Andrew's Cross (not to be confused with the white "fimbriation"), and the red diagonal St. Patrick's Cross (the blue parts are actually the background, which – according to the Wikipedia entry for Blazon – is actually referred to as the "field").   I don't know why, but I love all of this stuff, such as learning the meaning behind word like fimbriation , which I now know refers to small stripes of colour (technically called "tincture" in this sense in heraldry) placed around "common charges" or "ordinaries." This is usually in order to make the "common charges" stand out from the "field" (background), but may be used "just because the designer felt it looked better," or to avoid what would otherwise be a violation of the heraldic "rule of tincture." In order to explain what I mean, the "fimbriation" of the "ordinaries" is highlighted in yellow in the following depiction of the Union Jack:     Quite apart from anything else, this is also the first time I really understood how to tell whether the Union Jack is being flown the right way up, or not:   The correct way to fly the Union Jack (top) and the incorrect way (bottom).   The thing is that the Union Jack does not have reflection symmetry due to the slight pin-wheeling of the St Patrick's and St Andrew's crosses (technically the "counterchange of saltires"). The end result is that there is a right side up and a wrong side up. To fly the flag correctly, the white of St Andrew should be above the red of St Patrick in the "upper hoist canton" (the quarter at the top nearest to the flag-pole). I remember first hearing about this as a kid. I was watching an episode of some cowboy program like Bonanza or High Chaparral or Gunsmoke (I forget which). The bad guys had taken an old Englishman captive in his cabin, but our heroes worked out that something was wrong because he'd hoisted his Union Jack upside down. Now that I understand, I will certainly keep a watchful eye open whilst on my travels so as to quickly detect if any skullduggery is afoot. I tell you, you can really get sucked into all of this stuff. For example, do you know why the canton of the flag of Hawaii contains the Union Flag of the United Kingdom, prominent over the top quarter closest to the flag mast (this is the only U.S. state flag to feature the Union Flag)?   The flag of Hawaii   If you wish to learn why this should be, all I can say is Click Here to visit the appropriate page on the Wikipedia, but don't blame me if you get sucked into the ensuing "Informational vortex of no return"...  
  • 热度 20
    2012-3-21 18:55
    2178 次阅读|
    0 个评论
    A union paired with a discrete value that indicates the active member of the union is called a discriminated union or a tagged union . Some programming languages have a similar construct called a variant record . The discrete value is called a discriminator or tag . I prefer the terms discriminated union and discriminator because tag already has another meaning in C. The discriminator typically has an enumeration type, but could have an integral type. You can write a small assortment of initialization functions to properly initialize each kind of shape. For example: void rectangle_construct (shape *s, double h, double w) { s-kind = sk_rectangle; s-u.rectangle.height = h; s-u.rectangle.width = w; } initializes a shape as a rectangle with a particular height and width. You can write similar initialization functions for circle and triangles. Using these functions, you can create an array of assorted shapes as follows: shape sa ; ~~~ circle_construct(sa , 2); triangle_construct(sa , 5, 6, asin(0.8)); rectangle_construct(sa , 3, 4); circle_construct(sa , 3); Suppose your application needs to determine the shape with the largest area in a collection of shapes. You can do the job with the function named largest shown in Listing 1 . Listing 1: A function that finds the largest shape in an array of shapes, using an overt switch statement. shape const *largest(shape const s .kind) { case sk_circle: area = PI * s .u.circle.radius * s .u.circle.radius; break; case sk_rectangle: area = s .u.rectangle.height * s .u.rectangle.width; break; case sk_triangle: area = sin(s .u.triangle.angle) * s .u.triangle.side1 * s .u.triangle.side2 / 2; break; } if (area max) { max = area; p = s ; } } return p; } Most of the loop body in Listing 1 is a switch statement that computes the area of a shape. This is arguably a fundamental shape operation that would be better packaged as an operation all by itself: double shape_area(shape const *s) { switch (s-kind) { case sk_circle: return PI * s-u.circle.radius * s-u.circle.radius; case sk_rectangle: return s-u.rectangle.height * s-u.rectangle.width; case sk_triangle: return sin(s-u.triangle.angle) * s-u.triangle.side1 * s-u.triangle.side2 / 2; } return -1; } Using the shape_area function dramatically simplifies the largest function, as shown in Listing 2 . Listing 2: A function that finds the largest shape in an array of shapes, using the shape_area function. shape const *largest(shape const s ); if (area max) { max = area; p = s ; } } return p; } Correctness and maintenance problems One of the problems with discriminated unions is that it requires a lot of discipline to ensure that you don't access a union member unless it's the currently active member as indicated by the discriminator. In most cases, this means you should be scrupulous about wrapping the accesses to union members inside if- or switch-statements that test the discriminator. Unfortunately, nothing in C prevents accidents such as: case sk_rectangle: area = s-u.triangle.side1 * s-u.triangle.side2; ~~~ This code will compile and then produce unpredictable, possibly erroneous, run-time results. Some languages, such as Ada, have built-in support for discriminated unions intended to prevent such mishaps. Andrei Alexandescru showed how you can implement safe discriminated unions in C++ using template metaprogramming techniques. 3, 4   Another problem with discriminated unions is that they can easily lead to code that's hard to maintain. For example, in addition to the shape_area function, your application might include other shape operations such as shape_perimeter, shape_resize, or shape_put. Each function has a similar, if not identical, switch-statement structure as shape_area. If you add a new shape, or modify the attributes of an existing shape, you probably have to change every one of those functions. Virtual functions provide an alternative to discriminated unions that are safe, efficient, and often more maintainable. I'll discuss virtual functions in my upcoming columns. Endnotes: 1. Saks, Dan. "Learn about new with placement," Eetindia.co.in , September 2011. http://forum.eetindia.co.in/BLOG_ARTICLE_9401.HTM 2. Saks, Dan. "Employ member new to map devices," Eetindia.co.in , November 2011. http://forum.eetindia.co.in/BLOG_ARTICLE_10437.HTM . 3. Alexandrescu, Andrei. "Discriminated Unions (I)," Dr. Dobbs Journal , April 1, 2002. http://drdobbs.com/184403821. 4. Alexandrescu, Andrei."Discriminated Unions (II)," Dr. Dobbs Journal , June 1, 2002. http://drdobbs.com/184403828.  
  • 热度 16
    2012-3-11 11:50
    1584 次阅读|
    0 个评论
    Off and on for about two years, I've been writing about techniques for representing and manipulating memory-mapped devices in C and C++. My more recent columns have been more about C++ than C, focusing on language features such as constructors and new-expressions, which C++ doesn't share with C. 1, 2 Some readers have suggested that the C code I presented is preferable to the C++ code because the C structure implementations for devices are generally simpler than their corresponding C++ class implementations. Last month, I argued that the C++ implementations are actually better because they're easier to use correctly and harder to use incorrectly. Other readers have claimed that my C++ implementations are flawed because they're too simple—they don't use inheritance and preclude the use of virtual functions. Classes with virtual functions can be very useful, but they aren't the solution to every problem. Classes that represent memory-mapped devices, such as the ones I've presented, work with real hardware specifically because they don't use virtual functions. The new C++ Standard acknowledges the usefulness of such classes by defining categories such as standard layout classes , which avoid features such as virtual functions. In the coming months, I'll explain what virtual functions are. I'll show why they can be useful in some applications, but undesirable in classes that represent memory-mapped device registers. I'll also show you how C++ typically implements virtual functions by showing how you can emulate them in C. I'll begin this month by looking at the sort of problem that virtual functions are good at solving. I'll show a typical C solution using a construct called a discriminated union, and examine its limitations. An illustrative problem Suppose you have an application that employs two-dimensional geometric shapes, such as circles, rectangles, and triangles. At a minimum, each shape object contains some linear or angular distances sufficient to characterize the physical extent of the shape. For example, a circle has a radius, a rectangle has a height and a width, and a triangle has two sides and an angle. The shapes may have common attributes as well, such a position (planar coordinates), or outline and fill colors. A fairly traditional C implementation for a shape is a structure with a nested union, such as: typedef struct shape shape; struct shape { coordinates position; color outline, fill; shape_kind kind; union { circle_part circle; rectangle_part rectangle; triangle_part triangle; } u; }; The union member, u, is large enough to hold the largest of its members, but it can only store the value of one member at a time. In this example, each union member has a different structure type. For a circle, you need to store only the radius, as in: typedef struct circle_part circle_part; struct circle_part { double radius; }; For a rectangle, you need the height and width: typedef struct rectangle_part rectangle_part; struct rectangle_part { double height, width; }; For a triangle, you need two sides and an adjacent angle: typedef struct triangle_part triangle_part; struct triangle_part { double side1, side2, angle; }; When the union members have such simple types, you might find it easier to dispense with the named structure types and simply define unnamed structures inside the union, as in: union { struct { double radius; } circle; struct { double height, width; } rectangle; struct { double side1, side2, angle; } triangle; } u; The value of the shape's kind member indicates which union member is currently in use. The shape_kind type enumerates the possible values: enum shape_kind { sk_circle, sk_rectangle, sk_triangle }; typedef enum shape_kind shape_kind; For example, setting a shape's kind member to sk_rectangle indicates that it's now OK to access (write to or read from) the union's rectangle member and its height and width members.    
相关资源
  • 所需E币: 3
    时间: 2020-1-13 10:00
    大小: 517.99KB
    上传者: givh79_163.com
    UnionSemiconductorCompanyProfile&ESDProduct-JRDHighPerformanceAnalogSolutionsforDigitalWorldUnionSemiconductor,Inc.CompanyProfile&ESDProtectionProductBusinessHighPerformanceAnalogSolutionsforDigitalWorldCompanyOutlookFoundedinJan.08,2001inShanghai,China.InitialCapital1.8MFablesschipdesignhouseFocusedonAnalog&MixedSignalchipdesign&marketing32EmployeeinChina,6DesignEngineers.SetupDistributionChannelinTaiwan,China&Japanin2005.HighPerformanceAnalogSolutionsforDigitalWorldProductPortfoliosStandard232,485InterfaceESDProtection&EMIFilterPowersupplies&ManagementProductAnalogSwitchHighPerformanceAnalogSolutionsforDigitalWorldISO9001:2000QualitySystemHighPerformanceAnalogSolutionsforDigitalWorldContractedManufacturingC……
  • 所需E币: 3
    时间: 2020-1-15 12:38
    大小: 374.87KB
    上传者: wsu_w_hotmail.com
    Union_UM3157_R01UM315710Low-VoltageSPDTAnalogSwitchUM3157SC70-6/SC88/SOT363GeneralDescriptionTheUM3157isanadvancedCMOSanalogswitchfabricatedwithsilicongateCMOStechnology.ItachievesverylowpropagationdelayandRDS(ON)resistanceswhilemaintainingCMOSlowpowerdissipation.Thesemakeitidealforportableandbatterypowerapplications.Theswitchconductssignalswithinpowerrailsequallywellinbothdirectionswhenon,andblocksuptothepowersupplylevelwhenoff.Break-before-makeisguaranteed.Theselectpinhasover-voltageprotectionthatallowsvoltagesaboveVCC,upto6.5Vtobepresentonthepinwithoutdamageordisruptionofoperationofthepart,regardlessoftheoperatingvoltage.ApplicationszzzzSample-and-HoldCircuitsBatterypoweredEquipmentA……
  • 所需E币: 4
    时间: 2020-1-15 12:55
    大小: 379.05KB
    上传者: rdg1993
    Union_SM05&G_R01SM05/GLowCapacitanceQuadLineESDProtectionDiodeArrySM05/GSOT23-3GeneralDescriptionTheSM05/Goftransientvoltagesuppressors(TVS)aredesignedtoprotectcomponentswhichareconnectedtodataandtransmissionlinesfromvoltagesurgescausedbyelectrostaticdischarge(ESD).TVSdiodesarecharacterizedbytheirhighsurgecapability,lowoperatingandclampingvoltages,andfastresponsetime.Thismakesthemidealforuseasboardlevelprotectionofsensitivesemiconductorcomponents.Thedual-junctioncommon-anodedesignallowstheusertoprotectonebidirectionaldatalineortwounidirectionallines.ThelowprofileSOT23packageallowsflexibilityinthedesignof“crowded”circuitboards.TheSM05/GwillmeetthesurgerequirementsofIEC61000-4-2(FormerlyIEC801-……
  • 所需E币: 3
    时间: 2020-1-15 14:10
    大小: 211.9KB
    上传者: 2iot
    Union_UM5059_R03UM5059SingleLineESDProtectionDiodeArrayUM5059GeneralDescriptionTheUM5059ESDprotectiondiodeisdesignedtoreplacemultilayervaristors(MLVs)inportableapplicationssuchascellphones,notebookcomputers,andPDA’s.Theyfeaturelargecross-sectionalareajunctionsforconductinghightransientcurrents,offerdesirableelectricalcharacteristicsforboardlevelprotection,suchasfastresponsetime,loweroperatingvoltage,lowerclampingvoltageandnodevicedegradationwhencomparedtoMLVs.TheUM5059ESDprotectiondiodeprotectssensitivesemiconductorcomponentsfromdamageorupsetduetoelectrostaticdischarge(ESD)andothervoltageinducedtransientevents.TheUM5059isavailableinDFN21.0×0.6(compatiblewithSOD923&SOD882)packagewithworkingvoltage……