File: GSCSSolution.m

package info (click to toggle)
gnustep-gui 0.32.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,348 kB
  • sloc: objc: 178,763; ansic: 24,089; cpp: 664; yacc: 464; sh: 90; makefile: 72
file content (101 lines) | stat: -rw-r--r-- 2,762 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright (C) 2023 Free Software Foundation, Inc.

   By: Benjamin Johnson
   Date: 19-3-2023
   This file is part of the GNUstep Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110 USA.
*/

#import "GSCSSolution.h"
#import "GSCSFloatComparator.h"
#import "GSFastEnumeration.h"

@implementation GSCSSolution

- (instancetype) init
{
  self = [super init];
  if (self)
    {
      ASSIGN(_resultsByVariable, [NSMapTable strongToStrongObjectsMapTable]);
    }
  return self;
}

- (void) setResult: (CGFloat)result forVariable: (GSCSVariable *)variable
{
  NSNumber *encodedResult = [NSNumber numberWithFloat: result];
  [_resultsByVariable setObject: encodedResult forKey: variable];
}

- (NSNumber *) resultForVariable: (GSCSVariable *)variable;
{
  return [_resultsByVariable objectForKey: variable];
}

- (NSArray *) variables
{
  NSMutableArray *variables = [NSMutableArray array];
  NSEnumerator *resultVariables = [_resultsByVariable keyEnumerator];
  FOR_IN(GSCSVariable *, variable, resultVariables)
    [variables addObject: variable];
  END_FOR_IN(resultVariables)

  return variables;
}

- (BOOL) solution: (GSCSSolution *)solution
    hasEqualResultForVariable: (GSCSVariable *)variable
{
  NSNumber *lhsResult = [self resultForVariable: variable];
  NSNumber *rhsResult = [solution resultForVariable: variable];

  BOOL hasResultForBoth = lhsResult != nil && rhsResult != nil;
  if (!hasResultForBoth)
    {
      return NO;
    }

  return [GSCSFloatComparator isApproxiatelyEqual: [lhsResult floatValue]
                                                b: [rhsResult floatValue]];
}

- (BOOL) isEqualToCassowarySolverSolution: (GSCSSolution *)solution
{
  if ([[self variables] count] != [[solution variables] count])
    {
      return NO;
    }

  NSArray *variables = [self variables];
  FOR_IN(GSCSVariable *, variable, variables)
    if (![self solution: solution hasEqualResultForVariable: variable])
      {
        return NO;
      }
  END_FOR_IN(variables)

  return YES;
}

- (void) dealloc
{
  RELEASE(_resultsByVariable);
  [super dealloc];
}

@end