stringmap.lpr 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 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. {$WARN 5024 OFF}
  28. uses
  29. SysUtils,
  30. Classes,
  31. BrookStringMap;
  32. function map_sort(AData: Pointer; APairA, APairB: TBrookStringPair): Integer;
  33. begin
  34. Result := CompareStr(APairB.Name, APairA.Name); // desc
  35. end;
  36. function map_iter(AData: Pointer; APair: TBrookStringPair): Integer;
  37. begin
  38. WriteLn(#9, APair.Name[1], ': ', APair.Name);
  39. Result := 0;
  40. end;
  41. procedure chat(AMap: TBrookStringMap; const AName, AMsg: string);
  42. var
  43. VPair: TBrookStringPair;
  44. begin
  45. AMap.AddOrSet(AName, AMsg);
  46. if AMap.Find(AName, VPair) then
  47. WriteLn(VPair.Name[1], ':'#9, VPair.Value);
  48. end;
  49. var
  50. h: Pointer;
  51. map: TBrookStringMap;
  52. begin
  53. map := TBrookStringMap.Create(@h);
  54. try
  55. chat(map, 'Clecio', 'Hello!');
  56. chat(map, 'Paim', 'Hello. How are you?');
  57. chat(map, 'Clecio', 'I''m fine. And you?');
  58. chat(map, 'Paim', 'Me too.');
  59. WriteLn;
  60. WriteLn('Chatters:');
  61. map.Sort(map_sort, nil);
  62. map.Iterate(map_iter, nil);
  63. {$IFDEF MSWINDOWS}
  64. ReadLn;
  65. {$ENDIF}
  66. finally
  67. map.Free;
  68. end;
  69. end.