stringmap.lpr 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program stringmap;
  26. {$MODE DELPHI}
  27. uses
  28. SysUtils,
  29. Classes,
  30. BrookStringMap;
  31. function map_sort(AData: Pointer; APairA, APairB: TBrookStringPair): Integer;
  32. begin
  33. Result := CompareStr(APairB.Name, APairA.Name); // desc
  34. end;
  35. function map_iter(AData: Pointer; APair: TBrookStringPair): Integer;
  36. begin
  37. WriteLn(#9, APair.Name[1], ': ', APair.Name);
  38. Result := 0;
  39. end;
  40. procedure chat(AMap: TBrookStringMap; const AName, AMsg: string);
  41. var
  42. VPair: TBrookStringPair;
  43. begin
  44. AMap.AddOrSet(AName, AMsg);
  45. if AMap.Find(AName, VPair) then
  46. WriteLn(VPair.Name[1], ':'#9, VPair.Value);
  47. end;
  48. var
  49. map: TBrookStringMap;
  50. begin
  51. map := TBrookStringMap.Create(nil);
  52. try
  53. chat(map, 'Clecio', 'Hello!');
  54. chat(map, 'Paim', 'Hello. How are you?');
  55. chat(map, 'Clecio', 'I''m fine. And you?');
  56. chat(map, 'Paim', 'Me too.');
  57. WriteLn;
  58. WriteLn('Chatters:');
  59. map.Sort(map_sort, nil);
  60. map.Iterate(map_iter, nil);
  61. {$IFDEF MSWINDOWS}
  62. ReadLn;
  63. {$ENDIF}
  64. finally
  65. map.Free;
  66. end;
  67. end.